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::callable_with::BoxCallableWith,
};
pub trait RunnableWith<T, E> {
fn run_with(&mut self, input: &mut T) -> Result<(), E>;
fn into_box(mut self) -> BoxRunnableWith<T, E>
where
Self: Sized + 'static,
{
BoxRunnableWith::new(move |input| self.run_with(input))
}
fn into_rc(mut self) -> RcRunnableWith<T, E>
where
Self: Sized + 'static,
{
RcRunnableWith::new(move |input| self.run_with(input))
}
fn into_arc(mut self) -> ArcRunnableWith<T, E>
where
Self: Sized + Send + 'static,
{
ArcRunnableWith::new(move |input| self.run_with(input))
}
fn into_fn(mut self) -> impl FnMut(&mut T) -> Result<(), E>
where
Self: Sized + 'static,
{
move |input| self.run_with(input)
}
fn to_box(&self) -> BoxRunnableWith<T, E>
where
Self: Clone + Sized + 'static,
{
self.clone().into_box()
}
fn to_rc(&self) -> RcRunnableWith<T, E>
where
Self: Clone + Sized + 'static,
{
self.clone().into_rc()
}
fn to_arc(&self) -> ArcRunnableWith<T, E>
where
Self: Clone + Send + Sized + 'static,
{
self.clone().into_arc()
}
fn to_fn(&self) -> impl FnMut(&mut T) -> Result<(), E>
where
Self: Clone + Sized + 'static,
{
self.clone().into_fn()
}
fn into_callable_with(mut self) -> BoxCallableWith<T, (), E>
where
Self: Sized + 'static,
{
BoxCallableWith::new(move |input| self.run_with(input))
}
}
pub struct BoxRunnableWith<T, E> {
function: Box<dyn FnMut(&mut T) -> Result<(), E>>,
name: Option<String>,
}
impl<T, E> BoxRunnableWith<T, E> {
impl_common_new_methods!(
(FnMut(&mut T) -> Result<(), E> + 'static),
|function| Box::new(function),
"runnable-with"
);
impl_common_name_methods!("runnable-with");
#[inline]
pub fn and_then<N>(self, next: N) -> BoxRunnableWith<T, E>
where
N: RunnableWith<T, E> + 'static,
T: 'static,
E: 'static,
{
let name = self.name;
let mut function = self.function;
let mut next = next;
BoxRunnableWith::new_with_optional_name(
move |input| {
function(input)?;
next.run_with(input)
},
name,
)
}
#[inline]
pub fn then_callable_with<R, C>(self, callable: C) -> BoxCallableWith<T, R, E>
where
C: crate::tasks::callable_with::CallableWith<T, R, E> + 'static,
T: 'static,
R: 'static,
E: 'static,
{
let name = self.name;
let mut function = self.function;
let mut callable = callable;
BoxCallableWith::new_with_optional_name(
move |input| {
function(input)?;
callable.call_with(input)
},
name,
)
}
}
impl<T, E> RunnableWith<T, E> for BoxRunnableWith<T, E> {
#[inline]
fn run_with(&mut self, input: &mut T) -> Result<(), E> {
(self.function)(input)
}
impl_box_conversions!(
BoxRunnableWith<T, E>,
RcRunnableWith,
FnMut(&mut T) -> Result<(), E>
);
#[inline]
fn into_callable_with(self) -> BoxCallableWith<T, (), E>
where
Self: Sized + 'static,
{
let name = self.name;
let mut function = self.function;
BoxCallableWith::new_with_optional_name(
move |input| {
function(input)?;
Ok(())
},
name,
)
}
}
pub struct RcRunnableWith<T, E> {
function: Rc<RefCell<dyn FnMut(&mut T) -> Result<(), E>>>,
name: Option<String>,
}
impl<T, E> Clone for RcRunnableWith<T, E> {
#[inline]
fn clone(&self) -> Self {
Self {
function: Rc::clone(&self.function),
name: self.name.clone(),
}
}
}
impl<T, E> RcRunnableWith<T, E> {
impl_common_new_methods!(
(FnMut(&mut T) -> Result<(), E> + 'static),
|function| Rc::new(RefCell::new(function)),
"runnable-with"
);
impl_common_name_methods!("runnable-with");
}
impl<T, E> RunnableWith<T, E> for RcRunnableWith<T, E> {
#[inline]
fn run_with(&mut self, input: &mut T) -> Result<(), E> {
(self.function.borrow_mut())(input)
}
impl_rc_conversions!(
RcRunnableWith<T, E>,
BoxRunnableWith,
FnMut(input: &mut T) -> Result<(), E>
);
}
pub struct ArcRunnableWith<T, E> {
function: Arc<Mutex<dyn FnMut(&mut T) -> Result<(), E> + Send>>,
name: Option<String>,
}
impl<T, E> Clone for ArcRunnableWith<T, E> {
#[inline]
fn clone(&self) -> Self {
Self {
function: Arc::clone(&self.function),
name: self.name.clone(),
}
}
}
impl<T, E> ArcRunnableWith<T, E> {
impl_common_new_methods!(
(FnMut(&mut T) -> Result<(), E> + Send + 'static),
|function| Arc::new(Mutex::new(function)),
"runnable-with"
);
impl_common_name_methods!("runnable-with");
}
impl<T, E> RunnableWith<T, E> for ArcRunnableWith<T, E> {
#[inline]
fn run_with(&mut self, input: &mut T) -> Result<(), E> {
(self.function.lock())(input)
}
impl_arc_conversions!(
ArcRunnableWith<T, E>,
BoxRunnableWith,
RcRunnableWith,
FnMut(input: &mut T) -> Result<(), E>
);
}
impl_closure_trait!(
RunnableWith<T, E>,
run_with,
FnMut(input: &mut T) -> Result<(), E>
);
impl_function_debug_display!(BoxRunnableWith<T, E>);
impl_function_debug_display!(RcRunnableWith<T, E>);
impl_function_debug_display!(ArcRunnableWith<T, E>);