pub struct StateMachine<S, E>{
pub transitions: HashMap<S, StateMachineTransitions<S, E>>,
/* private fields */
}Expand description
The primitive state machine type that holds all context
- List of available states
- Maps of input events per state. An event not in the list is ignored by default
- Maps of transition functions per input event It also holds the live status of the state machine:
- Queue with incoming input events to be processed
- Current state
Important Note: This type is used to implement the async (and later the sync) versions of the generic state machine You should not use this type for most of use cases. Use the async (or sync) implementations instead!
Fields§
§transitions: HashMap<S, StateMachineTransitions<S, E>>Implementations§
Source§impl<S, E> StateMachine<S, E>
impl<S, E> StateMachine<S, E>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new empty state machine The state machine needs to be confifures properly using:
- add_state()
- add_transition()
- set_state() The above methods can be chained in a single command, like so:
use generic_state_machine::primitives::StateMachine;
let mut fsm = StateMachine::<String, String>::new();
fsm.add_states(&mut vec![
"alpha".to_string(),
"beta".to_string(),
"gamma".to_string(),
])
.add_transition("alpha".to_string(), "beta".to_string(), |_, _| {
"beta".to_string()
})
.initial_state("alpha".to_string());
println!("{:?}", fsm);Sourcepub fn current_state(&self) -> Result<S, Error>
pub fn current_state(&self) -> Result<S, Error>
Get the current state
pub fn initial_state(&mut self, state: S) -> Result<&mut Self, Error>
Sourcepub fn add_states(&mut self, states: &[S]) -> &mut Self
pub fn add_states(&mut self, states: &[S]) -> &mut Self
Add the provided states to the list of available states
Sourcepub fn add_transition(
&mut self,
state: S,
event: E,
function: TransitionFunction<S, E>,
) -> &mut Self
pub fn add_transition( &mut self, state: S, event: E, function: TransitionFunction<S, E>, ) -> &mut Self
Add the provided function as the transition function to be executed if the machine is in the provided state and receives the provided input event
Sourcepub fn execute(&mut self, event: E) -> Result<S, Error>
pub fn execute(&mut self, event: E) -> Result<S, Error>
Executes the received event Checks if a transition function is provided for the current state and for the incoming event It calls the function and sets the state to the output of the transition function. If no transition function is available, the incoming event is dropped.