StateMachine

Trait StateMachine 

Source
pub trait StateMachine<S: StateBehavior> {
    // Required methods
    fn current_state(&self) -> S::State;
    fn handle_event(&mut self, event: &S::Event<'_>);
    fn transit(&mut self, new_state: S::State);
    fn force_state(&mut self, new_state: S::State);
}
Expand description

§Trait for the state machine behavior.

This trait is implemented by the rust_sfsm attribute macro and shouldn’t be manually implemented by the user.

It may be used to monomorphize different types implementing the state machine behavior for a given set of states.

§Example

fn test_state_machine<S: StateMachine<States>>(state_machine: &mut S) {
    assert!(state_machine.current_state() == States::Init);

    state_machine.handle_event(&Events::Create);
    assert!(state_machine.current_state() == States::Opened);

    state_machine.handle_event(&Events::Close);
    assert!(state_machine.current_state() == States::Closed);

    state_machine.handle_event(&Events::Lock);
    assert!(state_machine.current_state() == States::Locked);

    state_machine.handle_event(&Events::Unlock);
    assert!(state_machine.current_state() == States::Closed);

    state_machine.handle_event(&Events::Open);
    assert!(state_machine.current_state() == States::Opened);
}

Required Methods§

Source

fn current_state(&self) -> S::State

Get the current state.

Source

fn handle_event(&mut self, event: &S::Event<'_>)

Handle an event and transit if necessary.

Source

fn transit(&mut self, new_state: S::State)

Transit to a new state.

Source

fn force_state(&mut self, new_state: S::State)

Force transition to a new state without calls to respectives enter and exit functions.

Implementors§