use crate::util::{Task, TaskImpl};
use std::{future::Future, ops::Deref};
pub trait Executor {
type Task<T: Send + 'static>: TaskImpl<Output = T> + Send + 'static;
fn block_on<T, F: Future<Output = T>>(&self, f: F) -> T
where
Self: Sized;
fn spawn<T: Send + 'static, F: Future<Output = T> + Send + 'static>(
&self,
f: F,
) -> Task<Self::Task<T>>
where
Self: Sized;
fn spawn_blocking<T: Send + 'static, F: FnOnce() -> T + Send + 'static>(
&self,
f: F,
) -> Task<Self::Task<T>>
where
Self: Sized;
}
impl<E: Deref> Executor for E
where
E::Target: Executor + Sized,
{
type Task<T: Send + 'static> = <E::Target as Executor>::Task<T>;
fn block_on<T, F: Future<Output = T>>(&self, f: F) -> T {
self.deref().block_on(f)
}
fn spawn<T: Send + 'static, F: Future<Output = T> + Send + 'static>(
&self,
f: F,
) -> Task<Self::Task<T>> {
self.deref().spawn(f)
}
fn spawn_blocking<T: Send + 'static, F: FnOnce() -> T + Send + 'static>(
&self,
f: F,
) -> Task<Self::Task<T>> {
self.deref().spawn_blocking(f)
}
}