@@ -7,9 +7,6 @@ pub(crate) mod param {
7
7
#[ derive( Debug ) ]
8
8
pub struct None ;
9
9
10
- #[ derive( Debug ) ]
11
- pub struct Disarmed ;
12
-
13
10
#[ derive( Debug ) ]
14
11
pub struct Armed ;
15
12
@@ -28,7 +25,7 @@ pub(crate) mod param {
28
25
#[ derive( Debug ) ]
29
26
pub struct Timer < ' a , Type , State , Clock : crate :: Clock , Dur : Duration > {
30
27
clock : & ' a Clock ,
31
- duration : Option < Dur > ,
28
+ duration : Dur ,
32
29
expiration : Option < Instant < Clock > > ,
33
30
_type : PhantomData < Type > ,
34
31
_state : PhantomData < State > ,
@@ -37,10 +34,10 @@ pub struct Timer<'a, Type, State, Clock: crate::Clock, Dur: Duration> {
37
34
impl < ' a , Clock : crate :: Clock , Dur : Duration > Timer < ' _ , param:: None , param:: None , Clock , Dur > {
38
35
/// Construct a new, `OneShot` `Timer`
39
36
#[ allow( clippy:: new_ret_no_self) ]
40
- pub fn new ( clock : & Clock ) -> Timer < OneShot , Disarmed , Clock , Dur > {
41
- Timer :: < OneShot , Disarmed , Clock , Dur > {
37
+ pub fn new ( clock : & Clock , duration : Dur ) -> Timer < OneShot , Armed , Clock , Dur > {
38
+ Timer :: < OneShot , Armed , Clock , Dur > {
42
39
clock,
43
- duration : Option :: None ,
40
+ duration,
44
41
expiration : Option :: None ,
45
42
_type : PhantomData ,
46
43
_state : PhantomData ,
@@ -72,30 +69,16 @@ impl<'a, Type, State, Clock: crate::Clock, Dur: Duration> Timer<'a, Type, State,
72
69
}
73
70
}
74
71
75
- impl < ' a , Type , Clock : crate :: Clock , Dur : Duration > Timer < ' a , Type , Disarmed , Clock , Dur > {
76
- /// Set the [`Duration`] of the timer
77
- ///
78
- /// This _arms_ the timer (makes it ready to run).
79
- pub fn set_duration ( self , duration : Dur ) -> Timer < ' a , Type , Armed , Clock , Dur > {
80
- Timer :: < Type , Armed , Clock , Dur > {
81
- clock : self . clock ,
82
- duration : Some ( duration) ,
83
- expiration : Option :: None ,
84
- _type : PhantomData ,
85
- _state : PhantomData ,
86
- }
87
- }
88
- }
89
72
impl < ' a , Type , Clock : crate :: Clock , Dur : Duration > Timer < ' a , Type , Armed , Clock , Dur > {
90
- /// Start the _armed_ timer from this instant
73
+ /// Start the timer from this instant
91
74
pub fn start ( self ) -> Timer < ' a , Type , Running , Clock , Dur >
92
75
where
93
76
Instant < Clock > : Add < Dur , Output = Instant < Clock > > ,
94
77
{
95
78
Timer :: < Type , Running , Clock , Dur > {
96
79
clock : self . clock ,
97
80
duration : self . duration ,
98
- expiration : Some ( self . clock . now ( ) . unwrap ( ) + self . duration . unwrap ( ) ) ,
81
+ expiration : Some ( self . clock . now ( ) . unwrap ( ) + self . duration ) ,
99
82
_type : PhantomData ,
100
83
_state : PhantomData ,
101
84
}
@@ -109,8 +92,9 @@ impl<Type, Clock: crate::Clock, Dur: Duration> Timer<'_, Type, Running, Clock, D
109
92
110
93
/// Returns the [`Duration`] of time elapsed since it was started
111
94
///
112
- /// The units of the [`Duration`] are the same as that used with
113
- /// [`set_duration()`](struct.Timer.html#method.set_duration).
95
+ /// **The duration is truncated, not rounded**.
96
+ ///
97
+ /// The units of the [`Duration`] are the same as that used to construct the `Timer`.
114
98
pub fn elapsed ( & self ) -> Dur
115
99
where
116
100
Dur :: Rep : TryFrom < Clock :: Rep > ,
@@ -119,14 +103,15 @@ impl<Type, Clock: crate::Clock, Dur: Duration> Timer<'_, Type, Running, Clock, D
119
103
self . clock
120
104
. now ( )
121
105
. unwrap ( )
122
- . duration_since ( & ( self . expiration . unwrap ( ) - self . duration . unwrap ( ) ) )
106
+ . duration_since ( & ( self . expiration . unwrap ( ) - self . duration ) )
123
107
. unwrap ( )
124
108
}
125
109
126
110
/// Returns the [`Duration`] until the expiration of the timer
127
111
///
128
- /// The units of the [`Duration`] are the same as that used with
129
- /// [`set_duration()`](struct.Timer.html#method.set_duration).
112
+ /// **The duration is truncated, not rounded**.
113
+ ///
114
+ /// The units of the [`Duration`] are the same as that used to construct the `Timer`.
130
115
pub fn remaining ( & self ) -> Dur
131
116
where
132
117
Dur :: Rep : TryFrom < Clock :: Rep > ,
@@ -151,8 +136,7 @@ impl<'a, Clock: crate::Clock, Dur: Duration> Timer<'a, OneShot, Running, Clock,
151
136
// since the timer is running, _is_expired() will return a value
152
137
while !self . _is_expired ( ) { }
153
138
154
- Timer :: < param:: None , param:: None , Clock , Dur > :: new ( self . clock )
155
- . set_duration ( self . duration . unwrap ( ) )
139
+ Timer :: < param:: None , param:: None , Clock , Dur > :: new ( self . clock , self . duration )
156
140
}
157
141
158
142
/// Check whether the timer has expired
@@ -177,9 +161,7 @@ impl<Clock: crate::Clock, Dur: Duration> Timer<'_, Periodic, Running, Clock, Dur
177
161
Self {
178
162
clock : self . clock ,
179
163
duration : self . duration ,
180
- expiration : self
181
- . expiration
182
- . map ( |expiration| expiration + self . duration . unwrap ( ) ) ,
164
+ expiration : self . expiration . map ( |expiration| expiration + self . duration ) ,
183
165
_type : PhantomData ,
184
166
_state : PhantomData ,
185
167
}
@@ -194,7 +176,7 @@ impl<Clock: crate::Clock, Dur: Duration> Timer<'_, Periodic, Running, Clock, Dur
194
176
{
195
177
// since the timer is running, _is_expired() will return a value
196
178
if self . _is_expired ( ) {
197
- self . expiration = Some ( self . expiration . unwrap ( ) + self . duration . unwrap ( ) ) ;
179
+ self . expiration = Some ( self . expiration . unwrap ( ) + self . duration ) ;
198
180
199
181
true
200
182
} else {
@@ -231,7 +213,7 @@ mod test {
231
213
init_ticks ( ) ;
232
214
let clock = Clock ;
233
215
234
- let timer = clock. new_timer ( ) . set_duration ( 1_u32 . seconds ( ) ) . start ( ) ;
216
+ let timer = clock. new_timer ( 1_u32 . seconds ( ) ) . start ( ) ;
235
217
236
218
thread:: scope ( |s| {
237
219
let timer_handle = s. spawn ( |_| timer. wait ( ) ) ;
@@ -260,11 +242,7 @@ mod test {
260
242
init_ticks ( ) ;
261
243
let clock = Clock ;
262
244
263
- let timer = clock
264
- . new_timer ( )
265
- . into_periodic ( )
266
- . set_duration ( 1_u32 . seconds ( ) )
267
- . start ( ) ;
245
+ let timer = clock. new_timer ( 1_u32 . seconds ( ) ) . into_periodic ( ) . start ( ) ;
268
246
269
247
thread:: scope ( |s| {
270
248
let timer_handle = s. spawn ( |_| timer. wait ( ) ) ;
@@ -292,11 +270,7 @@ mod test {
292
270
init_ticks ( ) ;
293
271
let clock = Clock ;
294
272
295
- let mut timer = clock
296
- . new_timer ( )
297
- . into_periodic ( )
298
- . set_duration ( 1_u32 . seconds ( ) )
299
- . start ( ) ;
273
+ let mut timer = clock. new_timer ( 1_u32 . seconds ( ) ) . into_periodic ( ) . start ( ) ;
300
274
301
275
add_to_ticks ( 2_u32 . seconds ( ) ) ;
302
276
@@ -309,7 +283,7 @@ mod test {
309
283
init_ticks ( ) ;
310
284
let clock = Clock ;
311
285
312
- let timer = clock. new_timer ( ) . set_duration ( 2_u32 . seconds ( ) ) . start ( ) ;
286
+ let timer = clock. new_timer ( 2_u32 . seconds ( ) ) . start ( ) ;
313
287
314
288
add_to_ticks ( 1_u32 . milliseconds ( ) ) ;
315
289
0 commit comments