yaaral 0.5.3

yet another async runtime abstraction library
Documentation
//! Support for compatibility between runtimes.

#[cfg(feature = "bevy_runtime_018")]
use bevy_tasks_018 as bevy_tasks;

/// Interface for compatibiility between runtimes. Primarly intented to allow
/// using library relying on tokio with future and smol executor.
pub trait CompatInterface: crate::TaskInterface {
    /// Drive a future in a compatibility layer.
    fn tokio_compat<T>(f: impl Future<Output = T> + Send) -> impl Future<Output = T> + Send;

    /// Spawn a task using tokio compatibility layer.
    fn spawn_tokio_task<F, T>(
        &self,
        future: F,
    ) -> Result<crate::utils::TaskHandle<Self::TaskHandle<F::Output>>, Self::SpawnError>
    where
        F: Future<Output = T> + Send + 'static,
        T: Send + 'static,
    {
        self.spawn_task(Self::tokio_compat(future))
    }
}

#[cfg(feature = "futures_runtime")]
impl CompatInterface for crate::futures::Runtime {
    fn tokio_compat<T>(f: impl Future<Output = T> + Send) -> impl Future<Output = T> + Send {
        async_compat::Compat::new(f)
    }
}

#[cfg(feature = "tokio_runtime")]
impl CompatInterface for crate::tokio::Runtime {
    fn tokio_compat<T>(f: impl Future<Output = T> + Send) -> impl Future<Output = T> + Send {
        f
    }
}

#[cfg(any(feature = "bevy_runtime", feature = "bevy_runtime_018"))]
impl<TP> CompatInterface for crate::bevy::Runtime<TP>
where
    TP: std::ops::Deref<Target = bevy_tasks::TaskPool> + Sync + 'static,
{
    fn tokio_compat<T>(f: impl Future<Output = T> + Send) -> impl Future<Output = T> + Send {
        async_compat::Compat::new(f)
    }
}