async_std/future/
timeout.rs1use std::error::Error;
2use std::fmt;
3use std::pin::Pin;
4use std::time::Duration;
5use std::future::Future;
6
7use futures_timer::Delay;
8use pin_project_lite::pin_project;
9
10use crate::task::{Context, Poll};
11
12pub async fn timeout<F, T>(dur: Duration, f: F) -> Result<T, TimeoutError>
33where
34 F: Future<Output = T>,
35{
36 let f = TimeoutFuture {
37 future: f,
38 delay: Delay::new(dur),
39 };
40 f.await
41}
42
43pin_project! {
44 pub struct TimeoutFuture<F> {
46 #[pin]
47 future: F,
48 #[pin]
49 delay: Delay,
50 }
51}
52
53impl<F> TimeoutFuture<F> {
54 #[allow(dead_code)]
55 pub(super) fn new(future: F, dur: Duration) -> TimeoutFuture<F> {
56 TimeoutFuture { future: future, delay: Delay::new(dur) }
57 }
58}
59
60impl<F: Future> Future for TimeoutFuture<F> {
61 type Output = Result<F::Output, TimeoutError>;
62
63 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
64 let this = self.project();
65 match this.future.poll(cx) {
66 Poll::Ready(v) => Poll::Ready(Ok(v)),
67 Poll::Pending => match this.delay.poll(cx) {
68 Poll::Ready(_) => Poll::Ready(Err(TimeoutError { _private: () })),
69 Poll::Pending => Poll::Pending,
70 },
71 }
72 }
73}
74
75#[derive(Clone, Copy, Debug, Eq, PartialEq)]
77pub struct TimeoutError {
78 _private: (),
79}
80
81impl Error for TimeoutError {}
82
83impl fmt::Display for TimeoutError {
84 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85 "future has timed out".fmt(f)
86 }
87}