-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsm_separate_msgs_any.rs
137 lines (122 loc) · 4.49 KB
/
sm_separate_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
127
128
129
130
131
132
133
134
135
136
137
use crate::{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>);
#[derive(Debug, Clone)]
pub struct Quit;
#[derive(Debug, Clone)]
pub struct Move {
pub x: Wrapping<i32>,
pub y: Wrapping<i32>,
}
#[derive(Debug, Clone)]
pub struct Write(pub String);
//#[derive(Debug)]
pub struct SmSeparateMsgsAny {
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 SmSeparateMsgsAny {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SmSeparateMsgsAny")
//.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 SmSeparateMsgsAny {
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;
if msg.downcast_ref::<Quit>().is_some() {
self.state0_quit_counter += 1;
} else if let Some(mm) = msg.downcast_ref::<Move>() {
self.state0_move_counter += 1;
self.state0_move_xy_counter += mm.x + mm.y;
} else if let Some(mw) = msg.downcast_ref::<Write>() {
self.state0_write_counter += 1;
self.state0_write_sum_len_s += mw.0.len();
} else {
self.state0_none_counter += 1;
}
self.transition(SmSeparateMsgsAny::state1);
}
pub fn state1(&mut self, msg: Box<MsgAny>) {
self.state1_counter += 1;
if msg.downcast_ref::<Quit>().is_some() {
self.state1_quit_counter += 1;
} else if let Some(mm) = msg.downcast_ref::<Move>() {
self.state1_move_counter += 1;
self.state1_move_xy_counter += mm.x + mm.y;
} else if let Some(mw) = msg.downcast_ref::<Write>() {
self.state1_write_counter += 1;
self.state1_write_sum_len_s += mw.0.len();
} else {
self.state1_none_counter += 1;
}
self.transition(SmSeparateMsgsAny::state0);
}
}
impl ProcessMsgAny for SmSeparateMsgsAny {
fn process_msg_any(&mut self, msg: Box<MsgAny>) {
(self.current_state)(self, msg);
}
}