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,
};
mod box_callable_with;
pub use box_callable_with::BoxCallableWith;
mod rc_callable_with;
pub use rc_callable_with::RcCallableWith;
mod arc_callable_with;
pub use arc_callable_with::ArcCallableWith;
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(|_| ()))
}
}