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
use std::time::Duration;
use rotor::{Machine, Scope, Response, EventSet, GenericScope, Time};
use rotor::void::{Void, unreachable};
pub struct Ticker<M: Timer> {
deadline: Time,
machine: M,
}
pub struct Interval<M: SimpleTimer>(Duration, M);
pub type IntervalFunc<C> = Ticker<Interval<Box<FnMut(&mut Scope<C>) + Send>>>;
pub trait Timer {
type Context;
fn timeout(self, scope: &mut Scope<Self::Context>) -> Self;
fn next_wakeup_time(&self, scheduled: Time,
scope: &mut Scope<Self::Context>)
-> Time;
}
pub trait SimpleTimer {
type Context;
fn timeout(self, scope: &mut Scope<Self::Context>) -> Self;
}
impl<T: Timer> Ticker<T> {
pub fn new(scope: &mut Scope<T::Context>, machine: T)
-> Response<Ticker<T>, Void>
{
let next = machine.next_wakeup_time(scope.now(), scope);
Response::ok(Ticker {
deadline: next,
machine: machine,
}).deadline(next)
}
}
impl<M: Timer> Machine for Ticker<M> {
type Context = M::Context;
type Seed = Void;
fn create(seed: Self::Seed, _scope: &mut Scope<Self::Context>)
-> Response<Self, Void>
{
unreachable(seed);
}
fn ready(self, _events: EventSet, _scope: &mut Scope<Self::Context>)
-> Response<Self, Self::Seed>
{
let deadline = self.deadline;
Response::ok(self).deadline(deadline)
}
fn spawned(self, _scope: &mut Scope<Self::Context>)
-> Response<Self, Self::Seed>
{
unreachable!();
}
fn timeout(self, scope: &mut Scope<Self::Context>)
-> Response<Self, Self::Seed>
{
let now = scope.now();
if now >= self.deadline {
let newm = self.machine.timeout(scope);
let next = newm.next_wakeup_time(self.deadline, scope);
Response::ok(Ticker {
deadline: next,
machine: newm,
}).deadline(next)
} else {
let deadline = self.deadline;
Response::ok(self).deadline(deadline)
}
}
fn wakeup(self, _scope: &mut Scope<Self::Context>)
-> Response<Self, Self::Seed>
{
let deadline = self.deadline;
Response::ok(self).deadline(deadline)
}
}
impl<T: SimpleTimer> Timer for Interval<T> {
type Context = T::Context;
fn timeout(self, scope: &mut Scope<Self::Context>) -> Self {
Interval(self.0, self.1.timeout(scope))
}
fn next_wakeup_time(&self, scheduled: Time,
scope: &mut Scope<Self::Context>)
-> Time
{
let goal = scheduled + self.0;
if scope.now() > goal {
return scope.now() + self.0;
} else {
return goal;
}
}
}
impl<C> SimpleTimer for Box<FnMut(&mut Scope<C>) + Send> {
type Context = C;
fn timeout(mut self, scope: &mut Scope<Self::Context>) -> Self {
self(scope);
self
}
}
pub fn interval_func<C, F>(scope: &mut Scope<C>, interval: Duration, fun: F)
-> Response<IntervalFunc<C>, Void>
where F: FnMut(&mut Scope<C>) + 'static + Send
{
Ticker::new(scope, Interval(interval, Box::new(fun)))
}