#![cfg_attr(windows, allow(dead_code))]
use std::future::Future;
use std::sync::LazyLock;
use std::time::Duration;
use private::Sealded;
use tokio::task::JoinHandle;
use tokio::time::timeout;
static RUNTIME: LazyLock<tokio::runtime::Runtime> = LazyLock::new(|| {
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
});
mod private {
pub trait Sealded {}
impl<F: Future> Sealded for F {}
}
pub trait AmbientRuntime: Future + Sealded {
fn block_on(self) -> Self::Output
where
Self: Sized,
{
RUNTIME.block_on(self)
}
#[allow(dead_code, async_fn_in_trait)] async fn with_timeout(self, t: Duration) -> Option<Self::Output>
where
Self: Sized,
{
timeout(t, self).await.ok()
}
fn spawn(self) -> JoinHandle<Self::Output>
where
Self: Sized + Send + 'static,
Self::Output: Send + 'static,
{
RUNTIME.spawn(self)
}
}
impl<F: Future> AmbientRuntime for F {}