StateMachine

Struct StateMachine 

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

Source

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

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 -> B
Source

pub fn trigger_if<F>(&mut self, event: E, condition: F) -> bool
where F: FnOnce(&S) -> 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);
Source

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-loop
Source

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

pub const fn current(&self) -> &S

Returns a reference to the current state.

§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.current(), &S::A);

Auto Trait Implementations§

§

impl<S, E, const NS: usize, const NE: usize> Freeze for StateMachine<S, E, NS, NE>
where S: Freeze,

§

impl<S, E, const NS: usize, const NE: usize> RefUnwindSafe for StateMachine<S, E, NS, NE>

§

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

§

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

§

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

§

impl<S, E, const NS: usize, const NE: usize> UnwindSafe for StateMachine<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.