pub struct AppState<T: ?Sized>(/* private fields */);Expand description
A thread-safe, read-only state container.
§Examples
use app_state::{AppState, AppStateTrait, stateful};
struct MyState {
counter: u32,
}
#[stateful]
fn func(state: AppState<MyState>) {
let state = state.get_ref();
println!("Counter: {}", state.counter);
}Implementations§
Trait Implementations§
Source§impl<T: 'static + Send + Sync> AppStateTrait<T, AppState<T>> for AppState<T>
impl<T: 'static + Send + Sync> AppStateTrait<T, AppState<T>> for AppState<T>
Source§fn 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. Read more
Source§fn 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. Read moreSource§fn get() -> U
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>>
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
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
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() -> 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.Source§impl<T: 'static + Send> CreateAppState<T> for AppState<T>
impl<T: 'static + Send> CreateAppState<T> for AppState<T>
Auto Trait Implementations§
impl<T> Freeze for AppState<T>where
T: ?Sized,
impl<T> RefUnwindSafe for AppState<T>where
T: RefUnwindSafe + ?Sized,
impl<T> Send for AppState<T>
impl<T> Sync for AppState<T>
impl<T> Unpin for AppState<T>where
T: ?Sized,
impl<T> UnwindSafe for AppState<T>where
T: RefUnwindSafe + ?Sized,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more