Skip to main content

tokio_os_timer/
delay.rs

1use crate::sys::{TimeSpec, Timer};
2use futures::{try_ready, Async, Future, Poll};
3use std::io;
4use std::time::Duration;
5
6/// A future that completes a specified amount of time from its creation.
7///
8/// Instances of `Delay` perform no work and complete with `()` once the specified duration has been passed.
9pub struct Delay {
10    e: Option<Timer>,
11}
12
13impl Delay {
14    /// Create a new `Delay` instance that elapses at now + `delay`.
15    #[deprecated(since = "0.1.8", note = "Please use the async-timer crate")]
16    pub fn new(delay: Duration) -> io::Result<Self> {
17        if delay.as_secs() == 0 && delay.subsec_nanos() == 0 {
18            // this would be interpreted as "inactive timer" by timerfd_settime
19            return Ok(Self { e: None });
20        }
21
22        let mut timer = Timer::new()?;
23
24        // arm the timer
25        timer.set(TimeSpec::Timeout(delay))?;
26
27        Ok(Self { e: Some(timer) })
28    }
29}
30
31impl Future for Delay {
32    type Item = ();
33    type Error = io::Error;
34    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
35        if let Some(ref mut e) = self.e {
36            try_ready!(e.poll());
37        }
38        Ok(Async::Ready(()))
39    }
40}