use crate::{
functions::macros::impl_function_debug_display,
macros::{
impl_box_once_conversions,
impl_closure_once_trait,
impl_common_name_methods,
impl_common_new_methods,
},
suppliers::supplier_once::SupplierOnce,
tasks::runnable::BoxRunnable,
};
pub trait Callable<R, E> {
fn call(self) -> Result<R, E>;
fn into_box(self) -> BoxCallable<R, E>
where
Self: Sized + 'static,
{
BoxCallable::new(move || self.call())
}
fn into_fn(self) -> impl FnOnce() -> Result<R, E>
where
Self: Sized + 'static,
{
move || self.call()
}
fn to_box(&self) -> BoxCallable<R, E>
where
Self: Clone + Sized + 'static,
{
self.clone().into_box()
}
fn to_fn(&self) -> impl FnOnce() -> Result<R, E>
where
Self: Clone + Sized + 'static,
{
self.clone().into_fn()
}
fn into_runnable(self) -> BoxRunnable<E>
where
Self: Sized + 'static,
{
BoxRunnable::new(move || self.call().map(|_| ()))
}
}
pub struct BoxCallable<R, E> {
function: Box<dyn FnOnce() -> Result<R, E>>,
name: Option<String>,
}
impl<R, E> BoxCallable<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) -> BoxCallable<U, E>
where
M: FnOnce(R) -> U + 'static,
R: 'static,
E: 'static,
{
let name = self.name;
let function = self.function;
BoxCallable::new_with_optional_name(move || function().map(mapper), name)
}
#[inline]
pub fn map_err<E2, M>(self, mapper: M) -> BoxCallable<R, E2>
where
M: FnOnce(E) -> E2 + 'static,
R: 'static,
E: 'static,
{
let name = self.name;
let function = self.function;
BoxCallable::new_with_optional_name(move || function().map_err(mapper), name)
}
#[inline]
pub fn and_then<U, N>(self, next: N) -> BoxCallable<U, E>
where
N: FnOnce(R) -> Result<U, E> + 'static,
R: 'static,
E: 'static,
{
let name = self.name;
let function = self.function;
BoxCallable::new_with_optional_name(move || function().and_then(next), name)
}
}
impl<R, E> Callable<R, E> for BoxCallable<R, E> {
#[inline]
fn call(self) -> Result<R, E> {
(self.function)()
}
impl_box_once_conversions!(BoxCallable<R, E>, Callable, FnOnce() -> Result<R, E>);
#[inline]
fn into_runnable(self) -> BoxRunnable<E>
where
Self: Sized + 'static,
{
let name = self.name;
let function = self.function;
BoxRunnable::new_with_optional_name(move || function().map(|_| ()), name)
}
}
impl<R, E> SupplierOnce<Result<R, E>> for BoxCallable<R, E> {
#[inline]
fn get(self) -> Result<R, E> {
self.call()
}
}
impl_closure_once_trait!(
Callable<R, E>,
call,
BoxCallable,
FnOnce() -> Result<R, E>
);
impl_function_debug_display!(BoxCallable<R, E>);