Expand description
§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 (
A
) is a mutable reference. - State
Machine - A simple, generic state machine. The argument can be
Enums§
- Trans
- A potential transition to another state.
Traits§
- DynState
- A dynamic version of
State
which allows transitions to boxedDynState
s. If you can use an enum instead, consider usingState
which is slightly more efficient. - State
State
trait with several callbacks which allow state transitions. It’s recommended that you use this with an enum; if you prefer trait objects, you should useDynState
instead.
Type Aliases§
- DynMachine
- Typedef for a state machine using boxed states (
DynState
). - DynResult
- Typedef for the result type for
DynState
. - State
Machine Ref - 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.