Expand description
§tinystate
A small, fast and no_std finite state machine for Rust.
tinystate provides a compile-time validated state machine implementation with zero runtime overhead (only when building it its O(n^2), but since NE and NS are known is O(1) :P).
All state transitions are defined at compile time using const generics.
§Quick Start
use tinystate::{StateMachineBuilder, States, Events};
#[derive(Debug, Clone, Copy, Default)]
enum TrafficLight {
#[default]
Red,
Yellow,
Green,
}
impl States for TrafficLight {
fn index(&self) -> usize {
*self as usize
}
fn from_index(i: usize) -> Self {
match i {
0 => Self::Red,
1 => Self::Yellow,
2 => Self::Green,
_ => unreachable!(),
}
}
}
#[derive(Debug, Clone, Copy)]
enum Timer { Tick }
impl Events for Timer {
fn index(&self) -> usize { 0 }
fn from_index(_: usize) -> Self { Self::Tick }
}
let mut light = StateMachineBuilder::<TrafficLight, Timer, 3, 1>::new()
.initial(TrafficLight::Red)
.transition(TrafficLight::Red, Timer::Tick, TrafficLight::Green)
.transition(TrafficLight::Green, Timer::Tick, TrafficLight::Yellow)
.transition(TrafficLight::Yellow, Timer::Tick, TrafficLight::Red)
.build()
.unwrap();
light.trigger(Timer::Tick);
assert!(matches!(light.current(), TrafficLight::Green));Structs§
- State
Machine - A deterministic finite automaton (DFA) state machine.
- State
Machine Builder - Builder for constructing and validating state machines.
- Transition
- A single state transition.
- Transition
Matrix - A matrix of state transitions.
Enums§
- Validation
Error - Errors that can occur during state machine validation.