|
@@ -1,3 +1,51 @@
|
|
|
+//! Simple GlobalContainer based on std::sync::RwLock.
|
|
|
+//!
|
|
|
+//! This crate is AS IS.
|
|
|
+//!
|
|
|
+//! By ZRY.
|
|
|
+//!
|
|
|
+//! # Example:
|
|
|
+//!
|
|
|
+//! ```
|
|
|
+//! use simple_rw_global::GlobalContainer;
|
|
|
+//!
|
|
|
+//! static GLOBAL : GlobalContainer<GlobalObject> = GlobalContainer::<GlobalObject>::new();
|
|
|
+//!
|
|
|
+//! pub struct GlobalObject {
|
|
|
+//! log_prefix: String,
|
|
|
+//! }
|
|
|
+//!
|
|
|
+//! impl GlobalObject {
|
|
|
+//! pub fn new(prefix: &str) -> GlobalObject {
|
|
|
+//! GlobalObject{log_prefix: String::from(prefix)}
|
|
|
+//! }
|
|
|
+//!
|
|
|
+//! pub fn log(&self, msg: &str) {
|
|
|
+//! println!("[{}] {}", self.log_prefix.as_str(), msg);
|
|
|
+//! }
|
|
|
+//!
|
|
|
+//! pub fn change_prefix(&mut self, prefix: &str) {
|
|
|
+//! self.log_prefix = String::from(prefix);
|
|
|
+//! }
|
|
|
+//! }
|
|
|
+//!
|
|
|
+//! fn main() {
|
|
|
+//! println!("before init, is empty: {}", GLOBAL.is_empty());
|
|
|
+//! GLOBAL.set(GlobalObject::new("log-test1"));
|
|
|
+//! println!("after init, is empty: {}", GLOBAL.is_empty());
|
|
|
+//! println!("init ok.");
|
|
|
+//! GLOBAL.get().log("hello 1");
|
|
|
+//! GLOBAL.get_mut().change_prefix("log-test2");
|
|
|
+//! GLOBAL.get().log("hello 2");
|
|
|
+//! GLOBAL.manual_drop();
|
|
|
+//! println!("dropped manually. will panic then.");
|
|
|
+//! GLOBAL.get().log("test after drop.");
|
|
|
+//! }
|
|
|
+//!
|
|
|
+//! ```
|
|
|
+//!
|
|
|
+//!
|
|
|
+
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
|
|
|
|