[][src]Struct reducer::Async

pub struct Async<D: Dispatcher<A>, A> { /* fields omitted */ }

An asynchronous adapter for dispatchers (requires async).

Once Async is spawned actions may be dispatched on it through its AsyncHandle.

Async requires all actions to be of the same type A. An effective way to fulfill this requirement, is to use an enum to represent actions.

Example

use futures::executor::ThreadPoolBuilder;
use reducer::*;
use std::error::Error;
use std::io::{self, Write};

// The state of your app.
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,
        }
    }
}

// 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 store = Async::new(Store::new(Calculator(0), Display));

    // Spin up a thread-pool.
    let mut executor = ThreadPoolBuilder::new().create()?;

    // Process incoming actions on a background thread.
    let mut dispatcher = store.spawn(&mut executor).unwrap();

    dispatcher.dispatch(Action::Add(5)); // displays "5"
    dispatcher.dispatch(Action::Mul(3)); // displays "15"
    dispatcher.dispatch(Action::Sub(1)); // displays "14"
    dispatcher.dispatch(Action::Div(7)); // displays "2"

    // Allow the background thread to catch up.
    std::thread::sleep(std::time::Duration::from_millis(500));

    Ok(())
}

Methods

impl<D: Dispatcher<A>, A> Async<D, A>[src]

pub fn new(inner: D) -> Self[src]

Constructs Async given any dispatcher.

impl<D, A> Async<D, A> where
    D: Dispatcher<A> + Send + 'static,
    D::Output: Send + 'static,
    A: Send + 'static, 
[src]

pub fn spawn(
    self,
    executor: &mut impl SpawnExt
) -> Result<AsyncHandle<D, A>, SpawnError>
[src]

Spawns Async onto an Executor and returns an AsyncHandle that may be used to dispatch actions.

The spawned Async will live as long as the handle (or one of its clones) lives.

Trait Implementations

impl<D: Eq + Dispatcher<A>, A: Eq> Eq for Async<D, A>[src]

impl<D: Clone + Dispatcher<A>, A: Clone> Clone for Async<D, A>[src]

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

Performs copy-assignment from source. Read more

impl<D: Copy + Dispatcher<A>, A: Copy> Copy for Async<D, A>[src]

impl<D: PartialEq + Dispatcher<A>, A: PartialEq> PartialEq<Async<D, A>> for Async<D, A>[src]

impl<D: Default + Dispatcher<A>, A: Default> Default for Async<D, A>[src]

impl<D, A> From<D> for Async<D, A> where
    D: Dispatcher<A>, 
[src]

impl<D, A> DerefMut for Async<D, A> where
    D: Dispatcher<A>, 
[src]

impl<D, A> Deref for Async<D, A> where
    D: Dispatcher<A>, 
[src]

type Target = D

The resulting type after dereferencing.

impl<D: Hash + Dispatcher<A>, A: Hash> Hash for Async<D, A>[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<D: Debug + Dispatcher<A>, A: Debug> Debug for Async<D, A>[src]

Auto Trait Implementations

impl<D, A> Send for Async<D, A> where
    A: Send,
    D: Send

impl<D, A> Sync for Async<D, A> where
    A: Sync,
    D: 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]