Crate machinae [−] [src]
machinae
machinae
provides a generic state machine with a strong focus efficiency.
It expects you to use enums for the states by default, but you can also work with
trait objects by using the Dyn*
types.
use machinae::{State, StateMachine, Trans}; struct Event {} enum HelloState { Hello, Bye, } impl State<i32, (), Event> for HelloState { fn start(&mut self, number: i32) -> Result<Trans<Self>, ()> { match *self { HelloState::Hello => println!("Hello, {}", number), HelloState::Bye => println!("Bye, {}", number), } Ok(Trans::None) } fn update(&mut self, number: i32) -> Result<Trans<Self>, ()> { match *self { HelloState::Hello => { if number == 5 { Ok(Trans::Switch(HelloState::Bye)) } else { Ok(Trans::None) } } HelloState::Bye => { if number == 10 { Ok(Trans::Quit) } else { Ok(Trans::None) } } } } } let mut machine = StateMachine::new(HelloState::Hello); machine.start(314).unwrap(); let mut counter = 1; while machine.running() { machine.update(counter).unwrap(); counter += 1; }
Structs
Ref |
A helper type used for the first type parameter of the
state machine in case the argument ( |
StateMachine |
A simple, generic state machine. The argument can be |
Enums
Trans |
A potential transition to another state. |
Traits
DynState |
A dynamic version of |
State |
|
Type Definitions
DynMachine |
Typedef for a state machine using boxed states ( |
DynResult |
Typedef for the result type for |
StateMachineRef |
A state machine accepting a mutable reference as argument. You need to use this in case you're passing a mutable argument to the state machine, otherwise the compiler will complain. |