1 unstable release

Uses old Rust 2015

0.1.0 May 8, 2018

#11 in #trans

MIT/Apache

15KB
228 lines

machinae

machinae is a generic state machine intended to be primarily used in game development.

If you need help, ask on the rust-gamedev Gitter channel.

Contribution

Contribution is highly welcome! If you'd like another feature, just create an issue. You can also help out if you want to; just pick a "help wanted" issue. If you need any help, feel free to ask!

All contributions are assumed to be dual-licensed under MIT/Apache-2.

License

machinae is distributed under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE-APACHE and LICENSE-MIT.


lib.rs:

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

No runtime deps