forked from mdeloof/statig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
112 lines (93 loc) · 2.98 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
100
101
102
103
104
105
106
107
108
109
110
111
112
#![allow(unused)]
use statig::blocking::{self, *};
use std::io::Write;
#[derive(Default)]
pub struct Blinky;
// The event that will be handled by the state machine.
pub enum Event {
TimerElapsed,
ButtonPressed,
}
// The enum representing all states of the state machine. These are
// the states you can actually transition to.
pub enum State {
LedOn,
LedOff,
NotBlinking,
}
// The enum representing the superstates of the system. You can not transition
// to a superstate, but instead they define shared behavior of underlying states or
// superstates.
pub enum Superstate {
Blinking,
}
// The `statig` trait needs to be implemented on the type that will
// imlement the state machine.
impl IntoStateMachine for Blinky {
/// The enum that represents the state.
type State = State;
type Superstate<'sub> = Superstate;
/// The event type that will be submitted to the state machine.
type Event<'evt> = Event;
type Context<'ctx> = ();
/// The initial state of the state machine.
const INITIAL: State = State::LedOn;
}
// Implement the `statig::State` trait for the state enum.
impl blocking::State<Blinky> for State {
fn call_handler(&mut self, blinky: &mut Blinky, event: &Event, _: &mut ()) -> Response<Self> {
match self {
State::LedOn => Blinky::led_on(event),
State::LedOff => Blinky::led_off(event),
State::NotBlinking => Blinky::not_blinking(event),
}
}
fn superstate(&mut self) -> Option<Superstate> {
match self {
State::LedOn => Some(Superstate::Blinking),
State::LedOff => Some(Superstate::Blinking),
State::NotBlinking => None,
}
}
}
// Implement the `statig::Superstate` trait for the superstate enum.
impl blocking::Superstate<Blinky> for Superstate {
fn call_handler(&mut self, blinky: &mut Blinky, event: &Event, _: &mut ()) -> Response<State> {
match self {
Superstate::Blinking => Blinky::blinking(event),
}
}
}
impl Blinky {
fn led_on(event: &Event) -> Response<State> {
match event {
Event::TimerElapsed => Transition(State::LedOff),
_ => Super,
}
}
fn led_off(event: &Event) -> Response<State> {
match event {
Event::TimerElapsed => Transition(State::LedOn),
_ => Super,
}
}
fn blinking(event: &Event) -> Response<State> {
match event {
Event::ButtonPressed => Transition(State::NotBlinking),
_ => Super,
}
}
fn not_blinking(event: &Event) -> Response<State> {
match event {
Event::ButtonPressed => Transition(State::LedOn),
_ => Super,
}
}
}
fn main() {
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);
}