#[cfg(feature = "std")]
mod std_system;
#[cfg(feature = "std")]
mod std_virtual;
#[cfg(all(feature = "std", feature = "tokio1"))]
mod tokio1_std_system;
#[cfg(all(feature = "std", feature = "tokio1"))]
mod tokio1_std_virtual;
#[cfg(all(feature = "std", feature = "chrono04", feature = "tokio1"))]
mod tokio1_chrono04_system;
#[cfg(all(feature = "std", feature = "chrono04", feature = "tokio1"))]
mod tokio1_chrono04_virtual;
mod ext;
mod private;
pub use ext::*;
#[cfg(feature = "std")]
pub use std_system::*;
#[cfg(feature = "std")]
pub use std_virtual::*;
#[cfg(all(feature = "std", feature = "tokio1"))]
pub use tokio1_std_system::*;
#[cfg(all(feature = "std", feature = "tokio1"))]
pub use tokio1_std_virtual::*;
#[cfg(all(feature = "std", feature = "chrono04", feature = "tokio1"))]
pub use tokio1_chrono04_system::*;
#[cfg(all(feature = "std", feature = "chrono04", feature = "tokio1"))]
pub use tokio1_chrono04_virtual::*;
pub trait Duration: PartialOrd {
fn zero() -> Self;
}
#[cfg(feature = "std")]
impl Duration for ::std::time::Duration {
fn zero() -> Self {
Self::ZERO
}
}
#[cfg(feature = "chrono04")]
impl Duration for ::chrono04::TimeDelta {
fn zero() -> Self {
::chrono04::TimeDelta::zero()
}
}
#[macro_export]
macro_rules! impl_now {
{
impl Now for $clock:ty {
type Instant = $instant:ty;
fn now(&$this:ident) -> Self::Instant $body:block
}
} => {
impl $crate::NowOnce for $clock {
type Instant = $instant;
fn now_once(mut self) -> Self::Instant {
$crate::NowMut::now_mut(&mut self)
}
}
impl $crate::NowMut for $clock {
fn now_mut(&mut self) -> Self::Instant {
$crate::Now::now(&*self)
}
}
impl $crate::Now for $clock {
#[allow(unused_variables)]
fn now(&self) -> Self::Instant {
let $this = self;
$body
}
}
};
}
#[macro_export]
macro_rules! impl_sleep {
{
impl Sleep<$duration:ty> for $sleep:ty {
type Timer = $timer:ty;
fn sleep(&$this:ident, $d:ident: $d_ty:ty) -> Self::Timer $body:block
}
} => {
impl $crate::SleepOnce<$duration> for $sleep {
type Timer = $timer;
fn sleep_once(mut self, duration: $duration) -> Self::Timer {
$crate::SleepMut::sleep_mut(&mut self, duration)
}
}
impl $crate::SleepMut<$duration> for $sleep {
fn sleep_mut(&mut self, duration: $duration) -> Self::Timer {
$crate::Sleep::sleep(&*self, duration)
}
}
impl $crate::Sleep<$duration> for $sleep {
#[allow(unused_variables)]
fn sleep(&self, $d: $d_ty) -> Self::Timer {
let $this = self;
$body
}
}
};
}
pub trait NowOnce {
type Instant: PartialOrd;
fn now_once(self) -> Self::Instant;
}
pub trait NowMut: NowOnce {
fn now_mut(&mut self) -> Self::Instant;
}
pub trait Now: NowMut {
fn now(&self) -> Self::Instant;
}
impl<'a, T> NowOnce for &'a T
where
T: Now,
{
type Instant = T::Instant;
fn now_once(self) -> Self::Instant {
self.now()
}
}
impl<'a, T> NowOnce for &'a mut T
where
T: NowMut,
{
type Instant = T::Instant;
fn now_once(self) -> Self::Instant {
self.now_mut()
}
}
pub unsafe trait Monotonic: NowOnce {}
pub trait SleepOnce<D> {
type Timer: Future<Output = ()> + Send;
fn sleep_once(self, duration: D) -> Self::Timer;
}
pub trait SleepMut<D>: SleepOnce<D> {
fn sleep_mut(&mut self, duration: D) -> Self::Timer;
}
pub trait Sleep<D>: SleepMut<D> {
fn sleep(&self, duration: D) -> Self::Timer;
}
#[cfg(feature = "std")]
pub trait ErasedSleepOnce<D>: private::ErasedSleepSealed<D> {
fn erased_sleep_once(self, duration: D) -> ::std::pin::Pin<Box<dyn Future<Output = ()> + Send>>;
}
#[cfg(feature = "std")]
impl<D, T> private::ErasedSleepSealed<D> for T
where
T: SleepOnce<D>,
T::Timer: Send + 'static,
{
}
#[cfg(feature = "std")]
impl<D, T> ErasedSleepOnce<D> for T
where
T: SleepOnce<D>,
T::Timer: Send + 'static,
{
fn erased_sleep_once(self, duration: D) -> ::std::pin::Pin<Box<dyn Future<Output = ()> + Send>> {
Box::pin(self.sleep_once(duration))
}
}
#[cfg(feature = "std")]
pub trait ErasedSleepMut<D>: ErasedSleepOnce<D> {
fn erased_sleep_mut(&mut self, duration: D) -> ::std::pin::Pin<Box<dyn Future<Output = ()> + Send>>;
}
#[cfg(feature = "std")]
impl<D, T> ErasedSleepMut<D> for T
where
T: ErasedSleepOnce<D> + SleepMut<D>,
T::Timer: Send + 'static,
{
fn erased_sleep_mut(&mut self, duration: D) -> ::std::pin::Pin<Box<dyn Future<Output = ()> + Send>> {
Box::pin(self.sleep_mut(duration))
}
}
#[cfg(feature = "std")]
pub trait ErasedSleep<D>: ErasedSleepMut<D> {
fn erased_sleep(&self, duration: D) -> ::std::pin::Pin<Box<dyn Future<Output = ()> + Send>>;
}
#[cfg(feature = "std")]
impl<D, T> ErasedSleep<D> for T
where
T: ErasedSleepMut<D> + Sleep<D>,
T::Timer: Send + 'static,
{
fn erased_sleep(&self, duration: D) -> ::std::pin::Pin<Box<dyn Future<Output = ()> + Send>> {
Box::pin(self.sleep(duration))
}
}
#[cfg(feature = "std")]
pub trait StdNow: Now<Instant = ::std::time::Instant> + private::StdNowSealed {
fn now_std(&self) -> ::std::time::Instant;
}
#[cfg(feature = "std")]
impl<T> private::StdNowSealed for T where T: Now<Instant = ::std::time::Instant> {}
#[cfg(feature = "std")]
impl<T> StdNow for T
where
T: Now<Instant = ::std::time::Instant>,
{
fn now_std(&self) -> ::std::time::Instant {
self.now()
}
}
#[cfg(feature = "std")]
pub trait StdClock: StdNow + Sleep<::std::time::Duration> {}
#[cfg(feature = "std")]
impl<T> StdClock for T where T: StdNow + Sleep<::std::time::Duration> {}
#[cfg(feature = "std")]
pub trait ErasedStdClock: StdNow + ErasedSleep<::std::time::Duration> {}
#[cfg(feature = "std")]
impl<T> ErasedStdClock for T where T: StdNow + ErasedSleep<::std::time::Duration> {}
#[cfg(feature = "chrono04")]
pub trait Chrono04Now: Now<Instant = ::chrono04::DateTime<::chrono04::Utc>> + private::ChronoNowSealed {
fn now_chrono(&self) -> ::chrono04::DateTime<::chrono04::Utc>;
}
#[cfg(feature = "chrono04")]
impl<T> private::ChronoNowSealed for T where T: Now<Instant = ::chrono04::DateTime<::chrono04::Utc>> {}
#[cfg(feature = "chrono04")]
impl<T> Chrono04Now for T
where
T: Now<Instant = ::chrono04::DateTime<::chrono04::Utc>>,
{
fn now_chrono(&self) -> ::chrono04::DateTime<::chrono04::Utc> {
self.now()
}
}
#[cfg(feature = "chrono04")]
pub trait Chrono04Clock: Chrono04Now + Sleep<::chrono04::TimeDelta> + Send + Sync {}
#[cfg(feature = "chrono04")]
impl<T> Chrono04Clock for T where T: Chrono04Now + Sleep<::chrono04::TimeDelta> + Send + Sync {}
#[cfg(feature = "chrono04")]
pub trait ErasedChrono04Clock: Chrono04Now + ErasedSleep<::chrono04::TimeDelta> + Send + Sync {}
#[cfg(feature = "chrono04")]
impl<T> ErasedChrono04Clock for T where T: Chrono04Now + ErasedSleep<::chrono04::TimeDelta> + Send + Sync {}