Skip to main content

async_rt/
arc.rs

1use crate::error::TimeoutError;
2use crate::{
3    AbortableJoinHandle, Executor, ExecutorBlockOn, ExecutorBlocking, ExecutorTimeout, JoinHandle,
4};
5use std::future::Future;
6use std::sync::Arc;
7use std::time::Duration;
8
9impl<E> Executor for Arc<E>
10where
11    E: Executor,
12{
13    fn runtime_type(&self) -> Option<&'static str> {
14        (**self).runtime_type()
15    }
16
17    fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
18    where
19        F: Future + Send + 'static,
20        F::Output: Send + 'static,
21    {
22        (**self).spawn(future)
23    }
24}
25
26impl<E> ExecutorBlocking for Arc<E>
27where
28    E: ExecutorBlocking,
29{
30    fn spawn_blocking<F, T>(&self, future: F) -> JoinHandle<T>
31    where
32        F: FnOnce() -> T + Send + 'static,
33        T: Send + 'static,
34    {
35        (**self).spawn_blocking(future)
36    }
37
38    fn spawn_blocking_abortable<F, R>(&self, f: F) -> AbortableJoinHandle<R>
39    where
40        F: FnOnce() -> R + Send + 'static,
41        R: Send + 'static,
42    {
43        (**self).spawn_blocking_abortable(f)
44    }
45}
46
47impl<E> ExecutorTimeout for Arc<E>
48where
49    E: ExecutorTimeout,
50{
51    fn spawn_timeout<F>(
52        &self,
53        duration: std::time::Duration,
54        f: F,
55    ) -> JoinHandle<Result<F::Output, TimeoutError>>
56    where
57        F: Future + Send + 'static,
58        F::Output: Send + 'static,
59    {
60        (**self).spawn_timeout(duration, f)
61    }
62
63    fn spawn_delay<F>(&self, duration: Duration, f: F) -> JoinHandle<F::Output>
64    where
65        F: Future + Send + 'static,
66        F::Output: Send + 'static,
67    {
68        (**self).spawn_delay(duration, f)
69    }
70
71    fn spawn_abortable_timeout<F>(
72        &self,
73        duration: std::time::Duration,
74        f: F,
75    ) -> AbortableJoinHandle<Result<F::Output, TimeoutError>>
76    where
77        F: Future + Send + 'static,
78        F::Output: Send + 'static,
79    {
80        (**self).spawn_abortable_timeout(duration, f)
81    }
82
83    fn spawn_abortable_delay<F>(&self, duration: Duration, f: F) -> AbortableJoinHandle<F::Output>
84    where
85        F: Future + Send + 'static,
86        F::Output: Send + 'static,
87    {
88        (**self).spawn_abortable_delay(duration, f)
89    }
90}
91
92impl<E> ExecutorBlockOn for Arc<E>
93where
94    E: ExecutorBlockOn,
95{
96    fn block_on<F: Future>(&self, f: F) -> F::Output {
97        (**self).block_on(f)
98    }
99}