1use crate::Executor;
2use core::future::Future;
3use std::sync::Arc;
4
5impl Executor for tokio::runtime::Runtime {
6 fn spawn<T: Send + 'static>(
7 &self,
8 fut: impl Future<Output = T> + Send + 'static,
9 ) -> async_task::Task<T> {
10 spawn_with_handle(tokio::runtime::Runtime::spawn(self, fut))
11 }
12}
13
14impl Executor for Arc<tokio::runtime::Runtime> {
15 fn spawn<T: Send + 'static>(
16 &self,
17 fut: impl Future<Output = T> + Send + 'static,
18 ) -> async_task::Task<T> {
19 spawn_with_handle(self.as_ref().spawn(fut))
20 }
21}
22
23impl Executor for &tokio::runtime::Runtime {
24 fn spawn<T: Send + 'static>(
25 &self,
26 fut: impl Future<Output = T> + Send + 'static,
27 ) -> async_task::Task<T> {
28 spawn_with_handle(tokio::runtime::Runtime::spawn(self, fut))
29 }
30}
31
32fn spawn_with_handle<T: Send + 'static>(handle: tokio::task::JoinHandle<T>) -> async_task::Task<T> {
33 let (runnable, task) = async_task::spawn(
34 async move { handle.await.expect("Tokio task panicked") },
35 |runnable: async_task::Runnable| {
36 tokio::spawn(async move {
37 runnable.run();
38 });
39 },
40 );
41 runnable.schedule();
42 task
43}