Struct stderr::StaticMut [] [src]

pub struct StaticMut<T>(_);

Internal variability for Lazystatic

Example:

On Cargo.toml:

lazy_static = "^0.2.8"
stderr = "0.8.0" 

On Code:

#[macro_use]
extern crate lazy_static;
extern crate stderr;
use stderr::StaticMut;

lazy_static!{
    static ref STRING : StaticMut<String> = StaticMut::new(String::new());
    static ref USIZE : StaticMut<Usize> = StaticMut::new(Usize(0));
}

fn main() {
    // Before write, You can read it Concurrent safely.
    println!("{:?}", STRING.as_ref());
    println!("{:?}", USIZE.as_ref());

    let str = {
        let mut str = "StaticMut, 雪之下".to_string();
        // do some work
        str
    };
    // But when write it, operate it Concurrent is unsafe.
    {
        STRING.set(str); // update by setting value
        USIZE.as_mut().0 = 123; // update by modifying field
    }

    // After write, You can read it Concurrent safely.
    println!("{:?}", STRING.as_ref());
    println!("{:?}", USIZE.as_ref());
}

#[derive(Debug)]
struct Usize(usize);

About safe and unsafe

If you read or write it when you write it, I don't ensure anything.

You can add AtomicXXX to ensure safe on above situation also.

If you need full Concurrent read and write, you maybe need RwLock

Methods

impl<T> StaticMut<T>
[src]

read it

write it

update it

Unwraps the value

Trait Implementations

impl<T: Debug> Debug for StaticMut<T>
[src]

Formats the value using the given formatter.

impl<T> Sync for StaticMut<T>
[src]