#![allow(unused_imports)]
use super::*;
pub struct BoxCallableOnce<R, E> {
pub(super) function: Box<dyn FnOnce() -> Result<R, E>>,
pub(super) name: Option<String>,
}
impl<R, E> BoxCallableOnce<R, E> {
impl_common_new_methods!(
(FnOnce() -> Result<R, E> + 'static),
|function| Box::new(function),
"callable"
);
#[inline]
pub fn from_supplier<S>(supplier: S) -> Self
where
S: SupplierOnce<Result<R, E>> + 'static,
{
Self::new(move || supplier.get())
}
impl_common_name_methods!("callable");
#[inline]
pub fn map<U, M>(self, mapper: M) -> BoxCallableOnce<U, E>
where
M: FnOnce(R) -> U + 'static,
R: 'static,
E: 'static,
{
let name = self.name;
let function = self.function;
BoxCallableOnce::new_with_optional_name(move || function().map(mapper), name)
}
#[inline]
pub fn map_err<E2, M>(self, mapper: M) -> BoxCallableOnce<R, E2>
where
M: FnOnce(E) -> E2 + 'static,
R: 'static,
E: 'static,
{
let name = self.name;
let function = self.function;
BoxCallableOnce::new_with_optional_name(move || function().map_err(mapper), name)
}
#[inline]
pub fn and_then<U, N>(self, next: N) -> BoxCallableOnce<U, E>
where
N: FnOnce(R) -> Result<U, E> + 'static,
R: 'static,
E: 'static,
{
let name = self.name;
let function = self.function;
BoxCallableOnce::new_with_optional_name(move || function().and_then(next), name)
}
}
impl<R, E> CallableOnce<R, E> for BoxCallableOnce<R, E> {
#[inline]
fn call(self) -> Result<R, E> {
(self.function)()
}
impl_box_once_conversions!(BoxCallableOnce<R, E>, CallableOnce, FnOnce() -> Result<R, E>);
#[inline]
fn into_runnable(self) -> BoxRunnableOnce<E>
where
Self: Sized + 'static,
{
let name = self.name;
let function = self.function;
BoxRunnableOnce::new_with_optional_name(move || function().map(|_| ()), name)
}
}
impl<R, E> SupplierOnce<Result<R, E>> for BoxCallableOnce<R, E> {
#[inline]
fn get(self) -> Result<R, E> {
self.call()
}
}
impl_closure_once_trait!(
CallableOnce<R, E>,
call,
BoxCallableOnce,
FnOnce() -> Result<R, E>
);
impl_function_debug_display!(BoxCallableOnce<R, E>);