5 releases

Uses old Rust 2015

0.1.14 Oct 29, 2018
0.1.13 Oct 29, 2018
0.1.12 Oct 29, 2018
0.1.1 Oct 29, 2018
0.1.0 Oct 25, 2018

#1799 in Algorithms

MIT/Apache

12KB
157 lines

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();
}

No runtime deps