2022-11-26 21:37:23 -05:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
cfg_if! {
|
2025-02-10 03:06:41 +00:00
|
|
|
if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] {
|
2023-07-02 16:23:04 -05:00
|
|
|
use futures_util::future::{select, Either};
|
2022-11-26 21:37:23 -05:00
|
|
|
|
|
|
|
pub async fn timeout<F, T>(dur_ms: u32, f: F) -> Result<T, TimeoutError>
|
|
|
|
where
|
|
|
|
F: Future<Output = T>,
|
|
|
|
{
|
2024-07-21 21:15:54 -04:00
|
|
|
let tout = select(Box::pin(sleep(dur_ms)), Box::pin(f));
|
|
|
|
|
|
|
|
match tout.await {
|
2022-11-26 21:37:23 -05:00
|
|
|
Either::Left((_x, _b)) => Err(TimeoutError()),
|
|
|
|
Either::Right((y, _a)) => Ok(y),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
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")] {
|
2024-07-21 21:15:54 -04:00
|
|
|
let tout = async_std::future::timeout(Duration::from_millis(dur_ms as u64), f);
|
2022-11-26 21:37:23 -05:00
|
|
|
} else if #[cfg(feature="rt-tokio")] {
|
2024-07-21 21:15:54 -04:00
|
|
|
let tout = tokio::time::timeout(Duration::from_millis(dur_ms as u64), f);
|
2022-11-26 21:37:23 -05:00
|
|
|
}
|
|
|
|
}
|
2024-07-21 21:15:54 -04:00
|
|
|
|
|
|
|
tout.await.map_err(|e| e.into())
|
2022-11-26 21:37:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|