|
| 1 | +//! async-global-executor implementation of async runtime definition traits |
| 2 | +
|
| 3 | +use crate::executor::{Executor, Task}; |
| 4 | +use alloc::boxed::Box; |
| 5 | +use async_trait::async_trait; |
| 6 | +use core::{ |
| 7 | + future::Future, |
| 8 | + pin::Pin, |
| 9 | + task::{Context, Poll}, |
| 10 | +}; |
| 11 | + |
| 12 | +/// Dummy object implementing executor-trait common interfaces on top of async-global-executor |
| 13 | +#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] |
| 14 | +pub struct AsyncGlobalExecutor; |
| 15 | + |
| 16 | +struct AGETask<T>(Option<async_global_executor::Task<T>>); |
| 17 | + |
| 18 | +impl Executor for AsyncGlobalExecutor { |
| 19 | + fn block_on<T>(&self, f: Pin<Box<dyn Future<Output = T>>>) -> T { |
| 20 | + async_global_executor::block_on(f) |
| 21 | + } |
| 22 | + |
| 23 | + fn spawn<T: Send + 'static>( |
| 24 | + &self, |
| 25 | + f: impl Future<Output = T> + Send + 'static, |
| 26 | + ) -> impl Task<T> { |
| 27 | + AGETask(Some(async_global_executor::spawn(f))) |
| 28 | + } |
| 29 | + |
| 30 | + fn spawn_blocking<F: FnOnce() -> T + Send + 'static, T: Send + 'static>( |
| 31 | + &self, |
| 32 | + f: F, |
| 33 | + ) -> impl Task<T> { |
| 34 | + AGETask(Some(async_global_executor::spawn_blocking(f))) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +#[async_trait(?Send)] |
| 39 | +impl<T> Task<T> for AGETask<T> { |
| 40 | + async fn cancel(&mut self) -> Option<T> { |
| 41 | + self.0.take()?.cancel().await |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl<T> Drop for AGETask<T> { |
| 46 | + fn drop(&mut self) { |
| 47 | + if let Some(task) = self.0.take() { |
| 48 | + task.detach(); |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +impl<T> Future for AGETask<T> { |
| 54 | + type Output = T; |
| 55 | + |
| 56 | + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 57 | + Pin::new(self.0.as_mut().expect("task canceled")).poll(cx) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +#[cfg(test)] |
| 62 | +mod tests { |
| 63 | + use super::*; |
| 64 | + use alloc::string::String; |
| 65 | + |
| 66 | + #[test] |
| 67 | + fn dyn_compat() { |
| 68 | + struct Test { |
| 69 | + _executor: Box<dyn Executor>, |
| 70 | + _task: Box<dyn Task<String>>, |
| 71 | + } |
| 72 | + |
| 73 | + let _ = Test { |
| 74 | + _executor: Box::new(AsyncGlobalExecutor::default()), |
| 75 | + _task: Box::new(AGETask(None)), |
| 76 | + }; |
| 77 | + } |
| 78 | +} |
0 commit comments