Browse Source

Add documentation.

ZRY 1 year ago
parent
commit
7d69991b13
1 changed files with 48 additions and 0 deletions
  1. 48 0
      src/lib.rs

+ 48 - 0
src/lib.rs

@@ -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};