pub trait Fsm {
type S;
type C;
type E;
type SE;
// Required methods
fn for_command(s: &Self::S, c: Self::C, se: &mut Self::SE) -> Option<Self::E>;
fn on_event(s: &mut Self::S, e: &Self::E) -> bool;
// Provided methods
fn on_entry(_s: &Self::S, _se: &mut Self::SE) { ... }
fn step(s: &mut Self::S, c: Self::C, se: &mut Self::SE) -> Option<Self::E> { ... }
}Expand description
Describes the behavior of a Finite State Machine (FSM) that can receive commands and produce events. Along the way, effects can be performed given the receipt of a command. State can be reconsituted by replaying events.
Note that effects are represented by a separate structure so that they can be consolidated, and also to help structure the code. Further, it is possible to have multiple implementations of effects e.g. different ones when testing.
Effects are also synchronous. If an effect handler must communicate, say, with a task in a
non-blocking fashion, then the state machine should represent this intermediate state. For
example, a channel could be used to communicate with such a task with try_send being used
and then causing a state transition in relation to that result. While this approach adds
steps to a state machine, it does allow them to remain responsive to receiving more
commands.
Required Associated Types§
Required Methods§
Provided Methods§
sourcefn on_entry(_s: &Self::S, _se: &mut Self::SE)
fn on_entry(_s: &Self::S, _se: &mut Self::SE)
Optional effect on entering a state i.e. transitioning in to state S from
another.
sourcefn step(s: &mut Self::S, c: Self::C, se: &mut Self::SE) -> Option<Self::E>
fn step(s: &mut Self::S, c: Self::C, se: &mut Self::SE) -> Option<Self::E>
This is the main entry point to the event driven FSM. Runs the state machine for a command, optionally performing effects, possibly producing an event and possibly transitioning to a new state. Also applies any “Entry/” processing when arriving at a new state.