#![allow(unused_imports)]
use super::*;
pub struct LocalBoxCallableOnce<R, E> {
pub(super) function: Box<dyn FnOnce() -> Result<R, E>>,
pub(super) name: Option<String>,
}
impl<R, E> LocalBoxCallableOnce<R, E> {
impl_common_new_methods!(
(FnOnce() -> Result<R, E> + 'static),
|function| Box::new(function),
"local 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!("local callable");
#[inline]
pub fn map<U, M>(self, mapper: M) -> LocalBoxCallableOnce<U, E>
where
M: FnOnce(R) -> U + 'static,
R: 'static,
E: 'static,
{
let name = self.name;
let function = self.function;
LocalBoxCallableOnce::new_with_optional_name(move || function().map(mapper), name)
}
#[inline]
pub fn map_err<E2, M>(self, mapper: M) -> LocalBoxCallableOnce<R, E2>
where
M: FnOnce(E) -> E2 + 'static,
R: 'static,
E: 'static,
{
let name = self.name;
let function = self.function;
LocalBoxCallableOnce::new_with_optional_name(move || function().map_err(mapper), name)
}
#[inline]
pub fn and_then<U, N>(self, next: N) -> LocalBoxCallableOnce<U, E>
where
N: FnOnce(R) -> Result<U, E> + 'static,
R: 'static,
E: 'static,
{
let name = self.name;
let function = self.function;
LocalBoxCallableOnce::new_with_optional_name(move || function().and_then(next), name)
}
}
impl<R, E> CallableOnce<R, E> for LocalBoxCallableOnce<R, E> {
#[inline]
fn call(self) -> Result<R, E> {
(self.function)()
}
#[inline]
fn into_local_box(self) -> LocalBoxCallableOnce<R, E>
where
Self: Sized + 'static,
{
self
}
#[inline]
fn into_fn(self) -> impl FnOnce() -> Result<R, E>
where
Self: Sized + 'static,
{
self.function
}
#[inline]
fn into_local_runnable(self) -> LocalBoxRunnableOnce<E>
where
Self: Sized + 'static,
{
let name = self.name;
let function = self.function;
LocalBoxRunnableOnce::new_with_optional_name(move || function().map(|_| ()), name)
}
}
impl<R, E> SupplierOnce<Result<R, E>> for LocalBoxCallableOnce<R, E> {
#[inline]
fn get(self) -> Result<R, E> {
self.call()
}
}
impl_function_debug_display!(LocalBoxCallableOnce<R, E>);