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

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]

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

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

Performs copy-assignment from source. Read more

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

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

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

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

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

View Store as a read-only owning smart pointer to the state.

type Target = S

The resulting type after dereferencing.

fn deref(&self) -> &Self::Target[src]

Grants read access to the current state.

impl<S: Hash, R: Hash + Reactor<S>> Hash for Store<S, R>[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<A, S, R, E> Sink<A> for Store<S, R> where
    S: Reducer<A> + Unpin + Clone,
    R: Reactor<S, Error = E> + Sink<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.

Auto Trait Implementations

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

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

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

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

Blanket Implementations

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

type Owned = T

The resulting type after obtaining ownership.

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

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

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.

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

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

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

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

fn with<U, Fut, F, E>(self, f: F) -> With<Self, Item, U, Fut, F> where
    E: From<Self::Error>,
    F: FnMut(U) -> Fut,
    Fut: Future<Output = Result<Item, E>>, 
[src]

Composes a function in front of the sink. Read more

fn with_flat_map<U, St, F>(self, f: F) -> WithFlatMap<Self, Item, U, St, F> where
    F: FnMut(U) -> St,
    St: Stream<Item = Result<Item, Self::Error>>, 
[src]

Composes a function in front of the sink. Read more

fn sink_map_err<E, F>(self, f: F) -> SinkMapErr<Self, F> where
    F: FnOnce(Self::Error) -> E, 
[src]

Transforms the error returned by the sink.

fn sink_err_into<E>(self) -> SinkErrInto<Self, Item, E> where
    Self::Error: Into<E>, 
[src]

Map this sink's error to a different error type using the Into trait. Read more

fn buffer(self, capacity: usize) -> Buffer<Self, Item>[src]

Adds a fixed-size buffer to the current sink. Read more

fn close(&mut self) -> Close<Self, Item> where
    Self: Unpin
[src]

Close the sink.

fn fanout<Si>(self, other: Si) -> Fanout<Self, Si> where
    Item: Clone,
    Si: Sink<Item, Error = Self::Error>, 
[src]

Fanout items to multiple sinks. Read more

fn flush(&mut self) -> Flush<Self, Item> where
    Self: Unpin
[src]

Flush the sync, processing all pending items. Read more

fn send(&mut self, item: Item) -> Send<Self, Item> where
    Self: Unpin
[src]

A future that completes after the given item has been fully processed into the sink, including flushing. Read more

fn send_all<St>(&'a mut self, stream: &'a mut St) -> SendAll<'a, Self, St> where
    Self: Unpin,
    St: Stream<Item = Item> + Unpin
[src]

A future that completes after the given stream has been fully processed into the sink, including flushing. Read more

fn left_sink<Si2>(self) -> Either<Self, Si2> where
    Si2: Sink<Item, Error = Self::Error>, 
[src]

Wrap this sink in an Either sink, making it the left-hand variant of that Either. Read more

fn right_sink<Si1>(self) -> Either<Si1, Self> where
    Si1: Sink<Item, Error = Self::Error>, 
[src]

Wrap this stream in an Either stream, making it the right-hand variant of that Either. Read more