Skip to content

Commit 5d737ca

Browse files
committed
Remove 'when' from Delay, switch reset to take a Duration
1 parent 2746e4d commit 5d737ca

File tree

3 files changed

+9
-20
lines changed

3 files changed

+9
-20
lines changed

src/delay.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use crate::{ScheduledTimer, TimerHandle};
2525
/// at.
2626
pub struct Delay {
2727
state: Option<Arc<Node<ScheduledTimer>>>,
28-
when: Instant,
2928
}
3029

3130
impl Delay {
@@ -38,12 +37,6 @@ impl Delay {
3837
Delay::new_handle(Instant::now() + dur, Default::default())
3938
}
4039

41-
/// Return the `Instant` when this delay will fire.
42-
#[inline]
43-
pub fn when(&self) -> Instant {
44-
self.when
45-
}
46-
4740
/// Creates a new future which will fire at the time specified by `at`.
4841
///
4942
/// The returned instance of `Delay` will be bound to the timer specified by
@@ -54,7 +47,6 @@ impl Delay {
5447
None => {
5548
return Delay {
5649
state: None,
57-
when: at,
5850
}
5951
}
6052
};
@@ -72,28 +64,25 @@ impl Delay {
7264
if inner.list.push(&state).is_err() {
7365
return Delay {
7466
state: None,
75-
when: at,
7667
};
7768
}
7869

7970
inner.waker.wake();
8071
Delay {
8172
state: Some(state),
82-
when: at,
8373
}
8474
}
8575

8676
/// Resets this timeout to an new timeout which will fire at the time
8777
/// specified by `at`.
8878
#[inline]
89-
pub fn reset(&mut self, at: Instant) {
90-
self.when = at;
91-
if self._reset(self.when).is_err() {
79+
pub fn reset(&mut self, delay: Duration) {
80+
if self._reset(delay).is_err() {
9281
self.state = None
9382
}
9483
}
9584

96-
fn _reset(&mut self, at: Instant) -> Result<(), ()> {
85+
fn _reset(&mut self, delay: Duration) -> Result<(), ()> {
9786
let state = match self.state {
9887
Some(ref state) => state,
9988
None => return Err(()),
@@ -111,7 +100,7 @@ impl Delay {
111100
Err(s) => bits = s,
112101
}
113102
}
114-
*state.at.lock().unwrap() = Some(at);
103+
*state.at.lock().unwrap() = Some(Instant::now() + delay);
115104
// If we fail to push our node then we've become an inert timer, so
116105
// we'll want to clear our `state` field accordingly
117106
timeouts.list.push(state)?;
@@ -165,6 +154,6 @@ impl Drop for Delay {
165154

166155
impl fmt::Debug for Delay {
167156
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
168-
f.debug_struct("Delay").field("when", &self.when).finish()
157+
f.debug_struct("Delay").finish()
169158
}
170159
}

src/delay_wasm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use gloo_timers::future::TimeoutFuture;
44
use send_wrapper::SendWrapper;
5-
use std::{time::{Duration, Instant}, pin::Pin, task::{Context, Poll}, future::Future};
5+
use std::{time::Duration, pin::Pin, task::{Context, Poll}, future::Future};
66

77
/// A version of `Delay` that works on wasm.
88
#[derive(Debug)]
@@ -19,8 +19,8 @@ impl Delay {
1919

2020
/// Resets the timeout. Panics on wasm.
2121
#[inline]
22-
pub fn reset(&mut self, at: Instant) {
23-
panic!("Resetting the timeout is not supported on wasm.")
22+
pub fn reset(&mut self, at: Duration) {
23+
*self = Delay::new(at);
2424
}
2525
}
2626

tests/smoke.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async fn reset() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
2424
assert!(i.elapsed() > dur);
2525

2626
let i = Instant::now();
27-
d.reset(Instant::now() + dur);
27+
d.reset(dur);
2828
d.await;
2929
assert!(i.elapsed() > dur);
3030
Ok(())

0 commit comments

Comments
 (0)