mut_static

Provides a struct to help create mutable statics with lazy_static.

(crates.io)

Quickstart

To create a mutable static simply put your lazystatic object inside of a MutStatic: ``` rust use mutstatic::MutStatic; use std::mem; use std::ops::DerefMut;

pub struct MyStruct { value: usize }

impl MyStruct { pub fn new(value: usize) -> Self { MyStruct{ value: value } }

pub fn get_value(&self) -> usize {
    self.value
}

pub fn set_value(&mut self, value: usize) {
    self.value = value
}

}

// Declaring a MutStatic lazystatic! { pub static ref MYSTRUCT: MutStatic = { MutStatic::new() }; }

// Declaring a MutStatic which already has data lazystatic! { pub static ref MYSTRUCT_PRESET: MutStatic = { MutStatic::from(MyStruct::new(0)) }; }

fn main() { // Setting a MutStatic MY_STRUCT.set(MyStruct::new(0)).unwrap();

// Using a MutStatic
{
    let my_struct = MY_STRUCT.read().unwrap();
    assert!(my_struct.get_value() == 0);
}

// Using a MutStatic mutably
{
    let mut my_struct = MY_STRUCT.write().unwrap();
    my_struct.set_value(1);
    assert!(my_struct.get_value() == 1);
}

// Resetting a MutStatic
{
    let mut my_struct = MY_STRUCT.write().unwrap();
    mem::replace(my_struct.deref_mut(), MyStruct::new(2));
    assert!(my_struct.get_value() == 2);
}

} ```