state_machine

Crates.iostate_machine
lib.rsstate_machine
version0.1.14
sourcesrc
created_at2018-10-25 19:21:01.2666
updated_at2018-10-29 19:52:16.268801
descriptionSimple state machine.
homepagehttps://crates.io
repositoryhttps://github.com/blowin/state_machine
max_upload_size
id92626
size11,790
(blowin)

documentation

https://docs.rs/cargo

README

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();
}
Commit count: 0

cargo fmt