aws_smithy_async/future/timeout.rs
1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6// This code was copied and then modified from Tokio.
7
8/*
9 * Copyright (c) 2021 Tokio Contributors
10 *
11 * Permission is hereby granted, free of charge, to any
12 * person obtaining a copy of this software and associated
13 * documentation files (the "Software"), to deal in the
14 * Software without restriction, including without
15 * limitation the rights to use, copy, modify, merge,
16 * publish, distribute, sublicense, and/or sell copies of
17 * the Software, and to permit persons to whom the Software
18 * is furnished to do so, subject to the following
19 * conditions:
20 *
21 * The above copyright notice and this permission notice
22 * shall be included in all copies or substantial portions
23 * of the Software.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
26 * ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
27 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
28 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
29 * SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
30 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
32 * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33 * DEALINGS IN THE SOFTWARE.
34 */
35
36//! Provides the [`Timeout`] future for adding a timeout to another future.
37
38use pin_project_lite::pin_project;
39use std::error::Error;
40use std::fmt;
41use std::future::Future;
42use std::pin::Pin;
43use std::task::{Context, Poll};
44
45/// Error returned when [`Timeout`] times out
46#[derive(Debug)]
47pub struct TimedOutError;
48
49impl Error for TimedOutError {}
50
51impl fmt::Display for TimedOutError {
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53 write!(f, "timed out")
54 }
55}
56
57pin_project! {
58 /// Timeout Future
59 #[non_exhaustive]
60 #[must_use = "futures do nothing unless you `.await` or poll them"]
61 #[derive(Debug)]
62 pub struct Timeout<T, S> {
63 #[pin]
64 value: T,
65 #[pin]
66 sleep: S,
67 }
68}
69
70impl<T, S> Timeout<T, S> {
71 /// Create a new future that will race `value` and `sleep`.
72 ///
73 /// If `sleep` resolves first, a timeout error is returned. Otherwise, the value is returned.
74 pub fn new(value: T, sleep: S) -> Timeout<T, S> {
75 Timeout { value, sleep }
76 }
77}
78
79impl<T, S> Future for Timeout<T, S>
80where
81 T: Future,
82 S: Future,
83{
84 type Output = Result<T::Output, TimedOutError>;
85
86 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
87 let me = self.project();
88
89 // First, try polling the future
90 if let Poll::Ready(v) = me.value.poll(cx) {
91 return Poll::Ready(Ok(v));
92 }
93
94 // Now check the timer
95 match me.sleep.poll(cx) {
96 Poll::Ready(_) => Poll::Ready(Err(TimedOutError)),
97 Poll::Pending => Poll::Pending,
98 }
99 }
100}
101
102#[cfg(test)]
103mod tests {
104 use super::{TimedOutError, Timeout};
105 use crate::future::never::Never;
106
107 #[tokio::test]
108 async fn success() {
109 assert!(matches!(
110 Timeout::new(async { Ok::<isize, isize>(5) }, Never).await,
111 Ok(Ok(5))
112 ));
113 }
114
115 #[tokio::test]
116 async fn failure() {
117 assert!(matches!(
118 Timeout::new(async { Err::<isize, isize>(0) }, Never).await,
119 Ok(Err(0))
120 ));
121 }
122
123 #[tokio::test]
124 async fn timeout() {
125 assert!(matches!(
126 Timeout::new(Never, async {}).await,
127 Err(TimedOutError)
128 ));
129 }
130
131 // If the value is available at the same time as the timeout, then return the value
132 #[tokio::test]
133 async fn prefer_value_to_timeout() {
134 assert!(matches!(Timeout::new(async { 5 }, async {}).await, Ok(5)));
135 }
136}