Moves when to Timeout from TimeoutToken

This commit is contained in:
Paul Colomiets 2016-10-06 21:11:46 +03:00
parent a99b2529e0
commit 411caa786d
3 changed files with 9 additions and 18 deletions

View File

@ -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<Task> {

View File

@ -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<Timeout> {
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);

View File

@ -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<TimeoutToken> {
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