chiark / gitweb /
global: Provide InstanceWeakRef
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 20 Mar 2021 17:29:02 +0000 (17:29 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 20 Mar 2021 21:46:02 +0000 (21:46 +0000)
The chess clock is going to need this.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/global.rs

index efcef687838647f2845cf9d18061e4773f8a6a66..4e31e66a669bac8fb8f8754109cdda787161e878 100644 (file)
@@ -37,6 +37,9 @@ pub struct InstanceName {
 #[derive(Debug,Clone)]
 pub struct InstanceRef (Arc<Mutex<InstanceContainer>>);
 
+#[derive(Debug,Clone)]
+pub struct InstanceWeakRef (std::sync::Weak<Mutex<InstanceContainer>>);
+
 #[derive(Debug,Clone,Serialize,Deserialize,Default)]
 #[serde(transparent)]
 pub struct LinksTable(pub EnumMap<LinkKind, Option<String>>);
@@ -287,6 +290,16 @@ impl InstanceRef {
       Err(poison) => poison.into_inner(),
     }
   }
+
+  pub fn downgrade_to_weak(&self) -> InstanceWeakRef {
+    InstanceWeakRef(Arc::downgrade(&self.0))
+  }
+}
+
+impl InstanceWeakRef {
+  pub fn upgrade(&self) -> Option<InstanceRef> {
+    Some(InstanceRef(self.0.upgrade()?))
+  }
 }
 
 impl<A> Unauthorised<InstanceRef, A> {