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
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Duration;
use futures::{future::Future, stream::Stream};
use futures_timer::Delay;
#[derive(Debug)]
pub struct Interval {
interval: Duration,
delay: Delay,
}
impl Stream for Interval {
type Item = ();
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
if Pin::new(&mut self.delay).poll(cx).is_pending() {
return Poll::Pending;
}
let interval = self.interval;
self.delay.reset(interval);
Poll::Ready(Some(()))
}
}
pub fn interval(duration: Duration) -> Interval {
Interval {
interval: duration,
delay: Delay::new(duration),
}
}