tower_timeout/
future.rs

1//! Future types
2
3use crate::error::{Elapsed, Error};
4use pin_project::pin_project;
5use std::{
6    future::Future,
7    pin::Pin,
8    task::{Context, Poll},
9};
10use tokio::time::Delay;
11
12/// `Timeout` response future
13#[pin_project]
14#[derive(Debug)]
15pub struct ResponseFuture<T> {
16    #[pin]
17    response: T,
18    #[pin]
19    sleep: Delay,
20}
21
22impl<T> ResponseFuture<T> {
23    pub(crate) fn new(response: T, sleep: Delay) -> Self {
24        ResponseFuture { response, sleep }
25    }
26}
27
28impl<F, T, E> Future for ResponseFuture<F>
29where
30    F: Future<Output = Result<T, E>>,
31    E: Into<Error>,
32{
33    type Output = Result<T, Error>;
34
35    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
36        let this = self.project();
37
38        // First, try polling the future
39        match this.response.poll(cx) {
40            Poll::Ready(v) => return Poll::Ready(v.map_err(Into::into)),
41            Poll::Pending => {}
42        }
43
44        // Now check the sleep
45        match this.sleep.poll(cx) {
46            Poll::Pending => Poll::Pending,
47            Poll::Ready(_) => Poll::Ready(Err(Elapsed(()).into())),
48        }
49    }
50}