use core::{
task::{Context, Poll},
time::Duration,
};
use futures_util::stream::Stream;
pub trait AsyncInterval: Stream<Item = Self::Instant> + Send + Unpin {
type Instant: super::Instant;
fn reset(&mut self, interval: Duration);
fn reset_at(&mut self, instant: Self::Instant);
fn poll_tick(&mut self, cx: &mut Context<'_>) -> Poll<Self::Instant>;
}
impl<T> AsyncInterval for T
where
T: Send + AsyncLocalInterval,
T::Instant: Send,
{
type Instant = T::Instant;
fn reset(&mut self, interval: Duration) {
AsyncLocalInterval::reset(self, interval)
}
fn reset_at(&mut self, instant: Self::Instant) {
AsyncLocalInterval::reset_at(self, instant)
}
fn poll_tick(&mut self, cx: &mut Context<'_>) -> Poll<Self::Instant> {
AsyncLocalInterval::poll_tick(self, cx)
}
}
impl<T> AsyncIntervalExt for T
where
T: Send + AsyncLocalIntervalExt,
T::Instant: Send,
{
fn interval(period: Duration) -> Self
where
Self: Sized,
{
AsyncLocalIntervalExt::interval_local(period)
}
fn interval_at(start: Self::Instant, period: Duration) -> Self
where
Self: Sized,
{
AsyncLocalIntervalExt::interval_local_at(start, period)
}
}
pub trait AsyncIntervalExt: AsyncInterval {
fn interval(period: Duration) -> Self
where
Self: Sized;
fn interval_at(start: Self::Instant, period: Duration) -> Self
where
Self: Sized;
}
pub trait AsyncLocalInterval: Stream<Item = Self::Instant> + Unpin {
type Instant: super::Instant;
fn reset(&mut self, interval: Duration);
fn reset_at(&mut self, instant: Self::Instant);
fn poll_tick(&mut self, cx: &mut Context<'_>) -> Poll<Self::Instant>;
}
pub trait AsyncLocalIntervalExt: AsyncInterval {
fn interval_local(period: Duration) -> Self
where
Self: Sized;
fn interval_local_at(start: Self::Instant, period: Duration) -> Self
where
Self: Sized;
}