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
40
41
//! tokio timeouts cannot actually error, but for some stupid reason tokio decided to put an io
//! error on them. This is just a wrapper which removes the error and makes error handling easier
//! when using timeouts.

use futures::{Async, Future};
use std::time::{Duration, Instant};
use tokio_core;
use tokio_core::reactor::Handle;
use void::Void;

pub struct Timeout {
    inner: tokio_core::reactor::Timeout,
}

impl Timeout {
    pub fn new(duration: Duration, handle: &Handle) -> Timeout {
        Timeout {
            inner: unwrap!(tokio_core::reactor::Timeout::new(duration, handle)),
        }
    }

    pub fn new_at(at: Instant, handle: &Handle) -> Timeout {
        Timeout {
            inner: unwrap!(tokio_core::reactor::Timeout::new_at(at, handle)),
        }
    }

    pub fn reset(&mut self, at: Instant) {
        self.inner.reset(at)
    }
}

impl Future for Timeout {
    type Item = ();
    type Error = Void;

    fn poll(&mut self) -> Result<Async<()>, Void> {
        Ok(unwrap!(self.inner.poll()))
    }
}