Files
tokio/tokio-timer/src/delay.rs
T

111 lines
3.6 KiB
Rust
Raw Normal View History

2019-05-14 10:27:36 -07:00
use crate::timer::{HandlePriv, Registration};
2019-06-24 12:34:30 -07:00
use std::future::Future;
use std::pin::Pin;
use std::task::{self, Poll};
use std::time::{Duration, Instant};
2018-03-28 22:26:47 -07:00
/// A future that completes at a specified instant in time.
///
2018-03-30 14:21:48 -07:00
/// Instances of `Delay` perform no work and complete with `()` once the
2018-03-28 22:26:47 -07:00
/// specified deadline has been reached.
///
2018-03-30 14:21:48 -07:00
/// `Delay` has a resolution of one millisecond and should not be used for tasks
2018-03-28 22:26:47 -07:00
/// that require high-resolution timers.
///
/// # Cancellation
///
/// Canceling a `Delay` is done by dropping the value. No additional cleanup or
/// other work is required.
///
2018-03-28 22:26:47 -07:00
/// [`new`]: #method.new
#[derive(Debug)]
2018-03-30 14:21:48 -07:00
pub struct Delay {
/// The link between the `Delay` instance at the timer that drives it.
2018-03-28 22:26:47 -07:00
///
2018-08-21 21:48:40 -07:00
/// This also stores the `deadline` value.
registration: Registration,
2018-03-28 22:26:47 -07:00
}
2018-03-30 14:21:48 -07:00
impl Delay {
/// Create a new `Delay` instance that elapses at `deadline`.
2018-03-28 22:26:47 -07:00
///
/// Only millisecond level resolution is guaranteed. There is no guarantee
/// as to how the sub-millisecond portion of `deadline` will be handled.
2018-03-30 14:21:48 -07:00
/// `Delay` should not be used for high-resolution timer use cases.
pub fn new(deadline: Instant) -> Delay {
let registration = Registration::new(deadline, Duration::from_millis(0));
2018-08-21 21:48:40 -07:00
Delay { registration }
2018-03-28 22:26:47 -07:00
}
pub(crate) fn new_timeout(deadline: Instant, duration: Duration) -> Delay {
let registration = Registration::new(deadline, duration);
Delay { registration }
}
2018-08-21 21:48:40 -07:00
pub(crate) fn new_with_handle(deadline: Instant, handle: HandlePriv) -> Delay {
let mut registration = Registration::new(deadline, Duration::from_millis(0));
2018-08-21 21:48:40 -07:00
registration.register_with(handle);
Delay { registration }
2018-03-28 22:26:47 -07:00
}
/// Returns the instant at which the future will complete.
pub fn deadline(&self) -> Instant {
2018-08-21 21:48:40 -07:00
self.registration.deadline()
2018-03-28 22:26:47 -07:00
}
2018-03-30 14:21:48 -07:00
/// Returns true if the `Delay` has elapsed
2018-03-28 22:26:47 -07:00
///
2018-03-30 14:21:48 -07:00
/// A `Delay` is elapsed when the requested duration has elapsed.
2018-03-28 22:26:47 -07:00
pub fn is_elapsed(&self) -> bool {
2018-08-21 21:48:40 -07:00
self.registration.is_elapsed()
2018-03-28 22:26:47 -07:00
}
2018-03-30 14:21:48 -07:00
/// Reset the `Delay` instance to a new deadline.
2018-03-28 22:26:47 -07:00
///
2018-03-30 14:21:48 -07:00
/// Calling this function allows changing the instant at which the `Delay`
2018-03-28 22:26:47 -07:00
/// future completes without having to create new associated state.
///
/// This function can be called both before and after the future has
/// completed.
pub fn reset(&mut self, deadline: Instant) {
2018-08-21 21:48:40 -07:00
self.registration.reset(deadline);
2018-03-28 22:26:47 -07:00
}
2019-06-24 12:34:30 -07:00
// Used by `Timeout<Stream>`
2019-06-30 08:48:53 -07:00
#[cfg(feature = "async-traits")]
pub(crate) fn reset_timeout(&mut self) {
self.registration.reset_timeout();
}
2018-03-30 14:21:48 -07:00
/// Register the delay with the timer instance for the current execution
2018-03-28 22:26:47 -07:00
/// context.
fn register(&mut self) {
2018-08-21 21:48:40 -07:00
self.registration.register();
2018-03-28 22:26:47 -07:00
}
}
2018-03-30 14:21:48 -07:00
impl Future for Delay {
2019-06-24 12:34:30 -07:00
type Output = ();
2018-03-28 22:26:47 -07:00
2019-06-24 12:34:30 -07:00
fn poll(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
2018-03-30 14:21:48 -07:00
// Ensure the `Delay` instance is associated with a timer.
2018-03-28 22:26:47 -07:00
self.register();
2019-06-24 12:34:30 -07:00
// `poll_elapsed` can return an error in two cases:
//
// - AtCapacity: this is a pathlogical case where far too many
// delays have been scheduled.
// - Shutdown: No timer has been setup, which is a mis-use error.
//
// Both cases are extremely rare, and pretty accurately fit into
// "logic errors", so we just panic in this case. A user couldn't
// really do much better if we passed the error onwards.
match ready!(self.registration.poll_elapsed(cx)) {
Ok(()) => Poll::Ready(()),
Err(e) => panic!("timer error: {}", e),
}
2018-03-28 22:26:47 -07:00
}
}