Reactor

Trait Reactor 

Source
pub trait Reactor<S: ?Sized> {
    type Error;

    // Required method
    fn react(&mut self, state: &S) -> Result<(), Self::Error>;
}
Expand description

Trait for types that react to state transitions.

Reactors connect the state to the view components. They can implement arbitrary logic in response to state transitions, but it’s often better to think of Reactors as channels that transmit the current state to other parts of your application.

Required Associated Types§

Source

type Error

The type returned if the Reactor fails.

Required Methods§

Source

fn react(&mut self, state: &S) -> Result<(), Self::Error>

Reacts to an update to S.

§Example
use reducer::*;
use std::fmt::Debug;
use std::io::{self, Write};

struct Console;

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

Implementations on Foreign Types§

Source§

impl<'a, S, T> Reactor<S> for &'a mut T
where S: ?Sized, T: Reactor<S> + ?Sized,

Forwards the event to a potentially stack allocated Reactor.

Source§

type Error = <T as Reactor<S>>::Error

Source§

fn react(&mut self, state: &S) -> Result<(), Self::Error>

Source§

impl<S, T> Reactor<S> for [T]
where S: ?Sized, T: Reactor<S>,

Notifies all Reactors in the slice in order.

§Example

use reducer::*;

struct State { /* ... */ }
struct Action { /* ... */ }

impl Reducer<Action> for State {
    fn reduce(&mut self, action: Action) {
        // ...
    }
}

struct Actor { /* ... */ }
struct ActorError(/*...*/);

impl Reactor<State> for Actor {
    type Error = ActorError;
    fn react(&mut self, state: &State) -> Result<(), Self::Error> {
        // ...
        Ok(())
    }
}

let mut actors = vec![];

actors.push(Actor { /* ... */ });
actors.push(Actor { /* ... */ });
// ...
actors.push(Actor { /* ... */ });

let mut store = Store::new(State { /* ... */ }, actors.into_boxed_slice());

// All actors get notified of state changes.
store.dispatch(Action { /* ... */ });
Source§

type Error = <T as Reactor<S>>::Error

Source§

fn react(&mut self, state: &S) -> Result<(), Self::Error>

Source§

impl<S, T> Reactor<S> for Box<T>
where S: ?Sized, T: Reactor<S> + ?Sized,

Forwards the event to the potentially unsized nested Reactor (requires alloc).

Source§

type Error = <T as Reactor<S>>::Error

Source§

fn react(&mut self, state: &S) -> Result<(), Self::Error>

Source§

impl<S, T, const N: usize> Reactor<S> for [T; N]
where S: ?Sized, T: Reactor<S>,

Notifies all Reactors in the array in order.

Currently implemented for arrays of up to 32 elements.

§Example

use reducer::*;

struct State { /* ... */ }
struct Action { /* ... */ }

impl Reducer<Action> for State {
    fn reduce(&mut self, action: Action) {
        // ...
    }
}

struct Actor { /* ... */ }
struct ActorError(/*...*/);

impl Reactor<State> for Actor {
    type Error = ActorError;
    fn react(&mut self, state: &State) -> Result<(), Self::Error> {
        // ...
        Ok(())
    }
}

let a = Actor { /* ... */ };
let b = Actor { /* ... */ };
// ...
let z = Actor { /* ... */ };

let mut store = Store::new(State { /* ... */ }, [a, b, /* ..., */ z]);

// All actors get notified of state changes.
store.dispatch(Action { /* ... */ });
Source§

type Error = <T as Reactor<S>>::Error

Source§

fn react(&mut self, state: &S) -> Result<(), Self::Error>

Source§

impl<S, X, A> Reactor<S> for (A,)
where S: ?Sized, A: Reactor<S, Error = X>,

Notifies all Reactors in the tuple in order.

Currently implemented for tuples of up to 12 elements.

§Example

use reducer::*;
use std::error::Error;

#[derive(Debug)]
struct State { /* ... */ }
struct Action { /* ... */ }

impl Reducer<Action> for State {
    fn reduce(&mut self, action: Action) {
        // ...
    }
}

struct GUI { /* ... */ }
struct DebugLogger { /* ... */ }

impl Reactor<State> for GUI {
    type Error = Box<dyn Error>;
    fn react(&mut self, state: &State) -> Result<(), Self::Error> {
        // ...
        Ok(())
    }
}

impl Reactor<State> for DebugLogger {
    type Error = Box<dyn Error>;
    fn react(&mut self, state: &State) -> Result<(), Self::Error> {
        println!("[DEBUG] {:#?}", state);
        Ok(())
    }
}

let gui = GUI { /* ... */ };
let logger = DebugLogger { /* ... */ };

