use crate::{
task::*,
time::{RuntimeTime, TimedTaskNotifier},
};
use pi_async_task::join::LocalOutput;
use pi_result::RawResult;
use std::{future::Future, time::Duration};
pub trait TaskDispatch {
type Notifier: TaskNotifier;
fn create_with_config<F, M>(
&self,
future: F,
config: TaskConfig<M>,
) -> RawResult<(UnsubmittedTask, TaskHandle<Self::Notifier, F::Output, M>), CreateError<F, M>>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
M: Send + Sync + 'static;
fn submit(&self, task: UnsubmittedTask) -> RawResult<(), SubmitError>;
fn create<F>(
&self,
future: F,
) -> RawResult<(UnsubmittedTask, TaskHandle<Self::Notifier, F::Output>), CreateError<F, ()>>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
self.create_with_config(future, TaskConfig::default())
}
fn spawn_with_config<F, M>(
&self,
future: F,
config: TaskConfig<M>,
) -> RawResult<TaskHandle<Self::Notifier, F::Output, M>, SpawnError<F, Self::Notifier, M>>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
M: Send + Sync + 'static,
{
let (task, handle) = self
.create_with_config(future, config)
.map_err(SpawnError::Create)?;
match self.submit(task) {
Ok(()) => Ok(handle),
Err(error) => Err(SpawnError::Submit { error, handle }),
}
}
fn spawn<F>(
&self,
future: F,
) -> RawResult<TaskHandle<Self::Notifier, F::Output>, SpawnError<F, Self::Notifier>>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
self.spawn_with_config(future, TaskConfig::default())
}
}
pub trait LocalTaskDispatch: TaskDispatch {
fn create_local_with_config<F, M>(
future: F,
config: TaskConfig<M>,
) -> RawResult<
(
UnsubmittedTask,
TaskHandle<Self::Notifier, F::Output, M, LocalOutput>,
),
CreateError<F, M>,
>
where
F: Future + 'static,
F::Output: 'static,
M: Send + Sync + 'static;
fn submit_local(task: UnsubmittedTask) -> RawResult<(), SubmitError>;
fn create_local<F>(
future: F,
) -> RawResult<
(
UnsubmittedTask,
TaskHandle<Self::Notifier, F::Output, (), LocalOutput>,
),
CreateError<F, ()>,
>
where
F: Future + 'static,
F::Output: 'static,
{
Self::create_local_with_config(future, TaskConfig::default())
}
fn spawn_local_with_config<F, M>(
future: F,
config: TaskConfig<M>,
) -> RawResult<
TaskHandle<Self::Notifier, F::Output, M, LocalOutput>,
SpawnError<F, Self::Notifier, M, LocalOutput>,
>
where
F: Future + 'static,
F::Output: 'static,
M: Send + Sync + 'static,
{
let (task, handle) =
Self::create_local_with_config(future, config).map_err(SpawnError::Create)?;
match Self::submit_local(task) {
Ok(()) => Ok(handle),
Err(error) => Err(SpawnError::Submit { error, handle }),
}
}
fn spawn_local<F>(
future: F,
) -> RawResult<
TaskHandle<Self::Notifier, F::Output, (), LocalOutput>,
SpawnError<F, Self::Notifier, (), LocalOutput>,
>
where
F: Future + 'static,
F::Output: 'static,
{
Self::spawn_local_with_config(future, TaskConfig::default())
}
}
pub trait IdleDispatch: TaskDispatch {
fn submit_idle(&self, task: UnsubmittedTask) -> RawResult<(), SubmitError>;
fn idle_with_config<F, M>(
&self,
future: F,
config: TaskConfig<M>,
) -> RawResult<TaskHandle<Self::Notifier, F::Output, M>, SpawnError<F, Self::Notifier, M>>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
M: Send + Sync + 'static,
{
let (task, handle) = self
.create_with_config(future, config)
.map_err(SpawnError::Create)?;
match self.submit_idle(task) {
Ok(()) => Ok(handle),
Err(error) => Err(SpawnError::Submit { error, handle }),
}
}
fn idle<F>(
&self,
future: F,
) -> RawResult<TaskHandle<Self::Notifier, F::Output>, SpawnError<F, Self::Notifier>>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
self.idle_with_config(future, TaskConfig::default())
}
fn idle_local_with_config<F, M>(
future: F,
config: TaskConfig<M>,
) -> RawResult<TaskHandle<Self::Notifier, F::Output, M>, SpawnError<F, Self::Notifier, M>>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
M: Send + Sync + 'static;
fn idle_local<F>(
future: F,
) -> RawResult<TaskHandle<Self::Notifier, F::Output>, SpawnError<F, Self::Notifier>>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
Self::idle_local_with_config(future, TaskConfig::default())
}
}
pub trait TimedTaskDispatch: TaskDispatch + RuntimeTime {
fn spawn_timing_with_config<F, M>(
&self,
future: F,
delay: Duration,
config: TaskConfig<M>,
) -> RawResult<
TaskHandle<TimedTaskNotifier<Self::Notifier, Self::Timer>, F::Output, M>,
SpawnError<F, TimedTaskNotifier<Self::Notifier, Self::Timer>, M>,
>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
M: Send + Sync + 'static;
fn spawn_timing<F>(
&self,
future: F,
delay: Duration,
) -> RawResult<
TaskHandle<TimedTaskNotifier<Self::Notifier, Self::Timer>, F::Output>,
SpawnError<F, TimedTaskNotifier<Self::Notifier, Self::Timer>>,
>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
self.spawn_timing_with_config(future, delay, TaskConfig::default())
}
fn spawn_timing_local_with_config<F, M>(
future: F,
delay: Duration,
config: TaskConfig<M>,
) -> RawResult<
TaskHandle<TimedTaskNotifier<Self::Notifier, Self::Timer>, F::Output, M, LocalOutput>,
SpawnError<F, TimedTaskNotifier<Self::Notifier, Self::Timer>, M, LocalOutput>,
>
where
F: Future + 'static,
F::Output: 'static,
M: Send + Sync + 'static;
fn spawn_timing_local<F>(
future: F,
delay: Duration,
) -> RawResult<
TaskHandle<TimedTaskNotifier<Self::Notifier, Self::Timer>, F::Output, (), LocalOutput>,
SpawnError<F, TimedTaskNotifier<Self::Notifier, Self::Timer>, (), LocalOutput>,
>
where
F: Future + 'static,
F::Output: 'static,
{
Self::spawn_timing_local_with_config(future, delay, TaskConfig::default())
}
}