Fsm

Trait Fsm 

Source
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) -> Option<Change>;
    fn on_change(s: &Self::S, e: &Self::E, se: &mut Self::SE, change: Change);

    // Provided method
    fn step(
        s: &mut Self::S,
        i: Input<Self::C, Self::E>,
        se: &mut Self::SE,
    ) -> Option<Self::E> { ... }
}
Expand description

Runs the state machine for a command or event, optionally performing effects, events, or receive events. These types of FSM can be broadly described as “Mealy” and “Moore” machines respectively. Along the way, effects can be performed given the receipt of a command or the application of an event. 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§

Source

type S

The state managed by the FSM

Source

type C

The command(s) that are able to be processed by the FSM

Source

type E

The event emitted having performed a command

Source

type SE

The side effect handler

Required Methods§

Source

fn for_command(s: &Self::S, c: Self::C, se: &mut Self::SE) -> Option<Self::E>

Given a state and command, optionally emit an event if it applies. Can perform side effects. This function is generally only called from the step function.

Source

fn on_event(s: &mut Self::S, e: &Self::E) -> Option<Change>

Given a state and event, modify state, which could indicate transition to the next state. No side effects are to be performed. Can be used to replay events to attain a new state i.e. the major function of event sourcing. Returns some enumeration of the Change type if there is a state transition.

Source

fn on_change(s: &Self::S, e: &Self::E, se: &mut Self::SE, change: Change)

Given a state and event having been applied then handle any potential change and optionally perform side effects. This function is generally only called from the step function.

Provided Methods§

Source

fn step( s: &mut Self::S, i: Input<Self::C, Self::E>, se: &mut Self::SE, ) -> Option<Self::E>

This is the common entry point to the event driven FSM. Runs the state machine for a command input, 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, and a change handler if there is a state change.

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§