pub trait Reducer<A> {
    fn reduce(&mut self, action: A);
}
Expand description

Trait for types that represent the logical state of an application.

Perhaps a more accurate mental model for types that implement this trait is that of a state machine, where the nodes correspond to the universe of all possible representable values and the edges correspond to actions.

Required methods

Implements the transition given the current state and an action.

This method is expected to have no side effects and must never fail. In many cases, an effective way to handle illegal state transitions is to make them idempotent, that is to leave the state unchanged.

Example
use reducer::Reducer;

#[derive(Debug)]
struct Todos(Vec<String>);

// Actions
struct Create(String);
struct Remove(usize);

impl Reducer<Create> for Todos {
    fn reduce(&mut self, Create(todo): Create) {
        self.0.push(todo);
    }
}

impl Reducer<Remove> for Todos {
    fn reduce(&mut self, Remove(i): Remove) {
        if i < self.0.len() {
            self.0.remove(i);
        } else {
            // Illegal transition, leave the state unchanged.
        }
    }
}

let mut todos = Todos(vec![]);

todos.reduce(Create("Buy milk".to_string()));
println!("{:?}", todos); // ["Buy milk"]

todos.reduce(Create("Learn Reducer".to_string()));
println!("{:?}", todos); // ["Buy milk", "Learn Reducer"]

todos.reduce(Remove(42)); // out of bounds
println!("{:?}", todos); // ["Buy milk", "Learn Reducer"]

todos.reduce(Remove(0));
println!("{:?}", todos); // ["Learn Reducer"]

Implementations on Foreign Types

Enhances a potentially unsized Reducer with copy-on-write semantics (requires alloc).

Helps avoiding cloning the entire state when it needs to be sent to other threads, e.g to the rendering thread of a GUI.

Example

use reducer::*;
use std::sync::Arc;

#[derive(Clone)]
struct State { /* ... */ }
struct Action { /* ... */ }
struct Actor<T> { states: Vec<T>, /* ... */ }

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

impl<T: Clone> Reactor<T> for Actor<T> {
    type Error = std::convert::Infallible; // TODO: use `!` once it's stable.
    fn react(&mut self, state: &T) -> Result<(), Self::Error> {
        self.states.push(state.clone());
        Ok(())
    }
}

let state = Arc::new(State { /* ... */ });
let reactor = Actor { states: vec![], /* ... */ };
let mut store = Store::new(state, reactor);

store.dispatch(Action { /* ... */ }); // `state` is not cloned yet.

// `reactor` now holds a reference to `state`.

store.dispatch(Action { /* ... */ }); // `state` is cloned through `Arc::make_mut`.

Updates the potentially unsized nested Reducer (requires alloc).

Enhances a potentially unsized Reducer with copy-on-write semantics (requires alloc).

Helps avoiding cloning the entire state when it needs to be sent to other parts of the application.

Example

use reducer::*;
use std::rc::Rc;

#[derive(Clone)]
struct State { /* ... */ }
struct Action { /* ... */ }
struct Actor<T> { states: Vec<T>, /* ... */ }

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

impl<T: Clone> Reactor<T> for Actor<T> {
    type Error = std::convert::Infallible; // TODO: use `!` once it's stable.
    fn react(&mut self, state: &T) -> Result<(), Self::Error> {
        self.states.push(state.clone());
        Ok(())
    }
}

let state = Rc::new(State { /* ... */ });
let reactor = Actor { states: vec![], /* ... */ };
let mut store = Store::new(state, reactor);

store.dispatch(Action { /* ... */ }); // `state` is not cloned yet.

// `reactor` now holds a reference to `state`.

store.dispatch(Action { /* ... */ }); // `state` is cloned through `Rc::make_mut`.

Updates all Reducers in the tuple in order.

Currently implemented for tuples of up to 12 elements.

Example

use reducer::Reducer;

struct ProductDetails { /* ... */ }
struct ShoppingCart { /* ... */ }
struct UserProfile { /* ... */ }

#[derive(Clone)]
enum Action {
    ViewProduct(/* ... */),
    AddToShoppingCart(/* ... */),
    UpdatePaymentPreferences(/* ... */),
    // ...
};

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

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

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

let product = ProductDetails { /* ... */ };
let cart = ShoppingCart { /* ... */ };
let user = UserProfile { /* ... */ };

