Clearly manage state transitions using closures in Rust.
struct States;
impl States
{
fn a() -> &'static str { "a" }
fn b() -> &'static str { "b" }
fn c() -> &'static str { "c" }
}
let mut state = Stator::new(States::a());
Stator::any_state()
as the first parameter and a custom state as the second to execute on state enterStator::any_state()
as the second to execute on state exitStator::any_state()
as both parameters to execute on every state change``` state.add_handler(States::a(), States::b(), |from: &String| { println!("a -> b"); });
state.addhandler(Stator::anystate(), States::b(), |from: &String| { println!("? -> b"); });
state.addhandler(States::a(), Stator::anystate(), |from: &String| { println!("a -> ?"); });
state.addhandler(Stator::anystate(), States::a(), |from: &String| { println!("? -> a"); }); ```
Protip: Don't pass Stator::any_state()
to the enter()
method.
state.enter(States::b());
Stator: The stator is the stationary part of a rotary system, found in electric generators, electric motors, sirens, or biological rotors.