Files
tokio/src/reactor/timeout_token.rs
T

57 lines
1.7 KiB
Rust
Raw Normal View History

2016-08-20 23:23:16 -07:00
use std::io;
use std::time::Instant;
use futures::task;
2016-09-07 16:11:19 -07:00
use reactor::{Message, Handle, Remote};
2016-09-02 11:07:52 -07:00
/// A token that identifies an active timeout.
pub struct TimeoutToken {
token: usize,
}
impl TimeoutToken {
2016-08-20 23:23:16 -07:00
/// Adds a new timeout to get fired at the specified instant, notifying the
/// specified task.
2016-09-07 16:11:19 -07:00
pub fn new(at: Instant, handle: &Handle) -> io::Result<TimeoutToken> {
match handle.inner.upgrade() {
Some(inner) => {
2016-10-06 21:11:46 +03:00
let token = inner.borrow_mut().add_timeout(at);
Ok(TimeoutToken { token: token })
2016-09-07 16:11:19 -07:00
}
None => Err(io::Error::new(io::ErrorKind::Other, "event loop gone")),
2016-08-20 23:23:16 -07:00
}
}
/// Updates a previously added timeout to notify a new task instead.
///
/// # Panics
///
/// This method will panic if the timeout specified was not created by this
/// loop handle's `add_timeout` method.
2016-09-07 16:11:19 -07:00
pub fn update_timeout(&self, handle: &Remote) {
2016-09-02 11:07:52 -07:00
handle.send(Message::UpdateTimeout(self.token, task::park()))
2016-08-20 23:23:16 -07:00
}
2016-10-06 01:59:26 +03:00
/// Resets previously added (or fired) timeout to an new timeout
///
/// # Panics
///
/// This method will panic if the timeout specified was not created by this
/// loop handle's `add_timeout` method.
pub fn reset_timeout(&mut self, at: Instant, handle: &Remote) {
handle.send(Message::ResetTimeout(self.token, at));
}
2016-08-20 23:23:16 -07:00
/// Cancel a previously added timeout.
///
/// # Panics
///
/// This method will panic if the timeout specified was not created by this
/// loop handle's `add_timeout` method.
2016-09-07 16:11:19 -07:00
pub fn cancel_timeout(&self, handle: &Remote) {
2016-09-02 11:07:52 -07:00
debug!("cancel timeout {}", self.token);
handle.send(Message::CancelTimeout(self.token))
2016-08-20 23:23:16 -07:00
}
}