Skip to content

Commit 7e5a7bc

Browse files
authored
Make overflow behavior Timer::never (#92)
1 parent 93cee46 commit 7e5a7bc

File tree

1 file changed

+32
-36
lines changed

1 file changed

+32
-36
lines changed

src/lib.rs

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,6 @@ fn duration_max() -> Duration {
9696
Duration::new(std::u64::MAX, 1_000_000_000 - 1)
9797
}
9898

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-
10599
/// A future or stream that emits timed events.
106100
///
107101
/// Timers are futures that output a single [`Instant`] when they fire.
@@ -209,11 +203,9 @@ impl Timer {
209203
/// # });
210204
/// ```
211205
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)
217209
}
218210

219211
/// Creates a timer that emits an event once at the given time instant.
@@ -250,12 +242,9 @@ impl Timer {
250242
/// # });
251243
/// ```
252244
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))
259248
}
260249

261250
/// Creates a timer that emits events periodically, starting at `start`.
@@ -299,11 +288,14 @@ impl Timer {
299288
/// # });
300289
/// ```
301290
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+
}
307299
}
308300

309301
/// Sets the timer to emit an event once at the given time instant.
@@ -327,10 +319,7 @@ impl Timer {
327319
/// # });
328320
/// ```
329321
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();
334323

335324
// Update the timeout.
336325
self.when = Some(instant);
@@ -362,12 +351,14 @@ impl Timer {
362351
/// # });
363352
/// ```
364353
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+
}
371362
}
372363

373364
/// Sets the timer to emit events periodically, starting at `start`.
@@ -392,10 +383,7 @@ impl Timer {
392383
/// # });
393384
/// ```
394385
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();
399387

400388
self.when = Some(start);
401389
self.period = period;
@@ -405,6 +393,14 @@ impl Timer {
405393
*id = Reactor::get().insert_timer(start, waker);
406394
}
407395
}
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+
}
408404
}
409405

410406
impl Drop for Timer {

0 commit comments

Comments
 (0)