pub struct Store<S, A: Action> { /* private fields */ }Expand description
Centralized state store with Redux-like reducer pattern
The store holds the application state and provides a single point
for state mutations through the dispatch method.
§Type Parameters
S- The application state typeA- The action type (must implementAction)
§Example
ⓘ
#[derive(Default)]
struct AppState {
counter: i32,
}
#[derive(Action, Clone, Debug)]
enum MyAction {
Increment,
Decrement,
}
fn reducer(state: &mut AppState, action: MyAction) -> bool {
match action {
MyAction::Increment => {
state.counter += 1;
true
}
MyAction::Decrement => {
state.counter -= 1;
true
}
}
}
let mut store = Store::new(AppState::default(), reducer);
store.dispatch(MyAction::Increment);
assert_eq!(store.state().counter, 1);Implementations§
Source§impl<S, A: Action> Store<S, A>
impl<S, A: Action> Store<S, A>
Sourcepub fn new(state: S, reducer: Reducer<S, A>) -> Self
pub fn new(state: S, reducer: Reducer<S, A>) -> Self
Create a new store with initial state and reducer
Auto Trait Implementations§
impl<S, A> Freeze for Store<S, A>where
S: Freeze,
impl<S, A> RefUnwindSafe for Store<S, A>where
S: RefUnwindSafe,
A: RefUnwindSafe,
impl<S, A> Send for Store<S, A>where
S: Send,
impl<S, A> Sync for Store<S, A>
impl<S, A> Unpin for Store<S, A>
impl<S, A> UnwindSafe for Store<S, A>where
S: UnwindSafe,
A: UnwindSafe,
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more