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
172
173
174
175
176
177
178
179
180
181
//! Timed future

use core::future::Future;
use core::{fmt, task, time};
use core::pin::Pin;

use crate::oneshot::Oneshot;
use crate::oneshot::Timer as PlatformTimer;

#[must_use = "Timed does nothing unless polled"]
///Limiter on time to wait for underlying `Future`
///
///# Usage
///
///```rust, no_run
///#![feature(async_await)]
///
///async fn job() {
///}
///
///async fn do_job() {
///    let work = unsafe {
///        async_timer::Timed::platform_new_unchecked(job(), core::time::Duration::from_secs(1))
///    };
///
///    match work.wait().await {
///        Ok(_) => println!("I'm done!"),
///        //You can `Expired::retry` to resume it
///        Err(expired) => println!("Job expired: {}", expired),
///    }
///}
///```
pub struct Timed<F, T=PlatformTimer> {
    timeout: time::Duration,
    inner: F,
    timer: T,
}

impl<F: Future + Unpin> Timed<F> {
    #[inline]
    ///Creates new instance using [Timer](../oneshot/type.Timer.html) alias.
    pub fn platform_new(inner: F, timeout: time::Duration) -> Self {
        Timed::<F, PlatformTimer>::new(inner, timeout)
    }
}

impl<F: Future> Timed<F> {
    #[inline]
    ///Creates new instance using [Timer](../oneshot/type.Timer.html) alias.
    ///
    ///Unsafe version of `platform_new` that doesn't require `Unpin`.
    pub unsafe fn platform_new_unchecked(inner: F, timeout: time::Duration) -> Self {
        Timed::<F, PlatformTimer>::new_unchecked(inner, timeout)
    }
}

impl<F: Future + Unpin, T: Oneshot> Timed<F, T> {
    ///Creates new instance with specified timeout
    ///
    ///Requires to specify `Oneshot` type (e.g. `Timed::<oneshoot::Timer>::new()`)
    pub fn new(inner: F, timeout: time::Duration) -> Self {
        Self {
            timeout,
            inner,
            timer: T::new(timeout),
        }
    }
}

impl<F: Future, T: Oneshot> Timed<F, T> {
    ///Creates new instance with specified timeout
    ///
    ///Unsafe version of `new` that doesn't require `Unpin`.
    ///
    ///Requires to specify `Oneshot` type (e.g. `Timed::<oneshoot::Timer>::new()`)
    pub unsafe fn new_unchecked(inner: F, timeout: time::Duration) -> Self {
        Self {
            timeout,
            inner,
            timer: T::new(timeout),
        }
    }

    ///Waits for future to finish executing within specified timeout.
    pub async fn wait(mut self) -> Result<F::Output, Expired<F, T>> {
        match unsafe { Pin::new_unchecked(&mut self) }.await {
            TimedStatus::Finished(result) => Ok(result),
            TimedStatus::Expired => Err(Expired {
                timed: self,
            }),
        }
    }
}

///Plain result of Timed Future execution.
pub enum TimedStatus<T> {
    ///Future is successfully finished in time.
    Finished(T),
    ///Timed expired before future finished execution.
    Expired,
}

impl<F: Future, T: Oneshot> Future for Timed<F, T> {
    type Output = TimedStatus<F::Output>;

    fn poll(self: Pin<&mut Self>, ctx: &mut task::Context) -> task::Poll<Self::Output> {
        let this = unsafe { self.get_unchecked_mut() };
        match Future::poll(unsafe { Pin::new_unchecked(&mut this.inner) }, ctx) {
            task::Poll::Pending => (),
            task::Poll::Ready(result) => return task::Poll::Ready(TimedStatus::Finished(result)),
        }

        match Future::poll(Pin::new(&mut this.timer), ctx) {
            task::Poll::Pending => task::Poll::Pending,
            task::Poll::Ready(_) => return task::Poll::Ready(TimedStatus::Expired),
        }
    }
}

impl<F: Future + Unpin, T: Oneshot> Unpin for Timed<F, T> {}

///Error when [Timed](struct.Timed.html) expires
///
///Implements `Future` that can be used to restart `Timed`
///Note, that `Oneshot` starts execution immediately after resolving this Future
pub struct Expired<F, T> {
    timed: Timed<F, T>
}

impl<F: Future, T: Oneshot> Expired<F, T> {
    ///Returns underlying `Future`
    pub fn into_inner(self) -> F {
        self.timed.inner
    }

    ///Retries to execute future again with the same timeout
    pub async fn retry(mut self) -> Timed<F, T> {
        unsafe { Pin::new_unchecked(&mut self) }.await;

        self.timed
    }

    ///Retries to execute future again with the new timeout
    pub async fn retry_within(mut self, timeout: time::Duration) -> Timed<F, T> {
        self.timed.timeout = timeout;
        unsafe { Pin::new_unchecked(&mut self) }.await;

        self.timed
    }
}

impl<F: Future, T: Oneshot> Future for Expired<F, T> {
    type Output = ();

    fn poll(self: Pin<&mut Self>, ctx: &mut task::Context) -> task::Poll<Self::Output> {
        let timeout = self.timed.timeout;
        let this = unsafe { self.get_unchecked_mut() };
        this.timed.timer.restart(&timeout, ctx.waker());

        task::Poll::Ready(())
    }
}

impl<F: Future + Unpin, T: Oneshot> Unpin for Expired<F, T> {}

#[cfg(not(feature = "no_std"))]
impl<F, T: Oneshot> std::error::Error for Expired<F, T> {}
impl<F, T: Oneshot> fmt::Debug for Expired<F, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self)
    }
}

impl<F, T: Oneshot> fmt::Display for Expired<F, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.timed.timeout.as_secs() {
            0 => write!(f, "Future expired in {} ms", self.timed.timeout.as_millis()),
            secs => write!(f, "Future expired in {} seconds and {} ms", secs, self.timed.timeout.subsec_millis()),
        }
    }
}