AppStateTrait

Trait AppStateTrait 

Source
pub trait AppStateTrait<T, U>
where T: 'static + Send, U: 'static + AppStateTrait<T, U> + CreateAppState<T> + Clone + Send,
{ // 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§

Source

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);
}
Source

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 });
}
Source

fn get() -> U

Returns a reference to the state. If the state store has not been initialized, this will panic.

Source

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.

Source

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.

Source

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.

Source

fn get_or_insert_default() -> U
where 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.

Implementors§

Source§

impl<T: 'static + Send + Sync> AppStateTrait<T, AppState<T>> for AppState<T>

Source§

impl<T: 'static + Send> AppStateTrait<T, MutAppState<T>> for MutAppState<T>