@@ -96,12 +96,6 @@ fn duration_max() -> Duration {
96
96
Duration :: new ( std:: u64:: MAX , 1_000_000_000 - 1 )
97
97
}
98
98
99
- fn instant_max ( ) -> Instant {
100
- // In order to ensure this point in time is never reached, it
101
- // is put 30 years into the future.
102
- Instant :: now ( ) + Duration :: from_secs ( 86400 * 365 * 30 )
103
- }
104
-
105
99
/// A future or stream that emits timed events.
106
100
///
107
101
/// Timers are futures that output a single [`Instant`] when they fire.
@@ -209,11 +203,9 @@ impl Timer {
209
203
/// # });
210
204
/// ```
211
205
pub fn after ( duration : Duration ) -> Timer {
212
- Timer :: at (
213
- Instant :: now ( )
214
- . checked_add ( duration)
215
- . unwrap_or_else ( instant_max) ,
216
- )
206
+ Instant :: now ( )
207
+ . checked_add ( duration)
208
+ . map_or_else ( Timer :: never, Timer :: at)
217
209
}
218
210
219
211
/// Creates a timer that emits an event once at the given time instant.
@@ -250,12 +242,9 @@ impl Timer {
250
242
/// # });
251
243
/// ```
252
244
pub fn interval ( period : Duration ) -> Timer {
253
- Timer :: interval_at (
254
- Instant :: now ( )
255
- . checked_add ( period)
256
- . unwrap_or_else ( instant_max) ,
257
- period,
258
- )
245
+ Instant :: now ( )
246
+ . checked_add ( period)
247
+ . map_or_else ( Timer :: never, |at| Timer :: interval_at ( at, period) )
259
248
}
260
249
261
250
/// Creates a timer that emits events periodically, starting at `start`.
@@ -299,11 +288,14 @@ impl Timer {
299
288
/// # });
300
289
/// ```
301
290
pub fn set_after ( & mut self , duration : Duration ) {
302
- self . set_at (
303
- Instant :: now ( )
304
- . checked_add ( duration)
305
- . unwrap_or_else ( instant_max) ,
306
- ) ;
291
+ match Instant :: now ( ) . checked_add ( duration) {
292
+ Some ( instant) => self . set_at ( instant) ,
293
+ None => {
294
+ // Overflow to never going off.
295
+ self . clear ( ) ;
296
+ self . when = None ;
297
+ }
298
+ }
307
299
}
308
300
309
301
/// Sets the timer to emit an event once at the given time instant.
@@ -327,10 +319,7 @@ impl Timer {
327
319
/// # });
328
320
/// ```
329
321
pub fn set_at ( & mut self , instant : Instant ) {
330
- if let ( Some ( when) , Some ( ( id, _) ) ) = ( self . when , self . id_and_waker . as_ref ( ) ) {
331
- // Deregister the timer from the reactor.
332
- Reactor :: get ( ) . remove_timer ( when, * id) ;
333
- }
322
+ self . clear ( ) ;
334
323
335
324
// Update the timeout.
336
325
self . when = Some ( instant) ;
@@ -362,12 +351,14 @@ impl Timer {
362
351
/// # });
363
352
/// ```
364
353
pub fn set_interval ( & mut self , period : Duration ) {
365
- self . set_interval_at (
366
- Instant :: now ( )
367
- . checked_add ( period)
368
- . unwrap_or_else ( instant_max) ,
369
- period,
370
- ) ;
354
+ match Instant :: now ( ) . checked_add ( period) {
355
+ Some ( instant) => self . set_interval_at ( instant, period) ,
356
+ None => {
357
+ // Overflow to never going off.
358
+ self . clear ( ) ;
359
+ self . when = None ;
360
+ }
361
+ }
371
362
}
372
363
373
364
/// Sets the timer to emit events periodically, starting at `start`.
@@ -392,10 +383,7 @@ impl Timer {
392
383
/// # });
393
384
/// ```
394
385
pub fn set_interval_at ( & mut self , start : Instant , period : Duration ) {
395
- if let ( Some ( when) , Some ( ( id, _) ) ) = ( self . when , self . id_and_waker . as_ref ( ) ) {
396
- // Deregister the timer from the reactor.
397
- Reactor :: get ( ) . remove_timer ( when, * id) ;
398
- }
386
+ self . clear ( ) ;
399
387
400
388
self . when = Some ( start) ;
401
389
self . period = period;
@@ -405,6 +393,14 @@ impl Timer {
405
393
* id = Reactor :: get ( ) . insert_timer ( start, waker) ;
406
394
}
407
395
}
396
+
397
+ /// Helper function to clear the current timer.
398
+ fn clear ( & mut self ) {
399
+ if let ( Some ( when) , Some ( ( id, _) ) ) = ( self . when , self . id_and_waker . as_ref ( ) ) {
400
+ // Deregister the timer from the reactor.
401
+ Reactor :: get ( ) . remove_timer ( when, * id) ;
402
+ }
403
+ }
408
404
}
409
405
410
406
impl Drop for Timer {
0 commit comments