#![allow(unused_imports)]
use super::*;
pub struct BoxCallableWith<T, R, E> {
pub(super) function: Box<dyn FnMut(&mut T) -> Result<R, E>>,
pub(super) name: Option<String>,
}
impl<T, R, E> BoxCallableWith<T, R, E> {
impl_common_new_methods!(
(FnMut(&mut T) -> Result<R, E> + 'static),
|function| Box::new(function),
"callable-with"
);
impl_common_name_methods!("callable-with");
#[inline]
pub fn map<U, M>(self, mut mapper: M) -> BoxCallableWith<T, U, E>
where
M: FnMut(R) -> U + 'static,
T: 'static,
R: 'static,
E: 'static,
{
let name = self.name;
let mut function = self.function;
BoxCallableWith::new_with_optional_name(move |input| function(input).map(&mut mapper), name)
}
#[inline]
pub fn map_err<E2, M>(self, mut mapper: M) -> BoxCallableWith<T, R, E2>
where
M: FnMut(E) -> E2 + 'static,
T: 'static,
R: 'static,
E: 'static,
{
let name = self.name;
let mut function = self.function;
BoxCallableWith::new_with_optional_name(
move |input| function(input).map_err(&mut mapper),
name,
)
}
#[inline]
pub fn and_then<U, N>(self, next: N) -> BoxCallableWith<T, U, E>
where
N: FnMut(R, &mut T) -> Result<U, E> + 'static,
T: 'static,
R: 'static,
E: 'static,
{
let name = self.name;
let mut function = self.function;
let mut next = next;
BoxCallableWith::new_with_optional_name(
move |input| {
let value = function(input)?;
next(value, input)
},
name,
)
}
}
impl<T, R, E> CallableWith<T, R, E> for BoxCallableWith<T, R, E> {
#[inline]
fn call_with(&mut self, input: &mut T) -> Result<R, E> {
(self.function)(input)
}
impl_box_conversions!(
BoxCallableWith<T, R, E>,
RcCallableWith,
FnMut(&mut T) -> Result<R, E>
);
#[inline]
fn into_runnable_with(self) -> BoxRunnableWith<T, E>
where
Self: Sized + 'static,
{
let name = self.name;
let mut function = self.function;
BoxRunnableWith::new_with_optional_name(move |input| function(input).map(|_| ()), name)
}
}