pub trait Reactor<S: ?Sized> {
    type Error;
    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.

Associated Types

The type returned if the Reactor fails.

Required methods

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

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 { /* ... */ });

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

Forwards the event to a potentially stack allocated Reactor.

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 { /* ... */ });

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 { /* ... */ });

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 { /* ... */ });

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 { /* ... */ });

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 { /* ... */ });

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 { /* ... */ });

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 { /* ... */ });

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 { /* ... */ });

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 { /* ... */ });

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 { /* ... */ });

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 { /* ... */ });

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 { /* ... */ });

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 { /* ... */ });

Implementors