use futures::future::BoxFuture;
use crate::runtime::AbortHandle;
use crate::runtime::AbortHandleRef;
use crate::runtime::Executor;
impl Executor for smol::Executor<'static> {
fn spawn(&self, fut: BoxFuture<'static, ()>) -> AbortHandleRef {
SmolAbortHandle::new_handle(smol::Executor::spawn(self, fut))
}
fn spawn_cpu(&self, task: Box<dyn FnOnce() + Send + 'static>) -> AbortHandleRef {
SmolAbortHandle::new_handle(smol::Executor::spawn(self, async move { task() }))
}
fn spawn_blocking_io(&self, task: Box<dyn FnOnce() + Send + 'static>) -> AbortHandleRef {
SmolAbortHandle::new_handle(smol::unblock(task))
}
}
pub(crate) struct SmolAbortHandle<T> {
task: Option<smol::Task<T>>,
}
impl<T: 'static + Send> SmolAbortHandle<T> {
pub(crate) fn new_handle(task: smol::Task<T>) -> AbortHandleRef {
Box::new(Self { task: Some(task) })
}
}
impl<T: Send> AbortHandle for SmolAbortHandle<T> {
fn abort(mut self: Box<Self>) {
drop(self.task.take());
}
}
impl<T> Drop for SmolAbortHandle<T> {
fn drop(&mut self) {
if let Some(task) = self.task.take() {
task.detach()
}
}
}