Эх сурвалжийг харах

First available version OK.

ZRY 1 жил өмнө
parent
commit
3fce1efad4
8 өөрчлөгдсөн 120 нэмэгдсэн , 0 устгасан
  1. 6 0
      .gitignore
  2. 8 0
      .idea/.gitignore
  3. 8 0
      .idea/modules.xml
  4. 12 0
      .idea/simple-rw-global.iml
  5. 6 0
      .idea/vcs.xml
  6. 6 0
      Cargo.toml
  7. 3 0
      README.md
  8. 71 0
      src/lib.rs

+ 6 - 0
.gitignore

@@ -11,3 +11,9 @@
 # Generated by Cargo
 /target/
 
+
+
+# Added by cargo
+
+/target
+/Cargo.lock

+ 8 - 0
.idea/.gitignore

@@ -0,0 +1,8 @@
+# 默认忽略的文件
+/shelf/
+/workspace.xml
+# 基于编辑器的 HTTP 客户端请求
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml

+ 8 - 0
.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/simple-rw-global.iml" filepath="$PROJECT_DIR$/.idea/simple-rw-global.iml" />
+    </modules>
+  </component>
+</project>

+ 12 - 0
.idea/simple-rw-global.iml

@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="WEB_MODULE" version="4">
+  <component name="Go" enabled="true" />
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$">
+      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+      <excludeFolder url="file://$MODULE_DIR$/target" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="" vcs="Git" />
+  </component>
+</project>

+ 6 - 0
Cargo.toml

@@ -0,0 +1,6 @@
+[package]
+name = "simple-rw-global"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]

+ 3 - 0
README.md

@@ -1,2 +1,5 @@
 # simple-rw-global
 
+As is.
+
+Based on `std::sync::RwLock`.

+ 71 - 0
src/lib.rs

@@ -0,0 +1,71 @@
+use std::ops::{Deref, DerefMut};
+use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
+
+pub enum GOption<T> {
+    None,
+    Some(T)
+}
+
+impl<T> Deref for GOption<T> {
+    type Target = T;
+
+    fn deref(&self) -> &Self::Target {
+        match self {
+            GOption::None => { panic!("operate on empty GlobalContainer"); },
+            GOption::Some(t) => t,
+        }
+    }
+}
+
+impl<T> DerefMut for GOption<T> {
+
+    fn deref_mut(&mut self) -> &mut Self::Target {
+        match self {
+            GOption::None => { panic!("operate on empty GlobalContainer"); },
+            GOption::Some(t) => t,
+        }
+    }
+}
+
+pub struct GlobalContainer<T>
+{
+    content: RwLock<GOption<T>>,
+}
+
+impl<T> GlobalContainer<T> {
+    pub const fn new() -> GlobalContainer<T> {
+        GlobalContainer::<T>{
+            content: RwLock::new(GOption::None),
+        }
+    }
+
+    pub fn set(&self, obj: T) {
+        *self.content.write().unwrap() = GOption::Some(obj);
+    }
+
+    pub fn is_empty(&self) -> bool {
+        let v = self.content.read().unwrap();
+        match v.deref() {
+            GOption::None => true,
+            GOption::Some(_) => false,
+        }
+    }
+
+    pub fn manual_drop(&self) {
+        *self.content.write().unwrap() = GOption::None;
+    }
+
+    pub fn get(&self) -> RwLockReadGuard<GOption<T>> {
+        self.content.read().unwrap()
+    }
+
+    pub fn get_mut(&self) -> RwLockWriteGuard<GOption<T>> {
+        self.content.write().unwrap()
+    }
+}
+
+impl<T> Drop for GlobalContainer<T> {
+    fn drop(&mut self) {
+        self.manual_drop();
+    }
+}