use std::sync::Arc;
use qubit_function::Callable;
use crate::{
TrackedTask,
hook::TaskHook,
service::SubmissionError,
task::spi::TaskEndpointPair,
};
use super::Executor;
#[derive(Clone)]
pub struct DirectExecutor {
hook: Option<Arc<dyn TaskHook>>,
}
impl DirectExecutor {
#[inline]
pub fn new() -> Self {
Self::default()
}
#[inline]
pub fn with_hook(mut self, hook: Arc<dyn TaskHook>) -> Self {
self.hook = Some(hook);
self
}
}
impl Default for DirectExecutor {
#[inline]
fn default() -> Self {
Self { hook: None }
}
}
impl Executor for DirectExecutor {
#[inline]
fn call<C, R, E>(&self, task: C) -> Result<TrackedTask<R, E>, SubmissionError>
where
C: Callable<R, E> + Send + 'static,
R: Send + 'static,
E: Send + 'static,
{
let (handle, slot) =
TaskEndpointPair::with_optional_hook(self.hook.clone()).into_tracked_parts();
handle.accept();
slot.run(task);
Ok(handle)
}
}