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§
The type used to represent state that common among all states. Should be a struct.
Required Methods§
Sourcefn on_event(
&mut self,
event: Self::Event,
) -> Result<Vec<Self::Command>, MachineError<Self::Error>>
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.
fn name(&self) -> &str
fn set_state(&mut self, new_state: Self::State)
Returns the current shared state of the machine
Sourcefn has_reached_final_state(&self) -> bool
fn has_reached_final_state(&self) -> bool
Returns true if the machine’s current state is a final one
Sourcefn from_parts(state: Self::State, shared: Self::SharedState) -> Self
fn from_parts(state: Self::State, shared: Self::SharedState) -> Self
Given the shared data and new state, create a new instance.
Sourcefn visualizer() -> &'static str
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.