pub trait AsyncRuntime {
type YieldNowFuture: core::future::Future<Output = ()>;
fn yield_now(&self) -> Self::YieldNowFuture;
}
#[cfg(feature = "runtime-tokio")]
#[derive(Debug, Default)]
pub struct TokioRuntime;
#[cfg(feature = "runtime-tokio")]
impl AsyncRuntime for TokioRuntime {
type YieldNowFuture =
core::pin::Pin<alloc::boxed::Box<dyn core::future::Future<Output = ()> + Send>>;
fn yield_now(&self) -> Self::YieldNowFuture {
alloc::boxed::Box::pin(tokio::task::yield_now())
}
}
#[doc(inline)]
pub use unknown_runtime::UnknownRuntime;
#[cfg(feature = "runtime-tokio")]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub type DefaultRuntime = TokioRuntime;
#[cfg(not(feature = "runtime-tokio"))]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub type DefaultRuntime = UnknownRuntime;
pub mod unknown_runtime {
#[derive(Debug, Default)]
pub struct UnknownRuntime;
impl super::AsyncRuntime for UnknownRuntime {
type YieldNowFuture = YieldNow;
fn yield_now(&self) -> Self::YieldNowFuture {
YieldNow(false)
}
}
pub struct YieldNow(bool);
impl core::future::Future for YieldNow {
type Output = ();
fn poll(
mut self: core::pin::Pin<&mut Self>,
cx: &mut core::task::Context<'_>,
) -> core::task::Poll<Self::Output> {
if !self.0 {
self.0 = true;
cx.waker().wake_by_ref();
core::task::Poll::Pending
} else {
core::task::Poll::Ready(())
}
}
}
}