[−][src]Struct reducer::Store
A reactive state container.
The only way to mutate the internal state managed by Store is by dispatching actions on it. The associated reactor is notified upon every state transition.
Example
use reducer::*; use std::error::Error; use std::io::{self, Write}; // The state of your app. struct Calculator(i32); // Actions the user can trigger. struct Add(i32); struct Sub(i32); struct Mul(i32); struct Div(i32); impl Reducer<Add> for Calculator { fn reduce(&mut self, Add(x): Add) { self.0 += x; } } impl Reducer<Sub> for Calculator { fn reduce(&mut self, Sub(x): Sub) { self.0 -= x; } } impl Reducer<Mul> for Calculator { fn reduce(&mut self, Mul(x): Mul) { self.0 *= x; } } impl Reducer<Div> for Calculator { fn reduce(&mut self, Div(x): Div) { self.0 /= x; } } // The user interface. struct Display; impl Reactor<Calculator> for Display { type Output = io::Result<()>; fn react(&self, state: &Calculator) -> Self::Output { io::stdout().write_fmt(format_args!("{}\n", state.0)) } } fn main() -> Result<(), Box<dyn Error>> { let mut store = Store::new(Calculator(0), Display); store.dispatch(Add(5))?; // displays "5" store.dispatch(Mul(3))?; // displays "15" store.dispatch(Sub(1))?; // displays "14" store.dispatch(Div(7))?; // displays "2" Ok(()) }
Methods
impl<S, R: Reactor<S>> Store<S, R>
[src]
pub fn new(state: S, reactor: R) -> Self
[src]
Constructs the Store given the initial state and a reactor.
pub fn subscribe(&mut self, reactor: impl Into<R>) -> R
[src]
Replaces the reactor and returns the previous one.
Trait Implementations
impl<A, S, R> Dispatcher<A> for Store<S, R> where
S: Reducer<A>,
R: Reactor<S>,
[src]
S: Reducer<A>,
R: Reactor<S>,
type Output = R::Output
fn dispatch(&mut self, action: A) -> R::Output
[src]
Updates the state via Reducer<A>::reduce
and notifies the reactor,
returning the result of calling Reactor<S>::react
with a reference
to the new state.
impl<S: Eq, R: Eq + Reactor<S>> Eq for Store<S, R>
[src]
impl<S: Clone, R: Clone + Reactor<S>> Clone for Store<S, R>
[src]
fn clone(&self) -> Store<S, R>
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<S: PartialEq, R: PartialEq + Reactor<S>> PartialEq<Store<S, R>> for Store<S, R>
[src]
impl<S: Copy, R: Copy + Reactor<S>> Copy for Store<S, R>
[src]
impl<S: Default, R: Default + Reactor<S>> Default for Store<S, R>
[src]
impl<S, R: Reactor<S>> Deref for Store<S, R>
[src]
type Target = S
The resulting type after dereferencing.
fn deref(&self) -> &Self::Target
[src]
Grants read access to the current state.
impl<S: Debug, R: Debug + Reactor<S>> Debug for Store<S, R>
[src]
impl<S: Hash, R: Hash + Reactor<S>> Hash for Store<S, R>
[src]
Auto Trait Implementations
impl<S, R> Send for Store<S, R> where
R: Send,
S: Send,
R: Send,
S: Send,
impl<S, R> Sync for Store<S, R> where
R: Sync,
S: Sync,
R: Sync,
S: Sync,
Blanket Implementations
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,