StateMachine

Trait StateMachine 

Source
pub trait StateMachine: Sized {
    type Error: Error;
    type State;
    type SharedState;
    type Event;
    type Command;

    // Required methods
    fn on_event(
        &mut self,
        event: Self::Event,
    ) -> Result<Vec<Self::Command>, MachineError<Self::Error>>;
    fn name(&self) -> &str;
    fn state(&self) -> &Self::State;
    fn set_state(&mut self, new_state: Self::State);
    fn shared_state(&self) -> &Self::SharedState;
    fn has_reached_final_state(&self) -> bool;
    fn from_parts(state: Self::State, shared: Self::SharedState) -> Self;
    fn visualizer() -> &'static str;
}
Expand description

This trait defines a state machine (more formally, a finite state transducer) which accepts events (the input alphabet), uses them to mutate itself, and (may) output some commands (the output alphabet) as a result.

Required Associated Types§

Source

type Error: Error

The error type produced by this state machine when handling events

Source

type State

The type used to represent different machine states. Should be an enum.

Source

type SharedState

The type used to represent state that common among all states. Should be a struct.

Source

type Event

The type used to represent events the machine handles. Should be an enum.

Source

type Command

The type used to represent commands the machine issues upon transitions.

Required Methods§

Source

fn on_event( &mut self, event: Self::Event, ) -> Result<Vec<Self::Command>, MachineError<Self::Error>>

Handle an incoming event, returning any new commands or an error. Implementations may mutate current state, possibly moving to a new state.

Source

fn name(&self) -> &str

Source

fn state(&self) -> &Self::State

Returns the current state of the machine

Source

fn set_state(&mut self, new_state: Self::State)

Source

fn shared_state(&self) -> &Self::SharedState

Returns the current shared state of the machine

Source

fn has_reached_final_state(&self) -> bool

Returns true if the machine’s current state is a final one

Source

fn from_parts(state: Self::State, shared: Self::SharedState) -> Self

Given the shared data and new state, create a new instance.

Source

fn visualizer() -> &'static str

Return a PlantUML definition of the fsm that can be used to visualize it

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§