Files
tokio/tokio-timer/src/interval.rs
T

89 lines
2.5 KiB
Rust
Raw Normal View History

2019-05-14 10:27:36 -07:00
use crate::clock;
use crate::Delay;
2019-06-30 08:48:53 -07:00
2019-06-24 12:34:30 -07:00
use std::future::Future;
use std::pin::Pin;
use std::task::{self, Poll};
2019-02-21 11:56:15 -08:00
use std::time::{Duration, Instant};
2018-03-28 22:26:47 -07:00
/// A stream representing notifications at fixed interval
#[derive(Debug)]
pub struct Interval {
/// Future that completes the next time the `Interval` yields a value.
2018-03-30 14:21:48 -07:00
delay: Delay,
2018-03-28 22:26:47 -07:00
/// The duration between values yielded by `Interval`.
duration: Duration,
}
impl Interval {
/// Create a new `Interval` that starts at `at` and yields every `duration`
/// interval after that.
///
/// Note that when it starts, it produces item too.
///
2018-03-28 22:26:47 -07:00
/// The `duration` argument must be a non-zero duration.
///
/// # Panics
///
/// This function panics if `duration` is zero.
pub fn new(at: Instant, duration: Duration) -> Interval {
2019-02-21 11:56:15 -08:00
assert!(
duration > Duration::new(0, 0),
"`duration` must be non-zero."
);
2018-03-28 22:26:47 -07:00
2018-03-30 14:21:48 -07:00
Interval::new_with_delay(Delay::new(at), duration)
2018-03-28 22:26:47 -07:00
}
/// Creates new `Interval` that yields with interval of `duration`.
///
/// The function is shortcut for `Interval::new(Instant::now() + duration, duration)`.
///
/// The `duration` argument must be a non-zero duration.
///
/// # Panics
///
/// This function panics if `duration` is zero.
pub fn new_interval(duration: Duration) -> Interval {
Interval::new(clock::now() + duration, duration)
}
2018-03-30 14:21:48 -07:00
pub(crate) fn new_with_delay(delay: Delay, duration: Duration) -> Interval {
2019-02-21 11:56:15 -08:00
Interval { delay, duration }
2018-03-28 22:26:47 -07:00
}
2019-06-30 08:48:53 -07:00
/// TODO: dox
pub fn poll_next(&mut self, cx: &mut task::Context<'_>) -> Poll<Option<Instant>> {
2018-03-30 14:21:48 -07:00
// Wait for the delay to be done
2019-06-24 12:34:30 -07:00
ready!(Pin::new(&mut self.delay).poll(cx));
2018-03-28 22:26:47 -07:00
2018-03-30 14:21:48 -07:00
// Get the `now` by looking at the `delay` deadline
let now = self.delay.deadline();
2018-03-28 22:26:47 -07:00
// The next interval value is `duration` after the one that just
// yielded.
2019-06-24 12:34:30 -07:00
let next = now + self.duration;
self.delay.reset(next);
2018-03-28 22:26:47 -07:00
// Return the current instant
2019-06-24 12:34:30 -07:00
Poll::Ready(Some(now))
2018-03-28 22:26:47 -07:00
}
2019-06-30 08:48:53 -07:00
/// TODO: dox
pub async fn next(&mut self) -> Option<Instant> {
use async_util::future::poll_fn;
poll_fn(|cx| self.poll_next(cx)).await
}
}
#[cfg(feature = "async-traits")]
impl futures_core::Stream for Interval {
type Item = Instant;
fn poll_next(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Option<Self::Item>> {
Interval::poll_next(self.get_mut(), cx)
}
2018-03-28 22:26:47 -07:00
}