Browse Source

add more blocking access for tokio feature.

ZRY 1 year ago
parent
commit
7aead82d29
1 changed files with 21 additions and 2 deletions
  1. 21 2
      src/tokio/mod.rs

+ 21 - 2
src/tokio/mod.rs

@@ -87,6 +87,10 @@ impl<T> AsyncGlobalContainer<T> {
         *self.content.write().await = GOption::Some(obj);
     }
 
+    pub fn blocking_set(&self, obj: T) {
+        *self.content.blocking_write() = GOption::Some(obj);
+    }
+
     pub async fn is_empty(&self) -> bool {
         let v = self.content.read().await;
         match v.deref() {
@@ -95,8 +99,16 @@ impl<T> AsyncGlobalContainer<T> {
         }
     }
 
-    pub async fn manual_drop(&self) {
-        *self.content.write().await = GOption::None;
+    pub fn is_empty_blocking(&self) -> bool {
+        let v = self.content.blocking_read();
+        match v.deref() {
+            GOption::None => true,
+            GOption::Some(_) => false,
+        }
+    }
+
+    pub fn manual_drop(&self) {
+        *self.content.blocking_write() = GOption::None;
     }
 
     pub async fn get(&self) -> RwLockReadGuard<GOption<T>> {
@@ -115,3 +127,10 @@ impl<T> AsyncGlobalContainer<T> {
         self.content.blocking_write()
     }
 }
+
+#[cfg(feature = "tokio")]
+impl<T> Drop for AsyncGlobalContainer<T> {
+    fn drop(&mut self) {
+        self.manual_drop();
+    }
+}