diff --git a/src/reactor/mod.rs b/src/reactor/mod.rs index b86178311..cf65b377f 100644 --- a/src/reactor/mod.rs +++ b/src/reactor/mod.rs @@ -451,7 +451,7 @@ impl Inner { } } - fn add_timeout(&mut self, at: Instant) -> io::Result<(usize, Instant)> { + fn add_timeout(&mut self, at: Instant) -> usize { if self.timeouts.vacant_entry().is_none() { let len = self.timeouts.len(); self.timeouts.reserve_exact(len); @@ -460,7 +460,7 @@ impl Inner { let slot = self.timer_heap.push((at, entry.index())); let entry = entry.insert((Some(slot), TimeoutState::NotFired)); debug!("added a timeout: {}", entry.index()); - Ok((entry.index(), at)) + return entry.index(); } fn update_timeout(&mut self, token: usize, handle: Task) -> Option { diff --git a/src/reactor/timeout.rs b/src/reactor/timeout.rs index 7efbf54b1..76fba9fd4 100644 --- a/src/reactor/timeout.rs +++ b/src/reactor/timeout.rs @@ -13,13 +13,14 @@ use reactor::timeout_token::TimeoutToken; /// A future representing the notification that a timeout has occurred. /// -/// Timeouts are created through the `LoopHandle::timeout` or -/// `LoopHandle::timeout_at` methods indicating when a timeout should fire at. +/// Timeouts are created through the `Timeout::new` or +/// `Timeout::new_at` methods indicating when a timeout should fire at. /// Note that timeouts are not intended for high resolution timers, but rather /// they will likely fire some granularity after the exact instant that they're /// otherwise indicated to fire at. pub struct Timeout { token: TimeoutToken, + when: Instant, handle: Remote, } @@ -41,6 +42,7 @@ impl Timeout { pub fn new_at(at: Instant, handle: &Handle) -> io::Result { Ok(Timeout { token: try!(TimeoutToken::new(at, &handle)), + when: at, handle: handle.remote().clone(), }) } @@ -53,7 +55,7 @@ impl Future for Timeout { fn poll(&mut self) -> Poll<(), io::Error> { // TODO: is this fast enough? let now = Instant::now(); - if *self.token.when() <= now { + if self.when <= now { Ok(Async::Ready(())) } else { self.token.update_timeout(&self.handle); diff --git a/src/reactor/timeout_token.rs b/src/reactor/timeout_token.rs index 98b38716b..1533deb1c 100644 --- a/src/reactor/timeout_token.rs +++ b/src/reactor/timeout_token.rs @@ -8,7 +8,6 @@ use reactor::{Message, Handle, Remote}; /// A token that identifies an active timeout. pub struct TimeoutToken { token: usize, - when: Instant, } impl TimeoutToken { @@ -17,23 +16,13 @@ impl TimeoutToken { pub fn new(at: Instant, handle: &Handle) -> io::Result { match handle.inner.upgrade() { Some(inner) => { - let (token, when) = try!(inner.borrow_mut().add_timeout(at)); - Ok(TimeoutToken { token: token, when: when }) + let token = inner.borrow_mut().add_timeout(at); + Ok(TimeoutToken { token: token }) } None => Err(io::Error::new(io::ErrorKind::Other, "event loop gone")), } } - /// Returns the instant in time when this timeout token will "fire". - /// - /// Note that this instant may *not* be the instant that was passed in when - /// the timeout was created. The event loop does not support high resolution - /// timers, so the exact resolution of when a timeout may fire may be - /// slightly fudged. - pub fn when(&self) -> &Instant { - &self.when - } - /// Updates a previously added timeout to notify a new task instead. /// /// # Panics