[][src]Struct reducer::Store

pub struct Store<R, S: Reactor<R>> { /* fields omitted */ }

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<R, S: Reactor<R>> Store<R, S>[src]

pub fn new(state: R, reactor: S) -> Self[src]

Constructs the Store given the initial state and a reactor.

pub fn subscribe(
    &mut self,
    reactor: impl Into<S>
) -> S
[src]

Replaces the reactor and returns the previous one.

Trait Implementations

impl<A, R, S> Dispatcher<A> for Store<R, S> where
    R: Reducer<A>,
    S: Reactor<R>, 
[src]

type Output = S::Output

fn dispatch(&mut self, action: A) -> S::Output[src]

Updates the state via <R as Reducer<A>>::reduce and notifies the reactor, returning the result of calling <S as Reactor<R>>::react with a reference to the new state.

impl<R: Eq, S: Eq + Reactor<R>> Eq for Store<R, S>[src]

impl<R: Clone, S: Clone + Reactor<R>> Clone for Store<R, S>[src]

fn clone_from(&mut self, source: &Self)
1.0.0
[src]

Performs copy-assignment from source. Read more

impl<R: Copy, S: Copy + Reactor<R>> Copy for Store<R, S>[src]

impl<R: PartialEq, S: PartialEq + Reactor<R>> PartialEq<Store<R, S>> for Store<R, S>[src]

impl<R: Default, S: Default + Reactor<R>> Default for Store<R, S>[src]

impl<R: Hash, S: Hash + Reactor<R>> Hash for Store<R, S>[src]

fn hash_slice<H>(data: &[Self], state: &mut H) where
    H: Hasher
1.3.0
[src]

Feeds a slice of this type into the given [Hasher]. Read more

impl<R: Debug, S: Debug + Reactor<R>> Debug for Store<R, S>[src]

Auto Trait Implementations

impl<R, S> Send for Store<R, S> where
    R: Send,
    S: Send

impl<R, S> Sync for Store<R, S> where
    R: Sync,
    S: Sync

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]