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
use std::error::Error;
use rotor::{Machine, Scope, EarlyScope, Loop, LoopInstance, SpawnError};
pub trait LoopExt<M> {
fn add_and_fetch<F, W, T, E, N>(&mut self, fsm_wrapper: W, fun: F)
-> Result<T, SpawnError<()>>
where E: Error + 'static,
W: FnOnce(N) -> M,
F: FnOnce(&mut EarlyScope) -> Result<(N, T), E>;
}
pub trait LoopInstanceExt<M: Machine> {
fn add_and_fetch<F, W, T, E, N>(&mut self, fsm_wrapper: W, fun: F)
-> Result<T, SpawnError<()>>
where E: Error + 'static,
W: FnOnce(N) -> M,
F: FnOnce(&mut Scope<M::Context>) -> Result<(N, T), E>;
}
impl<C, M: Machine<Context=C>> LoopExt<M> for Loop<C, M> {
fn add_and_fetch<F, W, T, E, N>(&mut self, fsm_wrapper: W, fun: F)
-> Result<T, SpawnError<()>>
where E: Error + 'static,
W: FnOnce(N) -> M,
F: FnOnce(&mut EarlyScope) -> Result<(N, T), E>
{
let mut result_opt = None;
try!(self.add_machine_with(|scope| {
let (fsm, value) = try!(fun(scope));
result_opt = Some(value);
Ok(fsm_wrapper(fsm))
}));
Ok(result_opt.unwrap())
}
}
impl<C, M: Machine<Context=C>> LoopInstanceExt<M> for LoopInstance<C, M> {
fn add_and_fetch<F, W, T, E, N>(&mut self, fsm_wrapper: W, fun: F)
-> Result<T, SpawnError<()>>
where E: Error + 'static,
W: FnOnce(N) -> M,
F: FnOnce(&mut Scope<M::Context>) -> Result<(N, T), E>
{
let mut result_opt = None;
try!(self.add_machine_with(|scope| {
let (fsm, value) = try!(fun(scope));
result_opt = Some(value);
Ok(fsm_wrapper(fsm))
}));
Ok(result_opt.unwrap())
}
}