let mut store = Store::new(State { /* ... */ }, (gui, logger));

// Both `gui` and `logger` get notified of state changes.
store.dispatch(Action { /* ... */ });
Source§

type Error = X

Source§

fn react(&mut self, state: &S) -> Result<(), Self::Error>

Source§

impl<S, X, A, B> Reactor<S> for (A, B)
where S: ?Sized, A: Reactor<S, Error = X>, B: Reactor<S, Error = X>,

Notifies all Reactors in the tuple in order.

Currently implemented for tuples of up to 12 elements.

§Example

use reducer::*;
use std::error::Error;

#[derive(Debug)]
struct State { /* ... */ }
struct Action { /* ... */ }

impl Reducer<Action> for State {
    fn reduce(&mut self, action: Action) {
        // ...
    }
}

struct GUI { /* ... */ }
struct DebugLogger { /* ... */ }

impl Reactor<State> for GUI {
    type Error = Box<dyn Error>;
    fn react(&mut self, state: &State) -> Result<(), Self::Error> {
        // ...
        Ok(())
    }
}

impl Reactor<State> for DebugLogger {
    type Error = Box<dyn Error>;
    fn react(&mut self, state: &State) -> Result<(), Self::Error> {
        println!("[DEBUG] {:#?}", state);
        Ok(())
    }
}

let gui = GUI { /* ... */ };
let logger = DebugLogger { /* ... */ };

let mut store = Store::new(State { /* ... */ }, (gui, logger));

// Both `gui` and `logger` get notified of state changes.
store.dispatch(Action { /* ... */ });
Source§

type Error = X

Source§

fn react(&mut self, state: &S) -> Result<(), Self::Error>

Source§

impl<S, X, A, B, C> Reactor<S> for (A, B, C)
where S: ?Sized, A: Reactor<S, Error = X>, B: Reactor<S, Error = X>, C: Reactor<S, Error = X>,

Notifies all Reactors in the tuple in order.

Currently implemented for tuples of up to 12 elements.

§Example

use reducer::*;
use std::error::Error;

#[derive(Debug)]
struct State { /* ... */ }
struct Action { /* ... */ }

impl Reducer<Action> for State {
    fn reduce(&mut self, action: Action) {
        // ...
    }
}

struct GUI { /* ... */ }
struct DebugLogger { /* ... */ }

impl Reactor<State> for GUI {
    type Error = Box<dyn Error>;
    fn react(&mut self, state: &State) -> Result<(), Self::Error> {
        // ...
        Ok(())
    }
}

impl Reactor<State> for DebugLogger {
    type Error = Box<dyn Error>;
    fn react(&mut self, state: &State) -> Result<(), Self::Error> {
        println!("[DEBUG] {:#?}", state);
        Ok(())
    }
}

let gui = GUI { /* ... */ };
let logger = DebugLogger { /* ... */ };

let mut store = Store::new(State { /* ... */ }, (gui, logger));

// Both `gui` and `logger` get notified of state changes.
store.dispatch(Action { /* ... */ });
Source§

type Error = X

Source§

fn react(&mut self, state: &S) -> Result<(), Self::Error>

Source§

impl<S, X, A, B, C, D> Reactor<S> for (A, B, C, D)
where S: ?Sized, A: Reactor<S, Error = X>, B: Reactor<S, Error = X>, C: Reactor<S, Error = X>, D: Reactor<S, Error = X>,

Notifies all Reactors in the tuple in order.

Currently implemented for tuples of up to 12 elements.

§Example

use reducer::*;
use std::error::Error;

#[derive(Debug)]
struct State { /* ... */ }
struct Action { /* ... */ }

impl Reducer<Action> for State {
    fn reduce(&mut self, action: Action) {
        // ...
    }
}

struct GUI { /* ... */ }
struct DebugLogger { /* ... */ }

impl Reactor<State> for GUI {
    type Error = Box<dyn Error>;
    fn react(&mut self, state: &State) -> Result<(), Self::Error> {
        // ...
        Ok(())
    }
}

impl Reactor<State> for DebugLogger {
    type Error = Box<dyn Error>;
    fn react(&mut self, state: &State) -> Result<(), Self::Error> {
        println!("[DEBUG] {:#?}", state);
        Ok(())
    }
}

let gui = GUI { /* ... */ };
let logger = DebugLogger { /* ... */ };

let mut store = Store::new(State { /* ... */ }, (gui, logger));

// Both `gui` and `logger` get notified of state changes.
store.dispatch(Action { /* ... */ });
Source§

type Error = X

Source§

fn react(&mut self, state: &S) -> Result<(), Self::Error>

