Trait actions::Component[][src]

pub trait Component<Error: stdError + 'static = ActionsError> {
    type Action: Clone;
    fn apply(
        &mut self,
        action: &Self::Action
    ) -> Result<Option<Self::Action>, Error>; }

Component is a trait that should be implemented for any datatype that describes the state of your application.

Associated Types

The type of the action. The action could be of any type but using an enum is encouraged.

Required Methods

Apply an action and return the inverse of that action.

Arguments

  • action - Data that describes an action.

Example

extern crate actions;
use actions::Component;
use actions::Error;
 
#[derive(Clone)]
struct Counter {
    counter: i32
}

#[derive(Clone)]
enum CounterAction {
    Increment,
    Decrement,
}

impl Component for Counter {
    type Action = CounterAction;

    fn apply(&mut self, action: &Self::Action)->Result<Option<Self::Action>,Error>
    {
        let inverse = match action {
            CounterAction::Increment => {
                self.counter += 1;
                Some(CounterAction::Decrement)
            },
            CounterAction::Decrement => {
                self.counter -= 1;
                Some(CounterAction::Increment)
            }
        };
        Ok(inverse)
    }
}

Implementors