pub struct StateMachine<S: States, E: Events, const NS: usize, const NE: usize> { /* private fields */ }Expand description
A deterministic finite automaton (DFA) state machine.
The state machine stores a transition matrix and tracks the current state.
All transitions are defined at compile time through the const generics NS (number of states)
and NE (number of events).
§Type Parameters
S: The state type implementingStatesE: The event type implementingEventsNS: The total number of states (const generic)NE: The total number of events (const generic)
§Example
use tinystate::StateMachineBuilder;
let mut sm = StateMachineBuilder::<MyState, MyEvent, 2, 1>::new()
.initial(MyState::A)
.transition(MyState::A, MyEvent::X, MyState::B)
.transition(MyState::B, MyEvent::X, MyState::A)
.build()
.unwrap();Implementations§
Source§impl<S: States, E: Events, const NS: usize, const NE: usize> StateMachine<S, E, NS, NE>
impl<S: States, E: Events, const NS: usize, const NE: usize> StateMachine<S, E, NS, NE>
Sourcepub fn trigger(&mut self, event: E)
pub fn trigger(&mut self, event: E)
Triggers a state transition based on the given event.
This unconditionally transitions to the next state defined in the transition matrix.
§Example
let mut sm = StateMachineBuilder::<S, E, 2, 1>::new()
.initial(S::A)
.transition(S::A, E::X, S::B)
.transition(S::B, E::X, S::A)
.build()
.unwrap();
sm.trigger(E::X);
assert_eq!(sm.current(), &S::B);Sourcepub fn can_trigger(&self, event: E) -> bool
pub fn can_trigger(&self, event: E) -> bool
Checks if an event would cause a state change.
Returns true if triggering the event would transition to a different state,
false if it would remain in the current state (self-loop).
§Example
let sm = StateMachineBuilder::<S, E, 2, 1>::new()
.initial(S::A)
.transition(S::A, E::X, S::B)
.self_loop(S::B, E::X)
.build()
.unwrap();
assert!(sm.can_trigger(E::X)); // Would transition A -> BSourcepub fn trigger_if<F>(&mut self, event: E, condition: F) -> bool
pub fn trigger_if<F>(&mut self, event: E, condition: F) -> bool
Conditionally triggers a state transition.
Only transitions if the provided condition returns true for the current state.
Returns true if the transition occurred, false otherwise.
§Example
let mut sm = StateMachineBuilder::<S, E, 2, 1>::new()
.initial(S::A)
.transition(S::A, E::X, S::B)
.transition(S::B, E::X, S::A)
.build()
.unwrap();
let transitioned = sm.trigger_if(E::X, |s| matches!(s, S::A));
assert!(transitioned);
assert_eq!(sm.current(), &S::B);Sourcepub fn try_trigger(&mut self, event: E) -> Result<S, S>
pub fn try_trigger(&mut self, event: E) -> Result<S, S>
Attempts to trigger a transition, returning the result.
Returns Ok(new_state) if the state changed, or Err(old_state) if it remained the same.
§Errors
Returns Err(old_state) if the event triggered a self-loop (state did not change).
§Example
let mut sm = StateMachineBuilder::<S, E, 2, 1>::new()
.initial(S::A)
.transition(S::A, E::X, S::B)
.self_loop(S::B, E::X)
.build()
.unwrap();
assert_eq!(sm.try_trigger(E::X), Ok(S::B));
assert_eq!(sm.try_trigger(E::X), Err(S::B)); // Self-loopSourcepub fn next_state(&self, event: E) -> S
pub fn next_state(&self, event: E) -> S
Returns the next state without transitioning.
Useful for previewing what state an event would lead to.
§Example
let sm = StateMachineBuilder::<S, E, 2, 1>::new()
.initial(S::A)
.transition(S::A, E::X, S::B)
.transition(S::B, E::X, S::A)
.build()
.unwrap();
assert_eq!(sm.next_state(E::X), S::B);