use std::{convert::Infallible, future::Future};
use async_trait::async_trait;
use futures::future::BoxFuture;
#[cfg(feature = "async-std")]
pub mod asyncstd;
pub mod threads;
#[cfg(feature = "async-std")]
pub use asyncstd::AsyncStd;
pub use threads::Threads;
use crate::types::Either;
#[async_trait]
pub trait AsyncJoin {
type Item;
type Error: std::error::Error + 'static;
async fn async_join(self) -> Result<Self::Item, Self::Error>;
}
#[async_trait]
impl<T> AsyncJoin for BoxFuture<'static, T>
where
T: Send,
{
type Item = T;
type Error = Infallible;
async fn async_join(self) -> Result<Self::Item, Self::Error> {
Ok(self.await)
}
}
pub trait SyncJoin {
type Item;
type Error: std::error::Error + 'static;
fn sync_join(self) -> Result<Self::Item, Self::Error>;
fn to_async(self) -> Either<Box<dyn AsyncJoin<Item = Self::Item, Error = Self::Error>>, Self>
where
Self: Sized,
{
Either::Sad(self)
}
}
pub trait Executor: Send + Sync + 'static {
type AsyncJoin<T: Send + 'static>: AsyncJoin<Item = T>;
type SyncJoin<T: Send + 'static>: SyncJoin<Item = T>;
fn spawn_async<T, F>(future: F) -> Self::AsyncJoin<T>
where
T: Send + Sync + 'static,
F: Future<Output = T> + Send + 'static;
fn spawn_sync<T, C>(closure: C) -> Self::SyncJoin<T>
where
T: Send + Sync + 'static,
C: FnOnce() -> T + Send + 'static;
}