state_machine
Example:
use Machine;
use StateMachine;
#[derive(Eq, PartialEq, Hash, Debug)]
enum States {
Empty,
Move,
Music,
Last
}
#[derive(Eq, PartialEq, Debug)] struct Empty;
#[derive(Eq, PartialEq, Debug)] struct Move;
#[derive(Eq, PartialEq, Debug)] struct Music;
impl Machine<States> for Empty {
fn update(&self, state: &mut States) -> bool {
println!("Empty state");
*state = States::Music;
true
}
}
impl Machine<States> for Music {
fn on_enter(&self, _: &mut States) -> bool {
println!("\nOn enter music state");
true
}
fn update(&self, state: &mut States) -> bool {
println!("Music state");
*state = States::Move;
true
}
}
impl Machine<States> for Move {
fn update(&self, _: &mut States) -> bool {
println!("\nStop state");
true
}
fn on_exit(&self, _: &mut States) -> bool {
println!("On exit move state");
false
}
}
fn main() {
let mut state = StateMachine::new(States::Empty);
state.add(States::Music, Box::new(Music));
state.add(States::Empty, Box::new(Empty));
state.add(States::Move, Box::new(Move));
// Empty state
state.next();
// On enter music state
// Music state
//
// Stop state
// On exit move state
let _ = state.run();
}