forked from mdeloof/statig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
99 lines (85 loc) · 3.14 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#![allow(unused)]
use statig::prelude::*;
use std::fmt::Debug;
use std::io::Write;
#[derive(Debug, Default)]
pub struct Blinky;
// The event that will be handled by the state machine.
#[derive(Debug)]
pub enum Event {
TimerElapsed,
ButtonPressed,
}
/// The `state_machine` procedural macro generates the `State` and `Superstate`
/// enums by parsing the function signatures with a `state`, `superstate` or
/// `action` attribute. It also implements the `statig::State` and
/// `statig::Superstate` traits.
#[state_machine(
// This sets the initial state to `led_on`.
initial = "State::led_on()",
// Derive the Debug trait on the `State` enum.
state(derive(Debug)),
// Derive the Debug trait on the `Superstate` enum.
superstate(derive(Debug)),
// Set the `on_transition` callback.
on_transition = "Self::on_transition",
// Set the `on_dispatch` callback.
on_dispatch = "Self::on_dispatch"
)]
impl Blinky {
/// The `#[state]` attibute marks this as a state handler. By default the
/// `event` argument will map to the event handler by the state machine.
/// Every state must return a `Response<State>`.
#[state(superstate = "blinking")]
fn led_on(event: &Event) -> Response<State> {
match event {
// When we receive a `TimerElapsed` event we transition to the `led_off` state.
Event::TimerElapsed => Transition(State::led_off()),
// Other events are deferred to the superstate, in this case `blinking`.
_ => Super,
}
}
#[state(superstate = "blinking")]
fn led_off(event: &Event) -> Response<State> {
match event {
Event::TimerElapsed => Transition(State::led_on()),
_ => Super,
}
}
/// The `#[superstate]` attribute marks this as a superstate handler.
#[superstate]
fn blinking(event: &Event) -> Response<State> {
match event {
Event::ButtonPressed => Transition(State::not_blinking()),
_ => Super,
}
}
#[state]
fn not_blinking(event: &Event) -> Response<State> {
match event {
Event::ButtonPressed => Transition(State::led_on()),
// Altough this state has no superstate, we can still defer the event which
// will cause the event to be handled by an implicit `top` superstate.
_ => Super,
}
}
}
impl Blinky {
// The `on_transition` callback that will be called after every transition.
fn on_transition(&mut self, source: &State, target: &State) {
println!("transitioned from `{source:?}` to `{target:?}`");
}
fn on_dispatch(&mut self, state: StateOrSuperstate<Self>, event: &Event) {
println!("dispatching `{event:?}` to `{state:?}`");
}
}
fn main() {
let start = std::time::Instant::now();
let mut state_machine = Blinky::default().state_machine();
state_machine.handle(&Event::TimerElapsed);
state_machine.handle(&Event::ButtonPressed);
state_machine.handle(&Event::TimerElapsed);
state_machine.handle(&Event::ButtonPressed);
let end = std::time::Instant::now();
println!("Duration: {:?}", end - start);
}