use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Duration;
use async_io::Timer;
use futures_core::stream::Stream;
pub fn interval(dur: Duration) -> Interval {
Interval {
delay: Timer::after(dur),
interval: dur,
}
}
#[derive(Debug)]
pub struct Interval {
delay: Timer,
interval: Duration,
}
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;
let _ = std::mem::replace(&mut self.delay, Timer::after(interval));
Poll::Ready(Some(()))
}
}