1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use core::future::Future;
use core::pin::Pin;
use core::task::{Context, Poll};

#[derive(Debug)]
pub struct ManagerTimeout<T, D> {
    future: T,
    delay: D,
}

impl<T, D> ManagerTimeout<T, D> {
    pub fn new(future: T, delay: D) -> Self {
        Self { future, delay }
    }
}

impl<T, D, E> Future for ManagerTimeout<T, D>
where
    T: Future,
    D: Future<Output = E>,
{
    type Output = Result<T::Output, E>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        unsafe {
            let p = self.as_mut().map_unchecked_mut(|this| &mut this.future);
            if let Poll::Ready(fut) = p.poll(cx) {
                return Poll::Ready(Ok(fut));
            }
        }

        unsafe {
            match self.map_unchecked_mut(|this| &mut this.delay).poll(cx) {
                Poll::Ready(e) => Poll::Ready(Err(e)),
                Poll::Pending => Poll::Pending,
            }
        }
    }
}