#director #engine #versatile #game #state-machine

macro no-std director_macros

Director is a simple, versatile, ergonomic state machine in Rust-lang

4 releases (breaking)

Uses new Rust 2021

0.4.0 Sep 17, 2022
0.3.0 Sep 16, 2022
0.2.0 Sep 14, 2022
0.1.0 Sep 14, 2022

#29 in #state-machine

42 downloads per month
Used in 2 crates (via director_core)

MIT/Apache

12KB
271 lines

Director

Director is a simple, versatile, ergonomic state machine in Rust-lang.

CI Crates.io Licensed Twitter

| Examples | Docs | Latest Note |

director = "0.3.0"

Getting Started

use crate::Engine; // Any common state

#[director::state {
    super = StateBaz,
    sub = [StateBar, StateBar2]
}]
pub struct StateFoo {
    count: u32,
}

impl director::State<Engine> for StateFoo {
    /// This determines whether or not to run this local state machine.
    fn toggle(engine: &mut Engine, inner: Option<&Self>) -> bool {
        director::on!(inner, None) || director::off!(state: StateBaz, Some(state) if state.count > 1000)
    }
    /// This creates and imports new initial state on this local state machine when the toggle's on.
    fn load(engine: &mut Engine) -> Self {
        Self { count: Self::lock_super__state_baz().get().count }
    }
    /// This executes custom logics and manipulates this local state machine's states.
    fn run(&mut self, engine: &mut Engine) {
        self.count += 1;
        println!("{}", self.count);
    }
    /// When the toggle's off
    fn drop(&self, engine: &mut Engine) {
        // ...
        // After then, the sub states[i.e) StateBar and StateBar2] will be droped automatically.
    }
}
pub struct Engine; // i.e) dummy engine

fn main() {
    StateFoo::run(&mut engine);
}

Dependencies

~235–640KB
~15K SLoC