-
Notifications
You must be signed in to change notification settings - Fork 0
/
sm_enum_msgs_any.rs
126 lines (114 loc) · 4.28 KB
/
sm_enum_msgs_any.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use crate::{EnumMsgs, MsgAny, ProcessMsgAny};
use std::{
fmt::{self, Debug},
num::Wrapping,
};
// Why do I have to declare a type alias here, I'd like to `use` it?
// use crate::SmProcessMsgFn;
pub type SmProcessMsgFn<SM> = fn(&mut SM, Box<MsgAny>);
pub struct SmEnumMsgsAny {
current_state: SmProcessMsgFn<Self>,
pub state0_counter: usize,
pub state0_quit_counter: usize,
pub state0_move_counter: usize,
pub state0_move_xy_counter: Wrapping<i32>,
pub state0_write_counter: usize,
pub state0_write_sum_len_s: usize,
pub state0_none_counter: usize,
pub state1_counter: usize,
pub state1_quit_counter: usize,
pub state1_move_counter: usize,
pub state1_move_xy_counter: Wrapping<i32>,
pub state1_write_counter: usize,
pub state1_write_sum_len_s: usize,
pub state1_none_counter: usize,
}
impl Debug for SmEnumMsgsAny {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SmEnumMsgsAny")
//.field("current_state", &self.current_state)
.field("state0_counter", &self.state0_counter)
.field("state0_quit_counter", &self.state0_quit_counter)
.field("state0_move_counter", &self.state0_move_counter)
.field("state0_move_xy_counter", &self.state0_move_xy_counter)
.field("state0_write_counter", &self.state0_write_counter)
.field(
"state0_write_sum_len_s_counter",
&self.state0_write_sum_len_s,
)
.field("state0_none_counter", &self.state0_none_counter)
.field("state1_counter", &self.state1_counter)
.field("state1_quit_counter", &self.state1_quit_counter)
.field("state1_move_counter", &self.state1_move_counter)
.field("state1_move_xy_counter", &self.state1_move_xy_counter)
.field("state1_write_counter", &self.state1_write_counter)
.field(
"state1_write_sum_len_s_counter",
&self.state1_write_sum_len_s,
)
.field("state1_none_counter", &self.state1_none_counter)
.finish()
}
}
#[allow(unused)]
impl SmEnumMsgsAny {
pub fn new(initial_state: SmProcessMsgFn<Self>) -> Self {
Self {
current_state: initial_state,
state0_counter: 0,
state0_quit_counter: 0,
state0_move_counter: 0,
state0_move_xy_counter: Wrapping(0),
state0_write_counter: 0,
state0_write_sum_len_s: 0,
state0_none_counter: 0,
state1_counter: 0,
state1_quit_counter: 0,
state1_move_counter: 0,
state1_move_xy_counter: Wrapping(0),
state1_write_counter: 0,
state1_write_sum_len_s: 0,
state1_none_counter: 0,
}
}
fn transition(&mut self, dest: SmProcessMsgFn<Self>) {
self.current_state = dest;
}
pub fn state0(&mut self, msg: Box<MsgAny>) {
self.state0_counter += 1;
match msg.downcast_ref::<EnumMsgs>() {
Some(EnumMsgs::Quit) => self.state0_quit_counter += 1,
Some(EnumMsgs::Move { x, y }) => {
self.state0_move_counter += 1;
self.state0_move_xy_counter += x + y;
}
Some(EnumMsgs::Write(s)) => {
self.state0_write_counter += 1;
self.state0_write_sum_len_s += s.len();
}
None => self.state0_none_counter += 1,
}
self.transition(SmEnumMsgsAny::state1);
}
pub fn state1(&mut self, msg: Box<MsgAny>) {
self.state1_counter += 1;
match msg.downcast_ref::<EnumMsgs>() {
Some(EnumMsgs::Quit) => self.state1_quit_counter += 1,
Some(EnumMsgs::Move { x, y }) => {
self.state1_move_counter += 1;
self.state1_move_xy_counter += x + y;
}
Some(EnumMsgs::Write(s)) => {
self.state1_write_counter += 1;
self.state1_write_sum_len_s += s.len();
}
None => self.state1_none_counter += 1,
}
self.transition(SmEnumMsgsAny::state0);
}
}
impl ProcessMsgAny for SmEnumMsgsAny {
fn process_msg_any(&mut self, msg: Box<MsgAny>) {
(self.current_state)(self, msg);
}
}