Source§

impl<S, X, A, B, C, D, E> Reactor<S> for (A, B, C, D, E)
where S: ?Sized, A: Reactor<S, Error = X>, B: Reactor<S, Error = X>, C: Reactor<S, Error = X>, D: Reactor<S, Error = X>, E: Reactor<S, Error = X>,

Notifies all Reactors in the tuple in order.

Currently implemented for tuples of up to 12 elements.

§Example

use reducer::*;
use std::error::Error;

#[derive(Debug)]
struct State { /* ... */ }
struct Action { /* ... */ }

impl Reducer<Action> for State {
    fn reduce(&mut self, action: Action) {
        // ...
    }
}

struct GUI { /* ... */ }
struct DebugLogger { /* ... */ }

impl Reactor<State> for GUI {
    type Error = Box<dyn Error>;
    fn react(&mut self, state: &State) -> Result<(), Self::Error> {
        // ...
        Ok(())
    }
}

impl Reactor<State> for DebugLogger {
    type Error = Box<dyn Error>;
    fn react(&mut self, state: &State) -> Result<(), Self::Error> {
        println!("[DEBUG] {:#?}", state);
        Ok(())
    }
}

let gui = GUI { /* ... */ };
let logger = DebugLogger { /* ... */ };

let mut store = Store::new(State { /* ... */ }, (gui, logger));

// Both `gui` and `logger` get notified of state changes.
store.dispatch(Action { /* ... */ });
Source§

type Error = X

Source§

fn react(&mut self, state: &S) -> Result<(), Self::Error>

Source§

impl<S, X, A, B, C, D, E, F> Reactor<S> for (A, B, C, D, E, F)
where S: ?Sized, A: Reactor<S, Error = X>, B: Reactor<S, Error = X>, C: Reactor<S, Error = X>, D: Reactor<S, Error = X>, E: Reactor<S, Error = X>, F: Reactor<S, Error = X>,

Notifies all Reactors in the tuple in order.

Currently implemented for tuples of up to 12 elements.

§Example

use reducer::*;
use std::error::Error;

#[derive(Debug)]
struct State { /* ... */ }
struct Action { /* ... */ }

impl Reducer<Action> for State {
    fn reduce(&mut self, action: Action) {
        // ...
    }
}

struct GUI { /* ... */ }
struct DebugLogger { /* ... */ }

impl Reactor<State> for GUI {
    type Error = Box<dyn Error>;
    fn react(&mut self, state: &State) -> Result<(), Self::Error> {
        // ...
        Ok(())
    }
}

impl Reactor<State> for DebugLogger {
    type Error = Box<dyn Error>;
    fn react(&mut self, state: &State) -> Result<(), Self::Error> {
        println!("[DEBUG] {:#?}", state);
        Ok(())
    }
}

let gui = GUI { /* ... */ };
let logger = DebugLogger { /* ... */ };

let mut store = Store::new(State { /* ... */ }, (gui, logger));

// Both `gui` and `logger` get notified of state changes.
store.dispatch(Action { /* ... */ });
Source§

type Error = X

Source§

fn react(&mut self, state: &S) -> Result<(), Self::Error>

Source§

impl<S, X, A, B, C, D, E, F, G> Reactor<S> for (A, B, C, D, E, F, G)
where S: ?Sized, A: Reactor<S, Error = X>, B: Reactor<S, Error = X>, C: Reactor<S, Error = X>, D: Reactor<S, Error = X>, E: Reactor<S, Error = X>, F: Reactor<S, Error = X>, G: Reactor<S, Error = X>,

Notifies all Reactors in the tuple in order.

Currently implemented for tuples of up to 12 elements.

§Example

use reducer::*;
use std::error::Error;

#[derive(Debug)]
struct State { /* ... */ }
struct Action { /* ... */ }

impl Reducer<Action> for State {
    fn reduce(&mut self, action: Action) {
        // ...
    }
}

struct GUI { /* ... */ }
struct DebugLogger { /* ... */ }

impl Reactor<State> for GUI {
    type Error = Box<dyn Error>;
    fn react(&mut self, state: &State) -> Result<(), Self::Error> {
        // ...
        Ok(())
    }
}

impl Reactor<State> for DebugLogger {
    type Error = Box<dyn Error>;
    fn react(&mut self, state: &State) -> Result<(), Self::Error> {
        println!("[DEBUG] {:#?}", state);
        Ok(())
    }
}

let gui = GUI { /* ... */ };
let logger = DebugLogger { /* ... */ };

let mut store = Store::new(State { /* ... */ }, (gui, logger));

