#![allow(unused_imports)]
use super::*;
pub struct BoxRunnable<E> {
pub(super) function: Box<dyn FnMut() -> Result<(), E>>,
pub(super) name: Option<String>,
}
impl<E> BoxRunnable<E> {
impl_common_new_methods!(
(FnMut() -> Result<(), E> + 'static),
|function| Box::new(function),
"runnable"
);
#[inline]
pub fn from_supplier<S>(supplier: S) -> Self
where
S: Supplier<Result<(), E>> + 'static,
{
Self::new(move || supplier.get())
}
impl_common_name_methods!("runnable");
#[inline]
pub fn and_then<N>(self, next: N) -> BoxRunnable<E>
where
N: Runnable<E> + 'static,
E: 'static,
{
let name = self.name;
let mut function = self.function;
let mut next = next;
BoxRunnable::new_with_optional_name(
move || {
function()?;
next.run()
},
name,
)
}
#[inline]
pub fn then_callable<R, C>(self, callable: C) -> BoxCallable<R, E>
where
C: crate::tasks::callable::Callable<R, E> + 'static,
R: 'static,
E: 'static,
{
let name = self.name;
let mut function = self.function;
let mut callable = callable;
BoxCallable::new_with_optional_name(
move || {
function()?;
callable.call()
},
name,
)
}
}
impl<E> Runnable<E> for BoxRunnable<E> {
#[inline]
fn run(&mut self) -> Result<(), E> {
(self.function)()
}
impl_box_conversions!(
BoxRunnable<E>,
RcRunnable,
FnMut() -> Result<(), E>
);
#[inline]
fn into_callable(self) -> BoxCallable<(), E>
where
Self: Sized + 'static,
{
let name = self.name;
let mut function = self.function;
BoxCallable::new_with_optional_name(
move || {
function()?;
Ok(())
},
name,
)
}
}