spawn_std_async_std/
lib.rs

1use std::convert::Infallible;
2use std::pin::Pin;
3use std::task::{Context, Poll};
4
5use async_std::task::JoinHandle;
6use spawn_std_core::{Executor, Handle};
7
8pub struct AsyncStdExecutor;
9
10impl Executor for AsyncStdExecutor {
11    type Handle<T: Send + 'static> = AsyncStdHandle<T>;
12
13    fn spawn<T, F>(future: F) -> Self::Handle<T>
14    where
15        T: Send + 'static,
16        F: Future<Output = T> + Send + 'static,
17    {
18        AsyncStdHandle(async_std::task::spawn(future))
19    }
20}
21
22pub struct AsyncStdHandle<T>(JoinHandle<T>);
23
24impl<T> AsyncStdHandle<T> {
25    #[inline]
26    fn inner_pin_mut(self: Pin<&mut Self>) -> Pin<&mut JoinHandle<T>> {
27        unsafe { self.map_unchecked_mut(|x| &mut x.0) }
28    }
29}
30
31impl<T> Future for AsyncStdHandle<T> {
32    type Output = Result<T, Infallible>;
33
34    #[inline]
35    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
36        self.inner_pin_mut().poll(cx).map(Ok)
37    }
38}
39
40impl<T> Handle<T> for AsyncStdHandle<T> {
41    type Error = Infallible;
42
43    #[inline(always)]
44    fn abort(self) {
45        drop(self.0);
46    }
47}