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
use crate::{FsmBackend, FsmEventQueue, FsmEventQueueSender, lib::*};
#[derive(Clone)]
pub enum FsmEvent<E, T> {
Start,
Stop,
Timer(T),
Event(E)
}
impl<E, T> From<E> for FsmEvent<E, T> {
fn from(event: E) -> Self {
FsmEvent::Event(event)
}
}
impl<E, T> Debug for FsmEvent<E, T> where E: Debug, T: Debug {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
FsmEvent::Start => f.write_str("Fsm::Start"),
FsmEvent::Stop => f.write_str("Fsm::Stop"),
FsmEvent::Timer(t) => f.write_fmt(format_args!("Fsm::Timer({:?})", t)),
FsmEvent::Event(ev) => ev.fmt(f)
}
}
}
impl<E, T> AsRef<str> for FsmEvent<E, T> where E: AsRef<str> {
fn as_ref(&self) -> &str {
match self {
FsmEvent::Start => "Fsm::Start",
FsmEvent::Stop => "Fsm::Stop",
FsmEvent::Timer(_) => "Fsm::Timer",
FsmEvent::Event(e) => e.as_ref()
}
}
}
impl<E, T> FsmEvent<E, T> {
pub fn to_sub_fsm<FSub>(self) -> FsmEvent<<FSub as FsmBackend>::Events, <FSub as FsmBackend>::Timers>
where FSub: FsmBackend,
<FSub as FsmBackend>::Timers: From<T>,
<FSub as FsmBackend>::Timers: From<E>
{
match self {
FsmEvent::Start => FsmEvent::Start,
FsmEvent::Stop => FsmEvent::Stop,
FsmEvent::Timer(t) => {
FsmEvent::Timer(t.into())
}
FsmEvent::Event(ev) => {
FsmEvent::Timer(ev.into())
}
}
}
}
pub type FsmRegionId = usize;
pub struct EventContext<'a, TFsm, Q> where TFsm: FsmBackend, Q: FsmEventQueueSender<TFsm> {
pub context: &'a mut TFsm::Context,
pub queue: &'a mut Q,
pub region: FsmRegionId
}
impl<'a, TFsm, Q> Deref for EventContext<'a, TFsm, Q> where TFsm: FsmBackend, Q: FsmEventQueueSender<TFsm>
{
type Target = <TFsm as FsmBackend>::Context;
fn deref(&self) -> &Self::Target {
self.context
}
}
impl<'a, TFsm: FsmBackend, Q> DerefMut for EventContext<'a, TFsm, Q> where TFsm: FsmBackend, Q: FsmEventQueueSender<TFsm> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.context
}
}