Thread-safe, mutable application states for rust.
Examples
Initializing the state
Any app state that may be used must be initialized first.
Note: An AppState can not be used as a MutAppState and vice versa.
This means that AppState and MutAppState must be initialized separately
and use independent values, even if they are of the same type.
use ;
Using derive
In order to avoid boilerplate code, the InitAppState and InitMutAppState
traits can be derived for any struct. These traits provide the init_app_state
and init_mut_app_state methods respectively which can be used to initialize
the state more easily.
use ;
Read-only state
App states internally use Arc to allow for thread-safe access.
use ;
Mutable state
Mutable states internally use a Mutex to ensure thread-safety.
This means that when reading from or writing to the state, the mutex must be locked.
This can be done either by calling get_mut() or by using the MutAppStateLock type.
use ;
Mutable state (locked)
In order to mutate the state, you must first lock it.
This can be done either by calling get_mut() or by using the MutAppStateLock type.
use ;
Get the state manually
You can also get the state manually by calling AppState::get() or MutAppState::get().
use app_state::{AppState, MutAppState, AppStateTrait};
struct MyState {
counter: u32,
}
fn main() {
let state = AppState::<MyState>::get();
let mut_state = MutAppState::<MyState>::get();
}