use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;
use parking_lot::Mutex;
use crate::{
functions::macros::impl_function_debug_display,
macros::{
impl_arc_conversions,
impl_box_conversions,
impl_closure_trait,
impl_common_name_methods,
impl_common_new_methods,
impl_rc_conversions,
},
tasks::runnable_with::BoxRunnableWith,
};
pub trait CallableWith<T, R, E> {
fn call_with(&mut self, input: &mut T) -> Result<R, E>;
fn into_box(mut self) -> BoxCallableWith<T, R, E>
where
Self: Sized + 'static,
{
BoxCallableWith::new(move |input| self.call_with(input))
}
fn into_rc(mut self) -> RcCallableWith<T, R, E>
where
Self: Sized + 'static,
{
RcCallableWith::new(move |input| self.call_with(input))
}
fn into_arc(mut self) -> ArcCallableWith<T, R, E>
where
Self: Sized + Send + 'static,
{
ArcCallableWith::new(move |input| self.call_with(input))
}
fn into_fn(mut self) -> impl FnMut(&mut T) -> Result<R, E>
where
Self: Sized + 'static,
{
move |input| self.call_with(input)
}
fn to_box(&self) -> BoxCallableWith<T, R, E>
where
Self: Clone + Sized + 'static,
{
self.clone().into_box()
}
fn to_rc(&self) -> RcCallableWith<T, R, E>
where
Self: Clone + Sized + 'static,
{
self.clone().into_rc()
}
fn to_arc(&self) -> ArcCallableWith<T, R, E>
where
Self: Clone + Send + Sized + 'static,
{
self.clone().into_arc()
}
fn to_fn(&self) -> impl FnMut(&mut T) -> Result<R, E>
where
Self: Clone + Sized + 'static,
{
self.clone().into_fn()
}
fn into_runnable_with(mut self) -> BoxRunnableWith<T, E>
where
Self: Sized + 'static,
{
BoxRunnableWith::new(move |input| self.call_with(input).map(|_| ()))
}
}
pub struct BoxCallableWith<T, R, E> {
function: Box<dyn FnMut(&mut T) -> Result<R, E>>,
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)
}
}
pub struct RcCallableWith<T, R, E> {
function: Rc<RefCell<dyn FnMut(&mut T) -> Result<R, E>>>,
name: Option<String>,
}
impl<T, R, E> Clone for RcCallableWith<T, R, E> {
#[inline]
fn clone(&self) -> Self {
Self {
function: Rc::clone(&self.function),
name: self.name.clone(),
}
}
}
impl<T, R, E> RcCallableWith<T, R, E> {
impl_common_new_methods!(
(FnMut(&mut T) -> Result<R, E> + 'static),
|function| Rc::new(RefCell::new(function)),
"callable-with"
);
impl_common_name_methods!("callable-with");
}
impl<T, R, E> CallableWith<T, R, E> for RcCallableWith<T, R, E> {
#[inline]
fn call_with(&mut self, input: &mut T) -> Result<R, E> {
(self.function.borrow_mut())(input)
}
impl_rc_conversions!(
RcCallableWith<T, R, E>,
BoxCallableWith,
FnMut(input: &mut T) -> Result<R, E>
);
#[inline]
fn into_runnable_with(self) -> BoxRunnableWith<T, E>
where
Self: Sized + 'static,
{
let name = self.name;
let function = self.function;
BoxRunnableWith::new_with_optional_name(
move |input| (function.borrow_mut())(input).map(|_| ()),
name,
)
}
}
pub struct ArcCallableWith<T, R, E> {
function: Arc<Mutex<dyn FnMut(&mut T) -> Result<R, E> + Send>>,
name: Option<String>,
}
impl<T, R, E> Clone for ArcCallableWith<T, R, E> {
#[inline]
fn clone(&self) -> Self {
Self {
function: Arc::clone(&self.function),
name: self.name.clone(),
}
}
}
impl<T, R, E> ArcCallableWith<T, R, E> {
impl_common_new_methods!(
(FnMut(&mut T) -> Result<R, E> + Send + 'static),
|function| Arc::new(Mutex::new(function)),
"callable-with"
);
impl_common_name_methods!("callable-with");
}
impl<T, R, E> CallableWith<T, R, E> for ArcCallableWith<T, R, E> {
#[inline]
fn call_with(&mut self, input: &mut T) -> Result<R, E> {
(self.function.lock())(input)
}
impl_arc_conversions!(
ArcCallableWith<T, R, E>,
BoxCallableWith,
RcCallableWith,
FnMut(input: &mut T) -> Result<R, E>
);
#[inline]
fn into_runnable_with(self) -> BoxRunnableWith<T, E>
where
Self: Sized + 'static,
{
let name = self.name;
let function = self.function;
BoxRunnableWith::new_with_optional_name(
move |input| (function.lock())(input).map(|_| ()),
name,
)
}
}
impl_closure_trait!(
CallableWith<T, R, E>,
call_with,
FnMut(input: &mut T) -> Result<R, E>
);
impl_function_debug_display!(BoxCallableWith<T, R, E>);
impl_function_debug_display!(RcCallableWith<T, R, E>);
impl_function_debug_display!(ArcCallableWith<T, R, E>);