StateMachineBuilder

Struct StateMachineBuilder 

Source
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 implementing States
  • E: The event type implementing Events
  • NS: 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>

Source

pub fn new() -> Self

Creates a new state machine builder.

All transitions are initially undefined and must be configured before calling build.

§Example
use tinystate::StateMachineBuilder;

let builder = StateMachineBuilder::<S, E, 1, 1>::new();
Source

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);
Source

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);
Source

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)]);
Source

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);
Source

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§

Source§

impl<S: States, E: Events, const NS: usize, const NE: usize> Default for StateMachineBuilder<S, E, NS, NE>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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>

§

impl<S, E, const NS: usize, const NE: usize> Send for StateMachineBuilder<S, E, NS, NE>
where S: Send, E: Send,

§

impl<S, E, const NS: usize, const NE: usize> Sync for StateMachineBuilder<S, E, NS, NE>
where S: Sync, E: Sync,

§

impl<S, E, const NS: usize, const NE: usize> Unpin for StateMachineBuilder<S, E, NS, NE>
where S: Unpin, E: Unpin,

§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.