Small finite state machine library with no allocations. Supports Mealy and Moore machines.
- Zero heap allocations
no_stdcompatible- Static transition and output tables
- Explicit error handling
- 256 states maximum (u8)
cargo run --example lightswitch_mealy
cargo run --example lightswitch_mooreOutput depends on current state.
Used when output is fixed per state, for example to display values.
use fsmall::Moore;#[derive(Copy,Clone,Eq,PartialEq)]enumInput{A,B}#[derive(Copy,Clone,Debug,PartialEq)]enumOutput{X,Y}// Transition table: (from_state, input, to_state)staticTRANSITIONS:[(u8,Input,u8);2] = [(0,Input::A,1),(1,Input::B,0),];// Output array: outputs[state] = outputstaticOUTPUTS:[Output;2] = [Output::X,// State 0 outputs XOutput::Y,// State 1 outputs Y];letmut fsm = Moore::new(0,&TRANSITIONS,&OUTPUTS);// Get current output without transitioningassert_eq!(fsm.current_output(),Ok(Output::X));// Transition to state 1, get new state's outputassert_eq!(fsm.step(Input::A),Ok(Output::Y));assert_eq!(fsm.current_state(),1);Output depends on both current state and input.
Used when output changes based on transition, for example in events and protocol handlers.
use fsmall::Mealy;#[derive(Copy,Clone,Eq,PartialEq)]enumInput{A,B}#[derive(Copy,Clone,Debug,PartialEq)]enumOutput{X,Y}// Transition table: (from_state, input, to_state)staticTRANSITIONS:[(u8,Input,u8);2] = [(0,Input::A,1),(1,Input::B,0),];// Output table: (state, input, output)staticOUTPUTS:[(u8,Input,Output);2] = [(0,Input::A,Output::X),(1,Input::B,Output::Y),];letmut fsm = Mealy::new(0,&TRANSITIONS,&OUTPUTS);assert_eq!(fsm.step(Input::A),Ok(Output::X));assert_eq!(fsm.current_state(),1);