Skip to main content

Reducer

Trait Reducer 

Source
pub trait Reducer:
    Clone
    + Default
    + Debug
    + 'static {
    type Update: Clone + 'static;

    // Required method
    fn reduce(&mut self, update: Self::Update);
}
Expand description

State, and how it changes.

The type that implements this is the state - there is no type State, because the struct is already there and already named. Two items, both about state; a reducer cannot know who asked, only what changed.

ⓘ
#[derive(Clone, Debug, Default)]
pub struct Processes { pub items: Vec<String> }

pub enum Refreshed { Items(Vec<String>) }

impl Reducer for Processes {
    type Update = Refreshed;

    fn reduce(&mut self, update: Refreshed) {
        match update { Refreshed::Items(items) => self.items = items }
    }
}

Usually it is written as a function instead - #[reducer] fn processes(this: &mut Processes, update: Refreshed) - and the attribute writes this impl.

There is no actor here, and there must not be. A reducer knows its own state and how it changes; naming the actor that happens to drive it would put the domain’s plumbing into the one declaration that is supposed to be free of it. What relates an action to an actor is Action, declared where actions already live.

Clone because a reader is handed the state as it is now, shared, and a change made while a reader still holds it goes to a copy.

Required Associated Types§

Source

type Update: Clone + 'static

What changes it. Clone because an observer is handed the update itself, and reduce consumes it - the copy is made only when something is actually observing.

Required Methods§

Source

fn reduce(&mut self, update: Self::Update)

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§