pub trait AppStateTrait<T, U>{
// Provided methods
fn init(state: T) { ... }
fn init_if_not_exists<F: FnOnce() -> T>(state: F) { ... }
fn get() -> U { ... }
fn try_get() -> Result<U, Box<dyn Error>> { ... }
fn get_or_insert(val: T) -> U { ... }
fn get_or_insert_with<F: FnOnce() -> T>(f: F) -> U { ... }
fn get_or_insert_default() -> U
where T: Default { ... }
}Provided Methods§
Sourcefn init(state: T)
fn init(state: T)
Initializes the state store with the given state. If the state store has already been initialized, this will overwrite the existing state.
§Examples
use app_state::{AppState, AppStateTrait};
struct MyState {
counter: u32,
}
fn main() {
let state = MyState { counter: 0 };
AppState::init(state);
}Sourcefn init_if_not_exists<F: FnOnce() -> T>(state: F)
fn init_if_not_exists<F: FnOnce() -> T>(state: F)
Initializes the state store with the given state.
If the state store has already been initialized, this will do nothing.
This is useful for initializing the state store with a default state.
If you want to overwrite the existing state, use init instead.
§Examples
use app_state::{AppState, AppStateTrait};
struct MyState {
counter: u32,
}
fn main() {
AppState::init_if_not_exists(|| MyState { counter: 0 });
}Sourcefn get() -> U
fn get() -> U
Returns a reference to the state. If the state store has not been initialized, this will panic.
Sourcefn try_get() -> Result<U, Box<dyn Error>>
fn try_get() -> Result<U, Box<dyn Error>>
Returns a reference to the state.
If the state store has not been initialized, this will return Err.
Sourcefn get_or_insert(val: T) -> U
fn get_or_insert(val: T) -> U
Returns a reference to the state. Inserts the supplied value if the state store has not been initialized.
Sourcefn get_or_insert_with<F: FnOnce() -> T>(f: F) -> U
fn get_or_insert_with<F: FnOnce() -> T>(f: F) -> U
Returns a reference to the state. Inserts the supplied value if the state store has not been initialized.
Sourcefn get_or_insert_default() -> Uwhere
T: Default,
fn get_or_insert_default() -> Uwhere
T: Default,
Returns a reference to the state.
Inserts the default value of T if the state store has not been initialized.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.