A simple Finite State Machine library in Rust. Provide State and Event types (usually enums), then generate a machine with an initial state, give it some transition behaviours and you have your state machine!
Usage
Using the simple coin-operated turnstyle example from the FSM wikipedia entry:
Define your states and events:
// currently states must be cloneable
// currently events must be cloneable
// currently must be convertable to u32
// currently must be convertable to u32
Create your machine and define your transitions:
// create the machine initially in the Locked state
let mut machine = new;
// create the transition from Locked -> Unlocked upon inserting a coin
machine.add_transition;
// create the transition from Unlocked -> Locked upon pushing the turnstyle
machine.add_transition;
Trigger events as needed and huzzah, off you go:
// initially we're in the Locked state
machine.on_event;
// now we're Unlocked, ("unlocked" was just printed)
machine.on_event;
// now we're Locked again, ("locked" was just printed)
This example is also the test case for the library, although here I've ommitted the test-related details.
To Do
- Remove need to Clone inside implementation, I'm sure it's unnecessary
- There must be a more elegant way to write both the add transition method and the on event method, perhaps using Option methods?
- A better trait than Into (u32) ? Not sure if I can get rid of ANY trait dependancy, but Into currently contributes to the dependancy on Clone, when all I really want is a unique ID for each state/ID (preferably sequential), aka: the enum tag.