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§
Required Methods§
Sourcefn react(&mut self, state: &S) -> Result<(), Self::Error>
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
Forwards the event to a potentially stack allocated Reactor.
impl<'a, S, T> Reactor<S> for &'a mut T
Forwards the event to a potentially stack allocated Reactor.
Source§impl<S, T> Reactor<S> for [T]
Notifies all Reactors in the slice in order.
impl<S, T> Reactor<S> for [T]
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§impl<S, T, const N: usize> Reactor<S> for [T; N]
Notifies all Reactors in the array in order.
impl<S, T, const N: usize> Reactor<S> for [T; N]
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§impl<S, X, A> Reactor<S> for (A,)
Notifies all Reactors in the tuple in order.
impl<S, X, A> Reactor<S> for (A,)
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§impl<S, X, A, B> Reactor<S> for (A, B)
Notifies all Reactors in the tuple in order.
impl<S, X, A, B> Reactor<S> for (A, B)
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§impl<S, X, A, B, C> Reactor<S> for (A, B, C)
Notifies all Reactors in the tuple in order.
impl<S, X, A, B, C> Reactor<S> for (A, B, C)
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§impl<S, X, A, B, C, D> Reactor<S> for (A, B, C, D)
Notifies all Reactors in the tuple in order.
impl<S, X, A, B, C, D> Reactor<S> for (A, B, C, D)
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§impl<S, X, A, B, C, D, E> Reactor<S> for (A, B, C, D, E)
Notifies all Reactors in the tuple in order.
impl<S, X, A, B, C, D, E> Reactor<S> for (A, B, C, D, E)
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§impl<S, X, A, B, C, D, E, F> Reactor<S> for (A, B, C, D, E, F)
Notifies all Reactors in the tuple in order.
impl<S, X, A, B, C, D, E, F> Reactor<S> for (A, B, C, D, E, F)
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§impl<S, X, A, B, C, D, E, F, G> Reactor<S> for (A, B, C, D, E, F, G)
Notifies all Reactors in the tuple in order.
impl<S, X, A, B, C, D, E, F, G> Reactor<S> for (A, B, C, D, E, F, G)
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§impl<S, X, A, B, C, D, E, F, G, H> Reactor<S> for (A, B, C, D, E, F, G, H)
Notifies all Reactors in the tuple in order.
impl<S, X, A, B, C, D, E, F, G, H> Reactor<S> for (A, B, C, D, E, F, G, H)
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§impl<S, X, A, B, C, D, E, F, G, H, I> Reactor<S> for (A, B, C, D, E, F, G, H, I)
Notifies all Reactors in the tuple in order.
impl<S, X, A, B, C, D, E, F, G, H, I> Reactor<S> for (A, B, C, D, E, F, G, H, I)
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§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)
Notifies all Reactors in the tuple in order.
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)
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§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.
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§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.
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 { /* ... */ });