// Both `gui` and `logger` get notified of state changes.
store.dispatch(Action { /* ... */ });
Source§

type Error = X

Source§

fn react(&mut self, state: &S) -> Result<(), Self::Error>

Source§

impl<S, X, A, B, C, D, E, F, G, H> Reactor<S> for (A, B, C, D, E, F, G, H)
where S: ?Sized, A: Reactor<S, Error = X>, B: Reactor<S, Error = X>, C: Reactor<S, Error = X>, D: Reactor<S, Error = X>, E: Reactor<S, Error = X>, F: Reactor<S, Error = X>, G: Reactor<S, Error = X>, H: Reactor<S, Error = X>,

Notifies all Reactors in the tuple in order.

Currently implemented for tuples of up to 12 elements.

§Example

use reducer::*;
use std::error::Error;

#[derive(Debug)]
struct State { /* ... */ }
struct Action { /* ... */ }

impl Reducer<Action> for State {
    fn reduce(&mut self, action: Action) {
        // ...
    }
}

struct GUI { /* ... */ }
struct DebugLogger { /* ... */ }

impl Reactor<State> for GUI {
    type Error = Box<dyn Error>;
    fn react(&mut self, state: &State) -> Result<(), Self::Error> {
        // ...
        Ok(())
    }
}

impl Reactor<State> for DebugLogger {
    type Error = Box<dyn Error>;
    fn react(&mut self, state: &State) -> Result<(), Self::Error> {
        println!("[DEBUG] {:#?}", state);
        Ok(())
    }
}

let gui = GUI { /* ... */ };
let logger = DebugLogger { /* ... */ };

let mut store = Store::new(State { /* ... */ }, (gui, logger));

// Both `gui` and `logger` get notified of state changes.
store.dispatch(Action { /* ... */ });
Source§

type Error = X

Source§

fn react(&mut self, state: &S) -> Result<(), Self::Error>

Source§

impl<S, X, A, B, C, D, E, F, G, H, I> Reactor<S> for (A, B, C, D, E, F, G, H, I)
where S: ?Sized, A: Reactor<S, Error = X>, B: Reactor<S, Error = X>, C: Reactor<S, Error = X>, D: Reactor<S, Error = X>, E: Reactor<S, Error = X>, F: Reactor<S, Error = X>, G: Reactor<S, Error = X>, H: Reactor<S, Error = X>, I: Reactor<S, Error = X>,

Notifies all Reactors in the tuple in order.

Currently implemented for tuples of up to 12 elements.

§Example

use reducer::*;
use std::error::Error;

#[derive(Debug)]
struct State { /* ... */ }
struct Action { /* ... */ }

impl Reducer<Action> for State {
    fn reduce(&mut self, action: Action) {
        // ...
    }
}

struct GUI { /* ... */ }
struct DebugLogger { /* ... */ }

impl Reactor<State> for GUI {
    type Error = Box<dyn Error>;
    fn react(&mut self, state: &State) -> Result<(), Self::Error> {
        // ...
        Ok(())
    }
}

impl Reactor<State> for DebugLogger {
    type Error = Box<dyn Error>;
    fn react(&mut self, state: &State) -> Result<(), Self::Error> {
        println!("[DEBUG] {:#?}", state);
        Ok(())
    }
}

let gui = GUI { /* ... */ };
let logger = DebugLogger { /* ... */ };

let mut store = Store::new(State { /* ... */ }, (gui, logger));

// Both `gui` and `logger` get notified of state changes.
store.dispatch(Action { /* ... */ });
Source§

type Error = X

Source§

fn react(&mut self, state: &S) -> Result<(), Self::Error>

Source§

impl<S, X, A, B, C, D, E, F, G, H, I, J> Reactor<S> for (A, B, C, D, E, F, G, H, I, J)
where S: ?Sized, A: Reactor<S, Error = X>, B: Reactor<S, Error = X>, C: Reactor<S, Error = X>, D: Reactor<S, Error = X>, E: Reactor<S, Error = X>, F: Reactor<S, Error = X>, G: Reactor<S, Error = X>, H: Reactor<S, Error = X>, I: Reactor<S, Error = X>, J: Reactor<S, Error = X>,

Notifies all Reactors in the tuple in order.

Currently implemented for tuples of up to 12 elements.

§Example

use reducer::*;
use std::error::Error;

#[derive(Debug)]
struct State { /* ... */ }
struct Action { /* ... */ }

impl Reducer<Action> for State {
    fn reduce(&mut self, action: Action) {
        // ...
    }
}

struct GUI { /* ... */ }
struct DebugLogger { /* ... */ }

