use std::io;
use std::time::{Duration, Instant};
use futures::Poll;
use futures::Stream;
use tokio_timer::Interval as NewInterval;
use reactor::Handle;
#[must_use = "streams do nothing unless polled"]
pub struct Interval {
new: NewInterval
}
impl Interval {
pub fn new(dur: Duration, handle: &Handle) -> io::Result<Interval> {
Interval::new_at(Instant::now() + dur, dur, handle)
}
pub fn new_at(at: Instant, dur: Duration, handle: &Handle)
-> io::Result<Interval>
{
Ok(Interval {
new: handle.remote.timer_handle.interval(at, dur)
})
}
}
impl Stream for Interval {
type Item = ();
type Error = io::Error;
fn poll(&mut self) -> Poll<Option<()>, io::Error> {
self.new.poll()
.map(|async| async.map(|option| option.map(|_| ())))
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))
}
}