Crate simple_rw_global

Source
Expand description

Simple GlobalContainer based on std::sync::RwLock.

This crate is AS IS.

NOTE: GlobalContainer use RWLock, GlobalMutexContainer use Mutex.

By ZRY.

§Features

§tokio

For tokio, use feature tokio

[dependency]
simple-rw-global = { version="0.3.0", feature=["tokio"] }

With this feature, module tokio is available.

§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.");
}

Modules§

tokiotokio
Async version for tokio, need feature tokio.

Structs§

GlobalContainer
GlobalMutexContainer

Enums§

GOption