use std::{future::Future, time::Duration};
#[cfg(feature = "async-std")]
pub async fn async_std_sleep(time: Duration) {
async_std::task::sleep(time).await
}
#[cfg(feature = "tokio")]
pub async fn tokio_sleep(time: Duration) {
tokio::time::sleep(time).await
}
pub async fn std_sleep(time: Duration) {
blocking::unblock(move || std::thread::sleep(time)).await
}
#[cfg(feature = "async-std")]
pub async fn sleep(time: Duration) {
async_std_sleep(time).await
}
#[cfg(all(feature = "tokio", not(feature = "async-std")))]
pub async fn sleep(time: Duration) {
tokio_sleep(time).await
}
#[cfg(not(any(feature = "tokio", feature = "async-std")))]
pub async fn sleep(time: Duration) {
std_sleep(time).await
}
pub async fn with_timeout<T>(
inner_future: impl Future<Output = T>,
timeout: Duration,
) -> Option<T> {
use crate::future::FutureExt;
use crate::mapfut::map_fut;
map_fut(inner_future, Some)
.or(map_fut(sleep(timeout), |_| None))
.await
}