[][src]Struct reducer::Store

pub struct Store<S, R: Reactor<S>> { /* 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 Console;

impl Reactor<Calculator> for Console {
    type Error = io::Error;
    fn react(&mut self, state: &Calculator) -> io::Result<()> {
        io::stdout().write_fmt(format_args!("{}\n", state.0))
    }
}

fn main() -> Result<(), Box<dyn Error>> {
    let mut store = Store::new(Calculator(0), Console);

    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(())
}

Implementations

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

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

impl<S: Debug, R: Debug + Reactor<S>> Debug 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.

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

type Output = Result<(), R::Error>

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

Updates the state via Reducer::reduce and notifies the Reactor, returning the result of calling Reactor::react with a reference to the new state.

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

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

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

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

impl<A, S, R, E> Sink<A> for Store<S, R> where
    S: Reducer<A>,
    R: Reactor<S, Error = E> + for<'s> Sink<&'s S, Error = E>, 
[src]

View Store as a Sink of actions (requires async).

type Error = E

The type of value produced by the sink when an error occurs.

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

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

impl<'pin, S, R: Reactor<S>> Unpin for Store<S, R> where
    __Store<'pin, S, R>: Unpin
[src]

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

Auto Trait Implementations

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

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

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

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

Blanket Implementations

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

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

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

impl<T> From<T> for T[src]

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

impl<T, Item> SinkExt<Item> for T where
    T: Sink<Item> + ?Sized
[src]

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

type Owned = T

The resulting type after obtaining ownership.

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

type Error = Infallible

The type returned in the event of a conversion error.

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

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

The type returned in the event of a conversion error.