use super::builder::Mapped;
use super::id_and_context::TaskContext;
use super::traits::Task;
pub trait TaskExt: Task + Sized {
fn map<F, R>(self, mapper: F) -> Mapped<Self, F>
where
F: FnOnce(Self::Output) -> R,
{
Mapped::new(self, mapper)
}
fn with_context(self, context: TaskContext) -> ContextualTask<Self> {
ContextualTask::new(self, context)
}
}
impl<T: Task> TaskExt for T {}
pub struct ContextualTask<T> {
task: T,
context: TaskContext,
}
impl<T: Task> ContextualTask<T> {
pub fn new(task: T, context: TaskContext) -> Self {
Self { task, context }
}
}
impl<T: Task> Task for ContextualTask<T> {
type Output = T::Output;
fn execute(self) -> Self::Output {
self.task.execute()
}
fn context(&self) -> &TaskContext {
&self.context
}
}