| Crates.io | fsmentry-core |
| lib.rs | fsmentry-core |
| version | 0.4.0 |
| created_at | 2023-10-08 03:22:13.243777+00 |
| updated_at | 2025-04-07 15:54:38.333951+00 |
| description | Finite State Machines with an entry API and data storage |
| homepage | https://crates.io/crates/fsmentry |
| repository | https://github.com/aatifsyed/fsmentry |
| max_upload_size | |
| id | 996884 |
| size | 61,170 |
fsmentryfsmentry! {
enum TrafficLight {
Red -> RedAmber -> Green -> Amber -> Red
}
}
A code generator for finite state machines (FSMs) with the following features:
entry api to transition the state machine.#[derive(..)] support.#[no_std] compatible.// define the machine.
fsmentry! {
/// This is a state machine for a traffic light
// Documentation on nodes and states will appear in the generated code
pub enum TrafficLight {
/// Documentation for the [`Red`] state.
Red, // this is a state
Green(String), // this state has data inside it.
Red -> RedAmber -> Green,
// ^ states can be defined inline.
Green -custom_method_name-> Amber
/// Custom method documentation
-> Red,
}
}
// instantiate the machine
let mut state = TrafficLight::Red;
loop {
match state.entry() {
TrafficLightEntry::Red(to) => to.red_amber(), // transition the state machine
// when you transition to a state with data,
// you must provide the data
TrafficLightEntry::RedAmber(to) => to.green(String::from("this is some data")),
TrafficLightEntry::Green(mut to) => {
// you can inspect or mutate the data in a state...
let data: &String = to.as_ref();
let data: &mut String = to.as_mut();
// ...and you get it back when you transition out of a state
let data: String = to.custom_method_name();
},
TrafficLightEntry::Amber(_) => break,
}
}
This macro has three main outputs:
mod my_state { // recommended to create a module per machine.
fsmentry::fsmentry! {
/// These attributes are passed through to the state enum.
#[derive(Debug)]
#[fsmentry(
mermaid(true), // Embed mermaid-js into the rustdoc to render a diagram.
entry(pub(crate) MyEntry), // Override the default visibility and name
unsafe(false), // By default, transition structs will panic if constructed incorrectly.
// If you promise to only create valid transition structs,
// or hide the transition structs in their own module,
// you can make these panics unreachable_unchecked instead.
rename_methods(false), // By default, non-overridden methods are given
// snake_case names according to their destination
// but you can turn this off.
)]
pub enum MyState<'a, T> {
Start -> GenericData(&'a mut T) -> Stop,
Start & GenericData -> Error,
// ^ This is shorthand for the following:
// Start -> Error,
// GenericData -> Error,
}
}}
assert_impl_debug::<my_state::MyState<u8>>();
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! {
enum Webcam {
NotBlinking -> Blinking(Led) -> NotBlinking
}
}
fsmentry! {
enum Led {
On -> Off -> On,
}
}
let mut webcam = Webcam::NotBlinking;
loop {
match webcam.entry() { // transition the outer machine
WebcamEntry::Blinking(mut webcam) => match webcam.as_mut().entry() { // transition the inner machine
LedEntry::Off(led) => led.on(),
LedEntry::On(led) => {
led.off();
webcam.not_blinking();
}
},
WebcamEntry::NotBlinking(webcam) => {
webcam.blinking(Led::On)
}
}
}
| 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 |