1use super::*;
2
3cfg_if! {
4 if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] {
5 use futures_util::future::{select, Either};
6
7 pub async fn timeout<F, T>(dur_ms: u32, f: F) -> Result<T, TimeoutError>
8 where
9 F: Future<Output = T>,
10 {
11 let tout = select(Box::pin(sleep(dur_ms)), Box::pin(f));
12
13 match tout.await {
14 Either::Left((_x, _b)) => Err(TimeoutError()),
15 Either::Right((y, _a)) => Ok(y),
16 }
17 }
18
19 } else {
20
21 pub async fn timeout<F, T>(dur_ms: u32, f: F) -> Result<T, TimeoutError>
22 where
23 F: Future<Output = T>,
24 {
25 cfg_if! {
26 if #[cfg(feature="rt-async-std")] {
27 let tout = async_std::future::timeout(Duration::from_millis(dur_ms as u64), f);
28 } else if #[cfg(feature="rt-tokio")] {
29 let tout = tokio::time::timeout(Duration::from_millis(dur_ms as u64), f);
30 }
31 }
32
33 tout.await.map_err(|e| e.into())
34 }
35
36 }
37}