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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use std::{future::Future, pin::Pin, task::Poll, time::Duration};

use pin_project_lite::pin_project;

use crate::{error::Elapsed, Delay};

/// Creates a new `Timeout` with a specified duration.
/// 
/// # Example
/// 
/// Creates a timeout with smol's timer
/// 
/// ```rust,no_run
/// use std::time::Duration;
/// use timer_kit::timeout;
/// 
/// let result = timeout::<smol::Timer, _>(Duration::from_millis(100), async { }).await;
/// ```
/// 
/// Creates a timeout with `fluvio_wasm_timer::Delay`
/// 
/// ```rust,no_run
/// use std::time::Duration;
/// use timer_kit::timeout;
/// 
/// let result = timeout::<fluvio_wasm_timer::Delay, _>(Duration::from_millis(100), async { }).await;
/// ```
pub fn timeout<D, Fut>(duration: Duration, fut: Fut) -> Timeout<D, Fut>
where
    D: Delay,
    Fut: Future,
{
    Timeout::new(duration, fut)
}

/// Creates a new `Timeout` with a specified deadline.
/// 
/// # Example
/// 
/// Creates a timeout with smol's timer
/// 
/// ```rust,no_run
/// use std::time::{Duration, Instant};
/// use timer_kit::timeout_at;
/// 
/// let result = timeout_at::<smol::Timer, _>(Instant::now() + Duration::from_millis(100), async { }).await;
/// ```
/// 
/// Creates a timeout with `fluvio_wasm_timer::Delay`
/// 
/// ```rust,no_run
/// use std::time::{Duration};
/// use fluent_wasm_timer::Instant;
/// use timer_kit::timeout_at;
/// 
/// let result = timeout_at::<fluvio_wasm_timer::Delay, _>(Instant::now() + Duration::from_millis(100), async { }).await;
/// ```
pub fn timeout_at<D, Fut>(deadline: D::Instant, fut: Fut) -> Timeout<D, Fut>
where
    D: Delay,
    Fut: Future,
{
    Timeout::new_at(deadline, fut)
}

pin_project! {
    /// A timeout future.
    ///
    /// It returns `<Fut as Future>::Output` if the future completes before the timeout or
    /// [`Elapsed`] if the timeout elapses before the future completes.
    ///
    /// # Warning - Exhaustion
    ///
    /// This future is not able to avoid exhaustion if the future never completes and never returns
    /// `Pending`. The user should ensure that the `Fut` future is able to return `Pending` at some
    /// point to avoid exhaustion.
    pub struct Timeout<D, Fut> {
        #[pin]
        delay: D,

        #[pin]
        future: Fut,
    }
}

impl<D, Fut> Timeout<D, Fut>
where
    D: Delay,
    Fut: Future,
{
    /// Creates a new `Timeout` with a specified duration.
    ///
    /// # Example
    ///
    /// Creates a timeout with smol's timer
    /// 
    /// ```rust,no_run
    /// use std::time::Duration;
    /// use timer_kit::Timeout;
    /// 
    /// let result = Timeout::<smol::Timer, _>::new(Duration::from_millis(100), async {}).await;
    /// ```
    /// 
    /// Creates a timeout with `fluvio_wasm_timer::Delay`
    /// 
    /// ```rust,no_run
    /// use std::time::Duration;
    /// use timer_kit::Timeout;
    /// 
    /// let result = Timeout::<fluvio_wasm_timer::Delay, _>::new(Duration::from_millis(100), async {}).await;
    /// ```
    pub fn new(duration: Duration, future: Fut) -> Self {
        Self {
            delay: D::delay(duration),
            future,
        }
    }

    /// Creates a new `Timeout` with a specified deadline.
    ///
    /// # Example
    ///
    /// Creates a timeout with smol's timer
    ///
    /// ```rust,no_run
    /// use std::time::{Duration, Instant};
    /// use timer_kit::Timeout;
    ///
    /// let result = Timeout::<smol::Timer, _>::new_at(Instant::now() + Duration::from_millis(100), async {}).await;
    /// ```
    ///
    /// Creates a timeout with `fluvio_wasm_timer::Delay`
    /// 
    /// ```rust,no_run
    /// use std::time::Duration;
    /// use fluent_wasm_timer::Instant;
    /// use timer_kit::Timeout;
    /// 
    /// let result = Timeout::<fluvio_wasm_timer::Delay, _>::new_at(Instant::now() + Duration::from_millis(100), async {}).await;
    /// ```
    pub fn new_at(deadline: D::Instant, future: Fut) -> Self {
        Self {
            delay: D::delay_until(deadline),
            future,
        }
    }
}

impl<D, Fut> Future for Timeout<D, Fut>
where
    D: Delay,
    Fut: Future,
{
    type Output = Result<Fut::Output, Elapsed>;

    fn poll(
        self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Self::Output> {
        let mut this = self.project();

        if let Poll::Ready(output) = this.future.poll(cx) {
            return Poll::Ready(Ok(output));
        }

        match this.delay.as_mut().poll_elapsed(cx) {
            Poll::Ready(_) => Poll::Ready(Err(Elapsed::new())),
            Poll::Pending => Poll::Pending,
        }
    }
}