use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use pin_project_lite::pin_project;
#[derive(Debug, Default, Clone)]
pub struct Clock;
impl Clock {
pub fn new() -> Self {
Self
}
}
pin_project! {
pub struct WrappedTimer {
#[pin]
timer: ::async_io::Timer,
}
}
impl Future for WrappedTimer {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
match this.timer.poll(cx) {
Poll::Ready(_) => Poll::Ready(()),
Poll::Pending => Poll::Pending,
}
}
}
impl crate::Clock for Clock {
type Instant = std::time::Instant;
type SleepFuture = WrappedTimer;
fn now(&self) -> Self::Instant {
std::time::Instant::now()
}
fn sleep(&self, duration: std::time::Duration) -> Self::SleepFuture {
WrappedTimer {
timer: ::async_io::Timer::after(duration),
}
}
}
impl crate::InstantExt for std::time::Instant {
fn saturating_duration_since(&self, earlier: Self) -> std::time::Duration {
self.saturating_duration_since(earlier)
}
}