use core::{
future::Future,
pin::Pin,
time::Duration,
};
pub trait AsyncSleep: Future<Output = Self::Instant> + Send {
type Instant: super::Instant;
fn reset(self: Pin<&mut Self>, deadline: Self::Instant);
}
pub trait AsyncSleepExt: AsyncSleep {
fn sleep(after: Duration) -> Self
where
Self: Sized;
fn sleep_until(deadline: Self::Instant) -> Self
where
Self: Sized;
}
impl<T: Send + AsyncLocalSleep> AsyncSleep for T
where
T: Send + AsyncLocalSleep,
T::Instant: Send,
{
type Instant = T::Instant;
fn reset(self: Pin<&mut Self>, deadline: Self::Instant) {
AsyncLocalSleep::reset(self, deadline)
}
}
impl<T> AsyncSleepExt for T
where
T: Send + AsyncLocalSleepExt,
T::Instant: Send,
{
fn sleep(after: Duration) -> Self
where
Self: Sized,
{
AsyncLocalSleepExt::sleep_local(after)
}
fn sleep_until(deadline: Self::Instant) -> Self
where
Self: Sized,
{
AsyncLocalSleepExt::sleep_local_until(deadline)
}
}
pub trait AsyncLocalSleep: Future<Output = Self::Instant> {
type Instant: super::Instant;
fn reset(self: Pin<&mut Self>, deadline: Self::Instant);
}
pub trait AsyncLocalSleepExt: AsyncLocalSleep {
fn sleep_local(after: Duration) -> Self
where
Self: Sized;
fn sleep_local_until(deadline: Self::Instant) -> Self
where
Self: Sized;
}