|
| 1 | +// Copyright 2024 FastLabs Developers |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use std::future::Future; |
| 16 | +use std::future::IntoFuture; |
| 17 | +use std::task::Context; |
| 18 | +use std::task::Poll; |
| 19 | +use std::time::Duration; |
| 20 | +use std::time::Instant; |
| 21 | + |
| 22 | +use crate::MakeDelay; |
| 23 | + |
| 24 | +/// Errors returned by [`Timeout`]. |
| 25 | +/// |
| 26 | +/// This error is returned when a timeout expires before the function was able |
| 27 | +/// to finish. |
| 28 | +#[derive(Debug, PartialEq, Eq)] |
| 29 | +pub struct Elapsed(()); |
| 30 | + |
| 31 | +/// A future that completes when the inner future completes or when the timeout expires. |
| 32 | +/// |
| 33 | +/// Created by [`timeout`] or [`timeout_at`]. |
| 34 | +#[must_use = "futures do nothing unless you `.await` or poll them"] |
| 35 | +#[derive(Debug)] |
| 36 | +#[pin_project::pin_project] |
| 37 | +pub struct Timeout<T, D> { |
| 38 | + #[pin] |
| 39 | + value: T, |
| 40 | + #[pin] |
| 41 | + delay: D, |
| 42 | +} |
| 43 | + |
| 44 | +impl<T, D> Timeout<T, D> { |
| 45 | + /// Gets a reference to the underlying value in this timeout. |
| 46 | + pub fn get(&self) -> &T { |
| 47 | + &self.value |
| 48 | + } |
| 49 | + |
| 50 | + /// Gets a mutable reference to the underlying value in this timeout. |
| 51 | + pub fn get_mut(&mut self) -> &mut T { |
| 52 | + &mut self.value |
| 53 | + } |
| 54 | + |
| 55 | + /// Consumes this timeout, returning the underlying value. |
| 56 | + pub fn into_inner(self) -> T { |
| 57 | + self.value |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +impl<T, D> Future for Timeout<T, D> |
| 62 | +where |
| 63 | + T: Future, |
| 64 | + D: Future<Output = ()>, |
| 65 | +{ |
| 66 | + type Output = Result<T::Output, Elapsed>; |
| 67 | + |
| 68 | + fn poll(self: std::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 69 | + let this = self.project(); |
| 70 | + |
| 71 | + if let Poll::Ready(v) = this.value.poll(cx) { |
| 72 | + return Poll::Ready(Ok(v)); |
| 73 | + } |
| 74 | + |
| 75 | + match this.delay.poll(cx) { |
| 76 | + Poll::Ready(()) => Poll::Ready(Err(Elapsed(()))), |
| 77 | + Poll::Pending => Poll::Pending, |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/// Requires a `Future` to complete before the specified duration has elapsed. |
| 83 | +pub fn timeout<F, D>( |
| 84 | + duration: Duration, |
| 85 | + future: F, |
| 86 | + make_delay: &D, |
| 87 | +) -> Timeout<F::IntoFuture, D::Delay> |
| 88 | +where |
| 89 | + F: IntoFuture, |
| 90 | + D: MakeDelay + ?Sized, |
| 91 | +{ |
| 92 | + let delay = make_delay.delay(duration); |
| 93 | + Timeout { |
| 94 | + value: future.into_future(), |
| 95 | + delay, |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +/// Requires a `Future` to complete before the specified instant in time. |
| 100 | +pub fn timeout_at<F, D>( |
| 101 | + deadline: Instant, |
| 102 | + future: F, |
| 103 | + make_delay: &D, |
| 104 | +) -> Timeout<F::IntoFuture, D::Delay> |
| 105 | +where |
| 106 | + F: IntoFuture, |
| 107 | + D: MakeDelay + ?Sized, |
| 108 | +{ |
| 109 | + let delay = make_delay.delay_util(deadline); |
| 110 | + Timeout { |
| 111 | + value: future.into_future(), |
| 112 | + delay, |
| 113 | + } |
| 114 | +} |
0 commit comments