impl Reactor<State> for GUI {
    type Error = Box<dyn Error>;
    fn react(&mut self, state: &State) -> Result<(), Self::Error> {
        // ...
        Ok(())
    }
}

impl Reactor<State> for DebugLogger {
    type Error = Box<dyn Error>;
    fn react(&mut self, state: &State) -> Result<(), Self::Error> {
        println!("[DEBUG] {:#?}", state);
        Ok(())
    }
}

let gui = GUI { /* ... */ };
let logger = DebugLogger { /* ... */ };

let mut store = Store::new(State { /* ... */ }, (gui, logger));

// Both `gui` and `logger` get notified of state changes.
store.dispatch(Action { /* ... */ });
Source§

type Error = X

Source§

fn react(&mut self, state: &S) -> Result<(), Self::Error>

Source§

impl<S, X, A, B, C, D, E, F, G, H, I, J, K> Reactor<S> for (A, B, C, D, E, F, G, H, I, J, K)
where S: ?Sized, A: Reactor<S, Error = X>, B: Reactor<S, Error = X>, C: Reactor<S, Error = X>, D: Reactor<S, Error = X>, E: Reactor<S, Error = X>, F: Reactor<S, Error = X>, G: Reactor<S, Error = X>, H: Reactor<S, Error = X>, I: Reactor<S, Error = X>, J: Reactor<S, Error = X>, K: Reactor<S, Error = X>,

Notifies all Reactors in the tuple in order.

Currently implemented for tuples of up to 12 elements.

§Example

use reducer::*;
use std::error::Error;

#[derive(Debug)]
struct State { /* ... */ }
struct Action { /* ... */ }

impl Reducer<Action> for State {
    fn reduce(&mut self, action: Action) {
        // ...
    }
}

struct GUI { /* ... */ }
struct DebugLogger { /* ... */ }

impl Reactor<State> for GUI {
    type Error = Box<dyn Error>;
    fn react(&mut self, state: &State) -> Result<(), Self::Error> {
        // ...
        Ok(())
    }
}

impl Reactor<State> for DebugLogger {
    type Error = Box<dyn Error>;
    fn react(&mut self, state: &State) -> Result<(), Self::Error> {
        println!("[DEBUG] {:#?}", state);
        Ok(())
    }
}

let gui = GUI { /* ... */ };
let logger = DebugLogger { /* ... */ };

let mut store = Store::new(State { /* ... */ }, (gui, logger));

// Both `gui` and `logger` get notified of state changes.
store.dispatch(Action { /* ... */ });
Source§

type Error = X

Source§

fn react(&mut self, state: &S) -> Result<(), Self::Error>

Source§

impl<S, X, A, B, C, D, E, F, G, H, I, J, K, L> Reactor<S> for (A, B, C, D, E, F, G, H, I, J, K, L)
where S: ?Sized, A: Reactor<S, Error = X>, B: Reactor<S, Error = X>, C: Reactor<S, Error = X>, D: Reactor<S, Error = X>, E: Reactor<S, Error = X>, F: Reactor<S, Error = X>, G: Reactor<S, Error = X>, H: Reactor<S, Error = X>, I: Reactor<S, Error = X>, J: Reactor<S, Error = X>, K: Reactor<S, Error = X>, L: Reactor<S, Error = X>,

Notifies all Reactors in the tuple in order.

Currently implemented for tuples of up to 12 elements.

§Example

use reducer::*;
use std::error::Error;

#[derive(Debug)]
struct State { /* ... */ }
struct Action { /* ... */ }

impl Reducer<Action> for State {
    fn reduce(&mut self, action: Action) {
        // ...
    }
}

struct GUI { /* ... */ }
struct DebugLogger { /* ... */ }

impl Reactor<State> for GUI {
    type Error = Box<dyn Error>;
    fn react(&mut self, state: &State) -> Result<(), Self::Error> {
        // ...
        Ok(())
    }
}

impl Reactor<State> for DebugLogger {
    type Error = Box<dyn Error>;
    fn react(&mut self, state: &State) -> Result<(), Self::Error> {
        println!("[DEBUG] {:#?}", state);
        Ok(())
    }
}

let gui = GUI { /* ... */ };
let logger = DebugLogger { /* ... */ };

let mut store = Store::new(State { /* ... */ }, (gui, logger));

// Both `gui` and `logger` get notified of state changes.
store.dispatch(Action { /* ... */ });
Source§

type Error = X

Source§

fn react(&mut self, state: &S) -> Result<(), Self::Error>

Implementors§

Source§

impl<S, T, E> Reactor<S> for AsyncReactor<T>
where S: ?Sized, Self: for<'s> Sink<&'s S, Error = E> + Unpin,

Source§

type Error = E