let mut shop = (product, cart, user);

// `shop` itself implements Reducer<Action>
shop.reduce(Action::AddToShoppingCart( /* ... */ ));

Updates all Reducers in the tuple in order.

Currently implemented for tuples of up to 12 elements.

Example

use reducer::Reducer;

struct ProductDetails { /* ... */ }
struct ShoppingCart { /* ... */ }
struct UserProfile { /* ... */ }

#[derive(Clone)]
enum Action {
    ViewProduct(/* ... */),
    AddToShoppingCart(/* ... */),
    UpdatePaymentPreferences(/* ... */),
    // ...
};

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

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

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

let product = ProductDetails { /* ... */ };
let cart = ShoppingCart { /* ... */ };
let user = UserProfile { /* ... */ };

let mut shop = (product, cart, user);

// `shop` itself implements Reducer<Action>
shop.reduce(Action::AddToShoppingCart( /* ... */ ));

Updates all Reducers in the tuple in order.

Currently implemented for tuples of up to 12 elements.

Example

use reducer::Reducer;

struct ProductDetails { /* ... */ }
struct ShoppingCart { /* ... */ }
struct UserProfile { /* ... */ }

#[derive(Clone)]
enum Action {
    ViewProduct(/* ... */),
    AddToShoppingCart(/* ... */),
    UpdatePaymentPreferences(/* ... */),
    // ...
};

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

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

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

let product = ProductDetails { /* ... */ };
let cart = ShoppingCart { /* ... */ };
let user = UserProfile { /* ... */ };

let mut shop = (product, cart, user);

// `shop` itself implements Reducer<Action>
shop.reduce(Action::AddToShoppingCart( /* ... */ ));

Updates all Reducers in the tuple in order.

Currently implemented for tuples of up to 12 elements.

Example

use reducer::Reducer;

struct ProductDetails { /* ... */ }
struct ShoppingCart { /* ... */ }
struct UserProfile { /* ... */ }

#[derive(Clone)]
enum Action {
    ViewProduct(/* ... */),
    AddToShoppingCart(/* ... */),
    UpdatePaymentPreferences(/* ... */),
    // ...
};

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

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

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

let product = ProductDetails { /* ... */ };
let cart = ShoppingCart { /* ... */ };
let user = UserProfile { /* ... */ };

let mut shop = (product, cart, user);

// `shop` itself implements Reducer<Action>
shop.reduce(Action::AddToShoppingCart( /* ... */ ));

Updates all Reducers in the tuple in order.

Currently implemented for tuples of up to 12 elements.

Example

use reducer::Reducer;

struct ProductDetails { /* ... */ }
struct ShoppingCart { /* ... */ }
struct UserProfile { /* ... */ }

#[derive(Clone)]
enum Action {
    ViewProduct(/* ... */),
    AddToShoppingCart(/* ... */),
    UpdatePaymentPreferences(/* ... */),
    // ...
};

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

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

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

let product = ProductDetails { /* ... */ };
let cart = ShoppingCart { /* ... */ };
let user = UserProfile { /* ... */ };

let mut shop = (product, cart, user);

// `shop` itself implements Reducer<Action>
shop.reduce(Action::AddToShoppingCart( /* ... */ ));

Updates all Reducers in the tuple in order.

Currently implemented for tuples of up to 12 elements.

Example

use reducer::Reducer;

struct ProductDetails { /* ... */ }
struct ShoppingCart { /* ... */ }
struct UserProfile { /* ... */ }

#[derive(Clone)]
enum Action {
    ViewProduct(/* ... */),
    AddToShoppingCart(/* ... */),
    UpdatePaymentPreferences(/* ... */),
    // ...
};

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

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

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

let product = ProductDetails { /* ... */ };
let cart = ShoppingCart { /* ... */ };
let user = UserProfile { /* ... */ };

let mut shop = (product, cart, user);

// `shop` itself implements Reducer<Action>
shop.reduce(Action::AddToShoppingCart( /* ... */ ));

Updates all Reducers in the tuple in order.

Currently implemented for tuples of up to 12 elements.

Example

use reducer::Reducer;

struct ProductDetails { /* ... */ }
struct ShoppingCart { /* ... */ }
struct UserProfile { /* ... */ }

