veilid/veilid-tools/src/timeout.rs

33 lines
974 B
Rust
Raw Normal View History

2022-11-27 02:37:23 +00:00
use super::*;
cfg_if! {
if #[cfg(target_arch = "wasm32")] {
pub async fn timeout<F, T>(dur_ms: u32, f: F) -> Result<T, TimeoutError>
where
F: Future<Output = T>,
{
2022-11-27 15:52:07 +00:00
match select(Box::pin(sleep(dur_ms)), Box::pin(f)).await {
2022-11-27 02:37:23 +00: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")] {
async_std::future::timeout(Duration::from_millis(dur_ms as u64), f).await.map_err(|e| e.into())
} else if #[cfg(feature="rt-tokio")] {
tokio::time::timeout(Duration::from_millis(dur_ms as u64), f).await.map_err(|e| e.into())
}
}
}
}
}