use async_io::Timer;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Instant;
use pin_project_lite::pin_project;
pin_project! {
#[must_use = "Futures do nothing unless polled or .awaited"]
#[derive(Debug)]
pub(crate) struct Deadline {
instant: Instant,
#[pin]
delay: Timer,
}
}
impl Clone for Deadline {
fn clone(&self) -> Self {
Self {
instant: self.instant,
delay: Timer::at(self.instant),
}
}
}
impl Future for Deadline {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
match this.delay.poll(cx) {
Poll::Ready(_) => Poll::Ready(()),
Poll::Pending => Poll::Pending,
}
}
}
impl Into<crate::Deadline> for std::time::Instant {
fn into(self) -> crate::Deadline {
let deadline = Deadline {
instant: self,
delay: Timer::at(self),
};
crate::Deadline {
kind: crate::deadline::DeadlineKind::AsyncIo { t: deadline },
}
}
}