spawn-std-async-std 0.1.0-alpha.1

async-std implementation for spawn-std
Documentation
use std::convert::Infallible;
use std::pin::Pin;
use std::task::{Context, Poll};

use async_std::task::JoinHandle;
use spawn_std_core::{Executor, Handle};

pub struct AsyncStdExecutor;

impl Executor for AsyncStdExecutor {
    type Handle<T: Send + 'static> = AsyncStdHandle<T>;

    fn spawn<T, F>(future: F) -> Self::Handle<T>
    where
        T: Send + 'static,
        F: Future<Output = T> + Send + 'static,
    {
        AsyncStdHandle(async_std::task::spawn(future))
    }
}

pub struct AsyncStdHandle<T>(JoinHandle<T>);

impl<T> AsyncStdHandle<T> {
    #[inline]
    fn inner_pin_mut(self: Pin<&mut Self>) -> Pin<&mut JoinHandle<T>> {
        unsafe { self.map_unchecked_mut(|x| &mut x.0) }
    }
}

impl<T> Future for AsyncStdHandle<T> {
    type Output = Result<T, Infallible>;

    #[inline]
    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        self.inner_pin_mut().poll(cx).map(Ok)
    }
}

impl<T> Handle<T> for AsyncStdHandle<T> {
    type Error = Infallible;

    #[inline(always)]
    fn abort(self) {
        drop(self.0);
    }
}