use std::io;
use std::time::{Duration, Instant};
use futures::{Future, Poll};
use tokio_timer::Delay;
use reactor::Handle;
#[must_use = "futures do nothing unless polled"]
#[derive(Debug)]
pub struct Timeout {
delay: Delay
}
impl Timeout {
pub fn new(dur: Duration, handle: &Handle) -> io::Result<Timeout> {
Timeout::new_at(Instant::now() + dur, handle)
}
pub fn new_at(at: Instant, handle: &Handle) -> io::Result<Timeout> {
Ok(Timeout {
delay: handle.remote.timer_handle.delay(at)
})
}
pub fn reset(&mut self, at: Instant) {
self.delay.reset(at)
}
}
impl Future for Timeout {
type Item = ();
type Error = io::Error;
fn poll(&mut self) -> Poll<(), io::Error> {
self.delay.poll()
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))
}
}