use std::ops::Deref;
use futures::future::BoxFuture;
use crate::runtime::AbortHandleRef;
use crate::runtime::Executor;
use crate::runtime::abort::TaskAbortHandle;
use crate::runtime::blocking_pool::BlockingPool;
pub(crate) struct SmolExecutor {
executor: smol::Executor<'static>,
blocking_pool: BlockingPool,
}
impl Default for SmolExecutor {
fn default() -> Self {
Self {
executor: smol::Executor::new(),
blocking_pool: BlockingPool::default(),
}
}
}
impl SmolExecutor {
pub(crate) fn async_executor(&self) -> &smol::Executor<'static> {
&self.executor
}
}
impl Deref for SmolExecutor {
type Target = smol::Executor<'static>;
fn deref(&self) -> &Self::Target {
&self.executor
}
}
impl Executor for SmolExecutor {
fn spawn(&self, fut: BoxFuture<'static, ()>) -> AbortHandleRef {
TaskAbortHandle::new_handle(self.executor.spawn(fut))
}
fn spawn_cpu(&self, task: Box<dyn FnOnce() + Send + 'static>) -> AbortHandleRef {
TaskAbortHandle::new_handle(self.executor.spawn(async move { task() }))
}
fn spawn_blocking_io(&self, task: Box<dyn FnOnce() + Send + 'static>) -> AbortHandleRef {
self.blocking_pool.spawn(task)
}
}