pub struct Store<S, R> { /* private fields */ }
Expand description

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

Turns the Store into a task that can be spawned onto an executor (requires async).

Once spawned, the task will receive actions dispatched through a lightweight Dispatcher handle that can be cloned and sent to other threads.

The task completes

Turning a Store into an asynchronous task requires all actions to be of the same type A; an effective way of fulfilling this requirement is to define actions as enum variants.

Example
use reducer::*;
use futures::prelude::*;
use std::error::Error;
use std::io::{self, Write};
use tokio::task::spawn;

// The state of your app.
#[derive(Clone)]
struct Calculator(i32);

// Actions the user can trigger.
enum Action {
    Add(i32),
    Sub(i32),
    Mul(i32),
    Div(i32),
}

impl Reducer<Action> for Calculator {
    fn reduce(&mut self, action: Action) {
        match action {
            Action::Add(x) => self.0 += x,
            Action::Sub(x) => self.0 -= x,
            Action::Mul(x) => self.0 *= x,
            Action::Div(x) => self.0 /= x,
        }
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let store = Store::new(
        Calculator(0),
        AsyncReactor(sink::unfold((), |_, state: Calculator| async move {
            writeln!(&mut io::stdout(), "{}", state.0)
        })),
    );

    // Process incoming actions on a background task.
    let (task, mut dispatcher) = store.into_task();
    let handle = spawn(task);

    dispatcher.dispatch(Action::Add(5))?; // asynchronously prints "5" to stdout
    dispatcher.dispatch(Action::Mul(3))?; // asynchronously prints "15" to stdout
    dispatcher.dispatch(Action::Sub(1))?; // asynchronously prints "14" to stdout
    dispatcher.dispatch(Action::Div(7))?; // asynchronously prints "2" to stdout

    // Closing the asynchronous dispatcher signals to the background task that
    // it can terminate once all pending actions have been processed.
    dispatcher.close().await?;

    // Wait for the background task to terminate.
    handle.await??;

    Ok(())
}

Constructs the Store given the initial state and a Reactor.

Replaces the Reactor and returns the previous one.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

The resulting type after dereferencing.

Dereferences the value.

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

Feeds this value into the given Hasher. Read more

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

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

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

Attempts to prepare the Sink to receive a value. Read more

Begin the process of sending a value to the sink. Each call to this function must be preceded by a successful call to poll_ready which returned Poll::Ready(Ok(())). Read more

Flush any remaining output from this sink. Read more

Flush any remaining output and close this sink, if necessary. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Composes a function in front of the sink. Read more

Composes a function in front of the sink. Read more

Transforms the error returned by the sink.

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

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

Close the sink.

Fanout items to multiple sinks. Read more

Flush the sink, processing all pending items. Read more

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

A future that completes after the given item has been received by the sink. Read more

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

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

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

A convenience method for calling [Sink::poll_ready] on Unpin sink types. Read more

A convenience method for calling [Sink::start_send] on Unpin sink types. Read more

A convenience method for calling [Sink::poll_flush] on Unpin sink types. Read more

A convenience method for calling [Sink::poll_close] on Unpin sink types. Read more

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

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

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.