use crate::Sleep;
use futures::future::Future;
use pin_project::pin_project;
use std::{
pin::Pin,
task::{Context, Poll},
};
use thiserror::Error;
#[derive(Debug, Error, Eq, PartialEq)]
#[error("timeout elapsed")]
pub struct Elapsed;
#[pin_project]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct Timeout<F> {
#[pin]
future: F,
#[pin]
delay: Sleep,
}
impl<F> Timeout<F> {
pub fn new(future: F, delay: Sleep) -> Self {
Self { future, delay }
}
pub fn into_inner(self) -> F {
self.future
}
}
impl<F: Future> Future for Timeout<F> {
type Output = Result<F::Output, Elapsed>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
if let Poll::Ready(v) = this.future.poll(cx) {
return Poll::Ready(Ok(v));
}
this.delay.poll(cx).map(|_| Err(Elapsed))
}
}