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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
use std::{
convert::{AsMut, AsRef},
fmt::Debug,
marker::PhantomData,
ops::{Deref, DerefMut},
};
pub use wlan_statemachine_macro::statemachine;
pub struct StateMachine<S> {
state: Option<S>,
}
impl<S: Debug> Debug for StateMachine<S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "State: {:?}", self.state)
}
}
impl<S: PartialEq> PartialEq for StateMachine<S> {
fn eq(&self, other: &Self) -> bool {
self.state == other.state
}
}
impl<S> StateMachine<S> {
pub fn new(state: S) -> Self {
StateMachine { state: Some(state) }
}
pub fn replace_state<F>(&mut self, map: F) -> &mut Self
where
F: FnOnce(S) -> S,
{
self.state = Some(map(self.state.take().unwrap()));
self
}
pub fn replace_state_with(&mut self, new_state: S) -> &mut Self {
self.state = Some(new_state);
self
}
pub fn into_state(self) -> S {
self.state.unwrap()
}
}
impl<S> AsRef<S> for StateMachine<S> {
fn as_ref(&self) -> &S {
&self.state.as_ref().unwrap()
}
}
impl<S> AsMut<S> for StateMachine<S> {
fn as_mut(&mut self) -> &mut S {
self.state.as_mut().unwrap()
}
}
impl<S> Deref for StateMachine<S> {
type Target = S;
fn deref(&self) -> &Self::Target {
self.as_ref()
}
}
impl<S> DerefMut for StateMachine<S> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.as_mut()
}
}
pub trait StateTransition<S> {
#[doc(hidden)]
fn __internal_transition_to(new_state: S) -> State<S>;
}
pub trait InitialState {}
pub struct State<S> {
pub data: S,
__internal_phantom: PhantomData<S>,
}
impl<S> State<S> {
pub fn new(data: S) -> State<S>
where
S: InitialState,
{
Self::__internal_new(data)
}
#[doc(hidden)]
pub fn __internal_new(data: S) -> State<S> {
Self { data, __internal_phantom: PhantomData }
}
pub fn release_data(self) -> (Transition<S>, S) {
(Transition { _phantom: PhantomData }, self.data)
}
pub fn transition_to<T>(self, new_state: T) -> State<T>
where
S: StateTransition<T>,
{
S::__internal_transition_to(new_state)
}
pub fn apply<T, E>(self, transition: T) -> E
where
T: MultiTransition<E, S>,
{
transition.from(self)
}
}
pub mod testing {
use super::*;
pub fn new_state<S>(data: S) -> State<S> {
State::<S>::__internal_new(data)
}
}
impl<S> Deref for State<S> {
type Target = S;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl<S> DerefMut for State<S> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.data
}
}
impl<S: Debug> Debug for State<S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "State data: {:?}", self.data)
}
}
impl<S: PartialEq> PartialEq for State<S> {
fn eq(&self, other: &Self) -> bool {
self.data == other.data
}
}
pub struct Transition<S> {
_phantom: PhantomData<S>,
}
pub trait MultiTransition<E, S> {
fn from(self, state: State<S>) -> E;
fn via(self, transition: Transition<S>) -> E;
}
impl<S> Transition<S> {
pub fn to<T>(self, new_state: T) -> State<T>
where
S: StateTransition<T>,
{
S::__internal_transition_to(new_state)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Default, Debug)]
struct SharedStateData {
foo: u8,
}
pub struct A;
pub struct B(SharedStateData);
pub struct C(SharedStateData);
statemachine!(
enum States,
() => A,
A => B,
B => [C, A],
C => [A],
B => C,
B => [A, C],
);
fn multi_transition(foo: u8) -> BTransition {
match foo {
0 => BTransition::ToA(A),
_ => BTransition::ToC(C(SharedStateData::default())),
}
}
#[derive(Debug)]
pub struct A2;
#[derive(Debug)]
pub struct B2;
statemachine!(
#[derive(Debug)]
enum States2,
() => A2,
A2 => B2,
A2 => B2, );
#[derive(Debug)]
pub struct NonGen;
#[derive(Debug)]
pub struct Gen1<E>(E);
#[derive(Debug)]
pub struct Gen2<'a, F>(&'a Vec<F>);
#[derive(Debug)]
pub struct Gen3<'a, E, F>(E, Gen2<'a, F>);
statemachine!(
#[derive(Debug)]
enum States3<'a, E, F>,
() => Gen1<E>,
Gen1<E> => [NonGen, Gen1<E>, Gen2<'a, F>, Gen3<'a, E, F>],
NonGen => [NonGen, Gen1<E>],
Gen2<'a, F> => Gen1<E>,
Gen3<'a, E, F> => Gen2<'a, F>,
);
#[test]
fn state_transitions() {
let state = State::new(A);
let state = state.transition_to(B(SharedStateData::default()));
let (transition, mut data) = state.release_data();
data.0.foo = 5;
let state = transition.to(C(data.0));
assert_eq!(state.0.foo, 5);
}
#[test]
fn state_transition_self_transition() {
let state = State::new(A);
let state = state.transition_to(B(SharedStateData { foo: 5 }));
let (transition, data) = state.release_data();
assert_eq!(data.0.foo, 5);
let state = transition.to(B(SharedStateData { foo: 2 }));
let (_, data) = state.release_data();
assert_eq!(data.0.foo, 2);
}
#[test]
fn statemachine() {
let mut statemachine = StateMachine::new(States::A(State::new(A)));
statemachine.replace_state(|state| match state {
States::A(state) => state.transition_to(B(SharedStateData::default())).into(),
_ => state,
});
match statemachine.into_state() {
States::B(State { data: B(SharedStateData { foo: 0 }), .. }) => (),
_ => panic!("unexpected state"),
}
}
#[test]
fn transition_enums() {
let state = State::new(A).transition_to(B(SharedStateData::default()));
let transition = multi_transition(0);
match state.apply(transition) {
States::A(_) => (),
_ => panic!("expected transition into A"),
};
}
#[test]
fn transition_enums_release() {
let state = State::new(A).transition_to(B(SharedStateData::default()));
let (transition, _data) = state.release_data();
let target = multi_transition(0);
match target.via(transition) {
States::A(_) => (),
_ => panic!("expected transition into A"),
};
}
#[test]
fn transition_enums_branching() {
let state = State::new(A).transition_to(B(SharedStateData::default()));
let (transition, _data) = state.release_data();
let target = multi_transition(1);
match target.via(transition) {
States::C(_) => (),
_ => panic!("expected transition into C"),
};
}
#[test]
fn generated_enum() {
let _state_machine: States2 = match States2::A2(State::new(A2)) {
States2::A2(state) => state.transition_to(B2).into(),
other => panic!("expected state A to be active: {:?}", other),
};
}
#[test]
fn generic_state_transitions() {
let test_vec = vec![10, 20, 30];
let state = State::new(Gen1("test"));
let state = state.transition_to(Gen2(&test_vec));
let (transition, data) = state.release_data();
let state = transition.to(Gen1("test2"));
let (transition, data2) = state.release_data();
assert_eq!(data2.0, "test2");
let state = transition.to(Gen3(data2.0, data));
assert_eq!((state.1).0, &test_vec);
}
#[test]
fn generated_generic_enum() {
let _state_machine: States3<'_, &str, u16> =
match States3::<&str, u16>::Gen1(State::new(Gen1("test"))) {
States3::Gen1(state) => state.transition_to(NonGen).into(),
other => panic!("expected state A to be active: {:?}", other),
};
}
}