5 个版本 (破坏性更新)
0.5.0 | 2022 年 9 月 17 日 |
---|---|
0.4.0 | 2022 年 9 月 16 日 |
0.3.0 | 2022 年 9 月 14 日 |
0.2.0 | 2022 年 9 月 14 日 |
0.1.0 | 2022 年 9 月 14 日 |
#1080 in 游戏开发
12KB
Director
Director
是 Rust 语言中的一个简单、通用、易用的状态机。 (no-std)
director = "0.5.0"
原因?
因为编写状态机有点繁琐。不符合人类的习惯。难以实现架构的灵活性、可读性和更强的可分析性。 这个包 提供了所有这些。并且经过良好的优化[例:RAII]。所以你不需要担心你的实现会有糟糕的性能。
如何使用,
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
#[director::main(std::syn)] // It can be any kind of Mutex
fn main() {
for _ in 0..10000 {
StateBaz::run(&mut engine);
}
}
依赖
~1.5MB
~36K SLoC