use crate::{
macros::{
impl_common_name_methods,
impl_common_new_methods,
},
suppliers::{
macros::impl_supplier_debug_display,
supplier_once::SupplierOnce,
},
tasks::runnable_once::RunnableOnce,
};
use crate::tasks::callable_once::{
BoxCallableOnce,
CallableOnce,
};
#[must_use = "callback wrappers do nothing unless stored or invoked"]
pub struct BoxRunnableOnce<E> {
pub(super) function: Box<dyn FnOnce() -> Result<(), E> + Send>,
pub(super) metadata: crate::internal::CallbackMetadata,
}
impl<E> BoxRunnableOnce<E> {
impl_common_new_methods!(
semantic(RunnableOnce<E> + Send + 'static),
|source| move || source.run_once(),
|function| Box::new(function),
"runnable"
);
#[inline]
pub fn from_supplier<S>(supplier: S) -> Self
where
S: SupplierOnce<Result<(), E>> + Send + 'static,
{
Self::new(move || supplier.get())
}
impl_common_name_methods!("runnable");
#[inline]
pub fn and_then<N>(self, next: N) -> BoxRunnableOnce<E>
where
N: RunnableOnce<E> + Send + 'static,
E: 'static,
{
let function = self.function;
BoxRunnableOnce::new(move || {
function()?;
next.run_once()
})
}
#[inline]
pub fn then_callable<R, C>(self, callable: C) -> BoxCallableOnce<R, E>
where
C: CallableOnce<R, E> + Send + 'static,
R: 'static,
E: 'static,
{
let function = self.function;
BoxCallableOnce::new(move || {
function()?;
callable.call_once()
})
}
}
impl<E> RunnableOnce<E> for BoxRunnableOnce<E> {
#[inline(always)]
fn run_once(self) -> Result<(), E> {
(self.function)()
}
}
impl<E> SupplierOnce<Result<(), E>> for BoxRunnableOnce<E> {
#[inline(always)]
fn get(self) -> Result<(), E> {
self.run_once()
}
}
impl_supplier_debug_display!(BoxRunnableOnce<E>);