use std::{future::Future, time::Duration};
pub trait Runtime: Clone + Send + Sync + Unpin {
type Instant: Copy + Send + Sync + Unpin;
type Sleep: Future<Output = ()> + Send;
fn spawn<F>(&self, future: F)
where
F: Future<Output = ()> + Send + 'static;
fn now(&self) -> Self::Instant;
fn elapsed(&self, instant: Self::Instant) -> Duration;
fn duration_between(&self, earlier: Self::Instant, later: Self::Instant) -> Duration;
fn sleep(&self, duration: Duration) -> Self::Sleep;
}
impl<'a, R: Runtime> Runtime for &'a R {
type Instant = R::Instant;
type Sleep = R::Sleep;
fn spawn<F>(&self, future: F)
where
F: Future<Output = ()> + Send + 'static,
{
(**self).spawn(future);
}
fn now(&self) -> Self::Instant {
(**self).now()
}
fn elapsed(&self, instant: Self::Instant) -> Duration {
(**self).elapsed(instant)
}
fn duration_between(&self, earlier: Self::Instant, later: Self::Instant) -> Duration {
(**self).duration_between(earlier, later)
}
fn sleep(&self, duration: Duration) -> Self::Sleep {
(**self).sleep(duration)
}
}