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