pub struct StateMachineBuilder<S: States, E: Events, const NS: usize, const NE: usize> { /* private fields */ }Expand description
Builder for constructing and validating state machines.
The builder pattern ensures all transitions are defined before the state machine is created. Validation occurs at build time to catch configuration errors early.
§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 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();Implementations§
Source§impl<S: States, E: Events, const NS: usize, const NE: usize> StateMachineBuilder<S, E, NS, NE>
impl<S: States, E: Events, const NS: usize, const NE: usize> StateMachineBuilder<S, E, NS, NE>
Sourcepub const fn initial(self, state: S) -> Self
pub const fn initial(self, state: S) -> Self
Sets the initial state of the state machine.
This must be called before building the state machine.
§Example
let builder = StateMachineBuilder::<S, E, 2, 1>::new()
.initial(S::A);Sourcepub fn transition(self, from: S, event: E, to: S) -> Self
pub fn transition(self, from: S, event: E, to: S) -> Self
Adds a single state transition.
Defines that when in from state and event occurs, transition to to state.
§Example
let builder = StateMachineBuilder::<S, E, 2, 1>::new()
.transition(S::A, E::X, S::B);Sourcepub fn transitions_from(self, from: S, transitions: &[(E, S)]) -> Self
pub fn transitions_from(self, from: S, transitions: &[(E, S)]) -> Self
Adds multiple transitions from the same source state.
Convenient for defining all outgoing transitions from a state at once.
§Example
let builder = StateMachineBuilder::<S, E, 3, 2>::new()
.transitions_from(S::A, &[(E::X, S::B), (E::Y, S::C)]);Sourcepub fn self_loop(self, state: S, event: E) -> Self
pub fn self_loop(self, state: S, event: E) -> Self
Adds a self-loop transition where a state transitions back to itself.
§Example
let builder = StateMachineBuilder::<S, E, 1, 1>::new()
.self_loop(S::A, E::X);Sourcepub fn build(self) -> Result<StateMachine<S, E, NS, NE>, ValidationError<S, E>>
pub fn build(self) -> Result<StateMachine<S, E, NS, NE>, ValidationError<S, E>>
Builds and validates the state machine.
Returns an error if:
- The initial state is not set
- Any transition is undefined
- State or event indices are invalid
§Errors
Returns a ValidationError if the state machine configuration is invalid.
§Example
let result = StateMachineBuilder::<S, E, 2, 1>::new()
.initial(S::A)
.transition(S::A, E::X, S::B)
.transition(S::B, E::X, S::A)
.build();
assert!(result.is_ok());Trait Implementations§
Auto Trait Implementations§
impl<S, E, const NS: usize, const NE: usize> Freeze for StateMachineBuilder<S, E, NS, NE>where
S: Freeze,
impl<S, E, const NS: usize, const NE: usize> RefUnwindSafe for StateMachineBuilder<S, E, NS, NE>where
S: RefUnwindSafe,
E: RefUnwindSafe,
impl<S, E, const NS: usize, const NE: usize> Send for StateMachineBuilder<S, E, NS, NE>
impl<S, E, const NS: usize, const NE: usize> Sync for StateMachineBuilder<S, E, NS, NE>
impl<S, E, const NS: usize, const NE: usize> Unpin for StateMachineBuilder<S, E, NS, NE>
impl<S, E, const NS: usize, const NE: usize> UnwindSafe for StateMachineBuilder<S, E, NS, NE>where
S: UnwindSafe,
E: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more