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
use super::*;
cfg_if! {
if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] {
use futures_util::future::{select, Either};
/// Await `f`, failing with [`TimeoutError`] if it does not complete within `dur_ms` milliseconds.
///
/// Abstracts over the async runtime (tokio, async-std, wasm).
pub async fn timeout<F, T>(dur_ms: u32, f: F) -> Result<T, TimeoutError>
where
F: Future<Output = T>,
{
let tout = select(Box::pin(sleep(dur_ms)), Box::pin(f));
match tout.await {
Either::Left((_x, _b)) => Err(TimeoutError()),
Either::Right((y, _a)) => Ok(y),
}
}
} else {
/// Await `f`, failing with [`TimeoutError`] if it does not complete within `dur_ms` milliseconds.
///
/// Abstracts over the async runtime (tokio, async-std, wasm).
pub async fn timeout<F, T>(dur_ms: u32, f: F) -> Result<T, TimeoutError>
where
F: Future<Output = T>,
{
cfg_if! {
if #[cfg(feature="rt-async-std")] {
let tout = async_std::future::timeout(Duration::from_millis(dur_ms as u64), f);
} else if #[cfg(feature="rt-tokio")] {
let tout = tokio::time::timeout(Duration::from_millis(dur_ms as u64), f);
}
}
tout.await.map_err(|e| e.into())
}
}
}