Skip to main content

tokio_os_timer/
interval.rs

1use crate::sys::{TimeSpec, Timer};
2use futures::{try_ready, Async, Poll, Stream};
3use std::io;
4use std::time::Duration;
5
6/// A stream that yields once every time a fixed amount of time elapses.
7///
8/// Instances of `Interval` perform no work.
9pub struct Interval {
10    e: Option<Timer>,
11}
12
13impl Interval {
14    /// Create a new `Interval` instance that yields at now + `interval`, and every subsequent
15    /// `interval`.
16    #[deprecated(since = "0.1.8", note = "Please use the async-timer crate")]
17    pub fn new(interval: Duration) -> io::Result<Self> {
18        if interval.as_secs() == 0 && interval.subsec_nanos() == 0 {
19            // this would be interpreted as "inactive timer" by timerfd_settime
20            return Ok(Self { e: None });
21        }
22
23        let mut timer = Timer::new()?;
24
25        // arm the timer
26        timer.set(TimeSpec::Interval(interval))?;
27
28        Ok(Self { e: Some(timer) })
29    }
30}
31
32impl Stream for Interval {
33    type Item = ();
34    type Error = io::Error;
35    fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
36        if let Some(ref mut e) = self.e {
37            try_ready!(e.poll());
38        }
39        Ok(Async::Ready(Some(())))
40    }
41}