#[derive(Clone)]
enum Action {
    ViewProduct(/* ... */),
    AddToShoppingCart(/* ... */),
    UpdatePaymentPreferences(/* ... */),
    // ...
};

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

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

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

let product = ProductDetails { /* ... */ };
let cart = ShoppingCart { /* ... */ };
let user = UserProfile { /* ... */ };

let mut shop = (product, cart, user);

// `shop` itself implements Reducer<Action>
shop.reduce(Action::AddToShoppingCart( /* ... */ ));

Updates all Reducers in the tuple in order.

Currently implemented for tuples of up to 12 elements.

Example

use reducer::Reducer;

struct ProductDetails { /* ... */ }
struct ShoppingCart { /* ... */ }
struct UserProfile { /* ... */ }

#[derive(Clone)]
enum Action {
    ViewProduct(/* ... */),
    AddToShoppingCart(/* ... */),
    UpdatePaymentPreferences(/* ... */),
    // ...
};

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

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

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

let product = ProductDetails { /* ... */ };
let cart = ShoppingCart { /* ... */ };
let user = UserProfile { /* ... */ };

let mut shop = (product, cart, user);

// `shop` itself implements Reducer<Action>
shop.reduce(Action::AddToShoppingCart( /* ... */ ));

Updates all Reducers in the tuple in order.

Currently implemented for tuples of up to 12 elements.

Example

use reducer::Reducer;

struct ProductDetails { /* ... */ }
struct ShoppingCart { /* ... */ }
struct UserProfile { /* ... */ }

#[derive(Clone)]
enum Action {
    ViewProduct(/* ... */),
    AddToShoppingCart(/* ... */),
    UpdatePaymentPreferences(/* ... */),
    // ...
};

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

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

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

let product = ProductDetails { /* ... */ };
let cart = ShoppingCart { /* ... */ };
let user = UserProfile { /* ... */ };

let mut shop = (product, cart, user);

// `shop` itself implements Reducer<Action>
shop.reduce(Action::AddToShoppingCart( /* ... */ ));

Updates all Reducers in the tuple in order.

Currently implemented for tuples of up to 12 elements.

Example

use reducer::Reducer;

struct ProductDetails { /* ... */ }
struct ShoppingCart { /* ... */ }
struct UserProfile { /* ... */ }

#[derive(Clone)]
enum Action {
    ViewProduct(/* ... */),
    AddToShoppingCart(/* ... */),
    UpdatePaymentPreferences(/* ... */),
    // ...
};

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

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

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

let product = ProductDetails { /* ... */ };
let cart = ShoppingCart { /* ... */ };
let user = UserProfile { /* ... */ };

let mut shop = (product, cart, user);

// `shop` itself implements Reducer<Action>
shop.reduce(Action::AddToShoppingCart( /* ... */ ));

Updates all Reducers in the tuple in order.

Currently implemented for tuples of up to 12 elements.

Example

use reducer::Reducer;

struct ProductDetails { /* ... */ }
struct ShoppingCart { /* ... */ }
struct UserProfile { /* ... */ }

#[derive(Clone)]
enum Action {
    ViewProduct(/* ... */),
    AddToShoppingCart(/* ... */),
    UpdatePaymentPreferences(/* ... */),
    // ...
};

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

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

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

let product = ProductDetails { /* ... */ };
let cart = ShoppingCart { /* ... */ };
let user = UserProfile { /* ... */ };

let mut shop = (product, cart, user);

// `shop` itself implements Reducer<Action>
shop.reduce(Action::AddToShoppingCart( /* ... */ ));

Updates all Reducers in the tuple in order.

Currently implemented for tuples of up to 12 elements.

Example

use reducer::Reducer;

struct ProductDetails { /* ... */ }
struct ShoppingCart { /* ... */ }
struct UserProfile { /* ... */ }

#[derive(Clone)]
enum Action {
    ViewProduct(/* ... */),
    AddToShoppingCart(/* ... */),
    UpdatePaymentPreferences(/* ... */),
    // ...
};

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

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

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

let product = ProductDetails { /* ... */ };
let cart = ShoppingCart { /* ... */ };
let user = UserProfile { /* ... */ };

let mut shop = (product, cart, user);

// `shop` itself implements Reducer<Action>
shop.reduce(Action::AddToShoppingCart( /* ... */ ));

Implementors