async_rs/traits/
executor.rs1use async_trait::async_trait;
4use std::{future::Future, ops::Deref};
5
6pub trait Executor {
8 fn block_on<T, F: Future<Output = T>>(&self, f: F) -> T
10 where
11 Self: Sized;
12
13 fn spawn<T: Send + 'static>(&self, f: impl Future<Output = T> + Send + 'static) -> impl Task<T>
15 where
16 Self: Sized;
17
18 fn spawn_blocking<F: FnOnce() -> T + Send + 'static, T: Send + 'static>(
20 &self,
21 f: F,
22 ) -> impl Task<T>
23 where
24 Self: Sized;
25}
26
27impl<E: Deref> Executor for E
28where
29 E::Target: Executor + Sized,
30{
31 fn block_on<T, F: Future<Output = T>>(&self, f: F) -> T {
32 self.deref().block_on(f)
33 }
34
35 fn spawn<T: Send + 'static>(
36 &self,
37 f: impl Future<Output = T> + Send + 'static,
38 ) -> impl Task<T> {
39 self.deref().spawn(f)
40 }
41
42 fn spawn_blocking<F: FnOnce() -> T + Send + 'static, T: Send + 'static>(
43 &self,
44 f: F,
45 ) -> impl Task<T> {
46 self.deref().spawn_blocking(f)
47 }
48}
49
50#[async_trait(?Send)]
52pub trait Task<T>: Future<Output = T> + Send {
53 async fn cancel(&mut self) -> Option<T>;
58}