Crates.io | etsm |
lib.rs | etsm |
version | 0.5.0 |
source | src |
created_at | 2022-05-24 19:36:44.180056 |
updated_at | 2022-05-26 13:40:58.824947 |
description | Efficient Tiny State Machine using object callbacks. |
homepage | |
repository | https://github.com/ethiffeault/etsm |
max_upload_size | |
id | 593021 |
size | 15,808 |
Tiny state machine for rust, see etsm
Implement a bare bones state machine in many languages. This library aim to be simple as possible and support only basic features:
Add the following line to your Cargo.toml file:
[dependencies]
etsm = "x.y.z"
Or
Add this file directly into your project: etsm
Declare state machine
use etsm::*;
state_machine!(
Foo,
State {
{A, Some(Foo::enter_a), Some(Foo::exit_a)},
{B, Some(Foo::enter_b), None}
}
);
Instanciate the state machine in your struct
struct Foo {
state_machine: StateMachine<State>,
}
impl Foo {
fn new() -> Foo {
Foo {
state_machine: StateMachine::new(),
}
}
}
Add enter/exit callbacks
impl Foo {
//...
fn enter_a(&mut self) {
print!(" ->A ");
}
fn exit_a(&mut self) {
print!(" A-> ");
}
fn enter_b(&mut self) {
print!(" ->B ");
}
}
Execute transitions
fn run(&mut self) {
transition!(self, state_machine, Some(State::A));
transition!(self, state_machine, Some(State::B));
transition!(self, state_machine, None);
}
Output: " ->A A-> ->B "
Sample: ab
Declare state machine with data, you might use any type of data, view this data as static data for each state.
state_machine!(
Foo,
Data,
State {
// state, enter, exit, data
{ A, None, None, Some(Data { run : Foo::run_a })},
{ B, None, None, Some(Data { run : Foo::run_b })}
}
);
Declare the statedata struct
struct Data {
run: fn(&mut Foo),
}
Implement Foo
struct Foo {
state_machine: StateMachine<State>,
}
impl Foo {
fn new() -> Foo {
Foo {
state_machine: StateMachine::new(),
}
}
fn run_a(&mut self) {
print!(" A ");
}
fn run_b(&mut self) {
print!(" B ");
}
fn run(&mut self) {
// call run on the current state
if let Some(statedata) = get_statedata(&self.state_machine) {
(statedata.run)(self);
}
}
}
Call method run on Foo that forward it to the current state
#[test]
fn virtual_call() {
let mut foo = Foo::new();
transition!(&mut foo, state_machine, Some(State::A));
foo.run();
transition!(&mut foo, state_machine, Some(State::B));
foo.run();
}
Output: " A B "
Sample: virtual_call