fsmentry
fsmentry!
A code generator for finite state machines (FSMs) with the following features:
- An
entryapi to transition the state machine. - Illegal states and transitions can be made unrepresentable.
- States can contain data.
- Generic over user types.
- Custom
#[derive(..)]support. - Inline SVG diagrams in docs.
- Generated code is
#[no_std]compatible.
// define the machine.
fsmentry!
// instantiate the machine
let mut state = Red;
loop
About the generated code.
This macro has three main outputs:
- A "state" enum, which reflects the enum you pass in.
- An "entry" enum, with variants that reflect.
- Data contained in the state (if any).
- Transitions to a different state variant (if any) - see below.
- "transition" structs, which access the data in a variant and allow only legal transitions via methods.
- Transition structs expose their mutable reference to the "state" above, to allow you to write e.g your own pinning logic. It is recommended that you wrap each machine in its own module to keep this reference private, lest you seed panics by manually creating a transition struct with the wrong underlying state.
;
Hierarchical state machines
fsmentry needs no special considerations for sub-state machines - simply store one
on the relevant node!
Here is the example from the statig crate:
┌─────────────────────────┐
│ Blinking │🞀─────────┐
│ ┌───────────────┐ │ │
│ ┌─🞂│ LedOn │──┐ │ ┌───────────────┐
│ │ └───────────────┘ │ │ │ NotBlinking │
│ │ ┌───────────────┐ │ │ └───────────────┘
│ └──│ LedOff │🞀─┘ │ 🞁
│ └───────────────┘ │──────────┘
└─────────────────────────┘
fsmentry!
fsmentry!
let mut webcam = NotBlinking;
loop
Comparison with other state machine libraries
| Crate | Illegal states/transitions unrepresentable | States contain data | State machine definition | Comments |
|---|---|---|---|---|
fsmentry |
Yes | Yes | Graph | |
sm |
Yes | No | States, events, transitions | |
rust-fsm |
No | Yes (manually) | States, events, transitions | |
finny |
No | Yes | Builder | |
sfsm |
No | No | States and transitions | |
statig |
? | ? | ? | Complicated API! |
sad_machine |
Yes | No | States, events, transitions | |
machine |
No | Yes | States, events, transitions |