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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use crate::core::MtopError;
use pin_project_lite::pin_project;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Duration;

/// `Timeout` can be used to add a timeout to any future emitted by mtop.
pub trait Timeout: Sized {
    fn timeout<S>(self, t: Duration, operation: S) -> Timed<Self>
    where
        S: Into<String>;
}

impl<F, V> Timeout for F
where
    F: Future<Output = Result<V, MtopError>>,
{
    fn timeout<S>(self, t: Duration, operation: S) -> Timed<F>
    where
        S: Into<String>,
    {
        Timed {
            operation: operation.into(),
            time: t,
            inner: tokio::time::timeout(t, self),
        }
    }
}

pin_project! {
    #[derive(Debug)]
    #[must_use = "futures do nothing unless you `.await` or poll them"]
    pub struct Timed<T> {
        operation: String,
        time: Duration,
        #[pin]
        inner: tokio::time::Timeout<T>,
    }
}

impl<F, V> Future for Timed<F>
where
    F: Future<Output = Result<V, MtopError>>,
{
    type Output = F::Output;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.project();
        // Poll the inner Timeout future and unwrap one layer of Result it adds,
        // converting a timeout into specific form of an MtopError
        this.inner.poll(cx).map(|res| match res {
            Ok(Ok(v)) => Ok(v),
            Ok(Err(e)) => Err(e),
            Err(_e) => Err(MtopError::timeout(*this.time, this.operation)),
        })
    }
}