1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use crate::sys::{TimeSpec, Timer};
use futures::{try_ready, Async, Future, Poll};
use std::io;
use std::time::Duration;
pub struct Delay {
e: Option<Timer>,
}
impl Delay {
pub fn new(delay: Duration) -> io::Result<Self> {
if delay.as_secs() == 0 && delay.subsec_nanos() == 0 {
return Ok(Self { e: None });
}
let mut timer = Timer::new()?;
timer.set(TimeSpec::Timeout(delay))?;
Ok(Self { e: Some(timer) })
}
}
impl Future for Delay {
type Item = ();
type Error = io::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
if let Some(ref mut e) = self.e {
try_ready!(e.poll());
}
Ok(Async::Ready(()))
}
}