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
use futures::{Future, Stream, Async, Poll};

use {Sleep, TimerError};

use std::time::Duration;

/// A stream representing notifications at fixed interval
///
/// Intervals are created through `Timer::interval`.
#[derive(Debug)]
pub struct Interval {
    sleep: Sleep,
    duration: Duration,
}

/// Create a new interval
pub fn new(sleep: Sleep, dur: Duration) -> Interval {
    Interval {
        sleep: sleep,
        duration: dur,
    }
}

impl Stream for Interval {
    type Item = ();
    type Error = TimerError;

    fn poll(&mut self) -> Poll<Option<()>, TimerError> {
        let _ = try_ready!(self.sleep.poll());

        // Reset the timeout
        self.sleep = self.sleep.timer().sleep(self.duration);

        Ok(Async::Ready(Some(())))
    }
}