use crate::{
functions::macros::impl_function_debug_display,
macros::{
impl_common_name_methods,
impl_common_new_methods,
},
tasks::runnable_with::RunnableWith,
};
use crate::tasks::callable_with::BoxCallableWith;
type BoxRunnableWithFn<T, E> = Box<dyn FnMut(&mut T) -> Result<(), E> + Send>;
#[must_use = "callback wrappers do nothing unless stored or invoked"]
pub struct BoxRunnableWith<T, E> {
pub(super) function: BoxRunnableWithFn<T, E>,
pub(super) metadata: crate::internal::CallbackMetadata,
}
impl<T, E> BoxRunnableWith<T, E> {
impl_common_new_methods!(
semantic_mut(RunnableWith<T, E> + Send + 'static),
|source| move |input: &mut T| source.run_with(input),
|function| Box::new(function),
"runnable-with"
);
impl_common_name_methods!("runnable-with");
#[inline]
pub fn and_then<N>(self, next: N) -> BoxRunnableWith<T, E>
where
N: RunnableWith<T, E> + Send + 'static,
T: 'static,
E: 'static,
{
let mut function = self.function;
let mut next = next;
BoxRunnableWith::new(move |input: &mut T| {
function(&mut *input)?;
next.run_with(input)
})
}
#[inline]
pub fn then_callable_with<R, C>(
self,
callable: C,
) -> BoxCallableWith<T, R, E>
where
C: crate::tasks::callable_with::CallableWith<T, R, E> + Send + 'static,
T: 'static,
R: 'static,
E: 'static,
{
let mut function = self.function;
let mut callable = callable;
BoxCallableWith::new(move |input: &mut T| {
function(&mut *input)?;
callable.call_with(input)
})
}
}
impl<T, E> RunnableWith<T, E> for BoxRunnableWith<T, E> {
#[inline(always)]
fn run_with(&mut self, input: &mut T) -> Result<(), E> {
(self.function)(input)
}
}
impl_function_debug_display!(BoxRunnableWith<T, E>);