| Crates.io | fsmall |
| lib.rs | fsmall |
| version | 0.1.0 |
| created_at | 2025-10-09 13:12:30.059875+00 |
| updated_at | 2025-10-09 13:12:30.059875+00 |
| description | Small finite state machine library with no allocations, supports Mealy and Moore |
| homepage | |
| repository | https://github.com/jpnt/fsmall |
| max_upload_size | |
| id | 1875564 |
| size | 20,803 |
Small finite state machine library with no allocations. Supports Mealy and Moore machines.
no_std compatiblecargo run --example lightswitch_mealy
cargo run --example lightswitch_moore
Output depends on current state.
Used when output is fixed per state, for example to display values.
use fsmall::Moore;
#[derive(Copy, Clone, Eq, PartialEq)]
enum Input { A, B }
#[derive(Copy, Clone, Debug, PartialEq)]
enum Output { X, Y }
// Transition table: (from_state, input, to_state)
static TRANSITIONS: [(u8, Input, u8); 2] = [
(0, Input::A, 1),
(1, Input::B, 0),
];
// Output array: outputs[state] = output
static OUTPUTS: [Output; 2] = [
Output::X, // State 0 outputs X
Output::Y, // State 1 outputs Y
];
let mut fsm = Moore::new(0, &TRANSITIONS, &OUTPUTS);
// Get current output without transitioning
assert_eq!(fsm.current_output(), Ok(Output::X));
// Transition to state 1, get new state's output
assert_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)]
enum Input { A, B }
#[derive(Copy, Clone, Debug, PartialEq)]
enum Output { X, Y }
// Transition table: (from_state, input, to_state)
static TRANSITIONS: [(u8, Input, u8); 2] = [
(0, Input::A, 1),
(1, Input::B, 0),
];
// Output table: (state, input, output)
static OUTPUTS: [(u8, Input, Output); 2] = [
(0, Input::A, Output::X),
(1, Input::B, Output::Y),
];
let mut fsm = Mealy::new(0, &TRANSITIONS, &OUTPUTS);
assert_eq!(fsm.step(Input::A), Ok(Output::X));
assert_eq!(fsm.current_state(), 1);