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,
},
suppliers::supplier::Supplier,
tasks::callable_once::BoxCallableOnce,
tasks::runnable::BoxRunnable,
};
pub trait Callable<R, E> {
fn call(&mut self) -> Result<R, E>;
fn into_box(mut self) -> BoxCallable<R, E>
where
Self: Sized + 'static,
{
BoxCallable::new(move || self.call())
}
fn into_rc(mut self) -> RcCallable<R, E>
where
Self: Sized + 'static,
{
RcCallable::new(move || self.call())
}
fn into_arc(mut self) -> ArcCallable<R, E>
where
Self: Sized + Send + 'static,
{
ArcCallable::new(move || self.call())
}
fn into_fn(mut self) -> impl FnMut() -> 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_rc(&self) -> RcCallable<R, E>
where
Self: Clone + Sized + 'static,
{
self.clone().into_rc()
}
fn to_arc(&self) -> ArcCallable<R, E>
where
Self: Clone + Send + Sized + 'static,
{
self.clone().into_arc()
}
fn to_fn(&self) -> impl FnMut() -> Result<R, E>
where
Self: Clone + Sized + 'static,
{
self.clone().into_fn()
}
fn into_once(mut self) -> BoxCallableOnce<R, E>
where
Self: Sized + 'static,
{
BoxCallableOnce::new(move || self.call())
}
fn to_once(&self) -> BoxCallableOnce<R, E>
where
Self: Clone + Sized + 'static,
{
self.clone().into_once()
}
fn into_runnable(mut self) -> BoxRunnable<E>
where
Self: Sized + 'static,
{
BoxRunnable::new(move || self.call().map(|_| ()))
}
}
pub struct BoxCallable<R, E> {
function: Box<dyn FnMut() -> Result<R, E>>,
name: Option<String>,
}
impl<R, E> BoxCallable<R, E> {
impl_common_new_methods!(
(FnMut() -> Result<R, E> + 'static),
|function| Box::new(function),
"callable"
);
#[inline]
pub fn from_supplier<S>(supplier: S) -> Self
where
S: Supplier<Result<R, E>> + 'static,
{
Self::new(move || supplier.get())
}
impl_common_name_methods!("callable");
#[inline]
pub fn map<U, M>(self, mut mapper: M) -> BoxCallable<U, E>
where
M: FnMut(R) -> U + 'static,
R: 'static,
E: 'static,
{
let name = self.name;
let mut function = self.function;
BoxCallable::new_with_optional_name(move || function().map(&mut mapper), name)
}
#[inline]
pub fn map_err<E2, M>(self, mut mapper: M) -> BoxCallable<R, E2>
where
M: FnMut(E) -> E2 + 'static,
R: 'static,
E: 'static,
{
let name = self.name;
let mut function = self.function;
BoxCallable::new_with_optional_name(move || function().map_err(&mut mapper), name)
}
#[inline]
pub fn and_then<U, N>(self, next: N) -> BoxCallable<U, E>
where
N: FnMut(R) -> Result<U, E> + 'static,
R: 'static,
E: 'static,
{
let name = self.name;
let mut function = self.function;
let mut next = next;
BoxCallable::new_with_optional_name(
move || {
let value = function()?;
next(value)
},
name,
)
}
}
impl<R, E> Callable<R, E> for BoxCallable<R, E> {
#[inline]
fn call(&mut self) -> Result<R, E> {
(self.function)()
}
impl_box_conversions!(
BoxCallable<R, E>,
RcCallable,
FnMut() -> Result<R, E>,
BoxCallableOnce
);
#[inline]
fn into_runnable(self) -> BoxRunnable<E>
where
Self: Sized + 'static,
{
let name = self.name;
let mut function = self.function;
BoxRunnable::new_with_optional_name(move || function().map(|_| ()), name)
}
}
pub struct RcCallable<R, E> {
function: Rc<RefCell<dyn FnMut() -> Result<R, E>>>,
name: Option<String>,
}
impl<R, E> Clone for RcCallable<R, E> {
#[inline]
fn clone(&self) -> Self {
Self {
function: Rc::clone(&self.function),
name: self.name.clone(),
}
}
}
impl<R, E> RcCallable<R, E> {
impl_common_new_methods!(
(FnMut() -> Result<R, E> + 'static),
|function| Rc::new(RefCell::new(function)),
"callable"
);
#[inline]
pub fn from_supplier<S>(supplier: S) -> Self
where
S: Supplier<Result<R, E>> + 'static,
{
Self::new(move || supplier.get())
}
impl_common_name_methods!("callable");
}
impl<R, E> Callable<R, E> for RcCallable<R, E> {
#[inline]
fn call(&mut self) -> Result<R, E> {
(self.function.borrow_mut())()
}
impl_rc_conversions!(
RcCallable<R, E>,
BoxCallable,
BoxCallableOnce,
FnMut() -> 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.borrow_mut())().map(|_| ()), name)
}
}
pub struct ArcCallable<R, E> {
function: Arc<Mutex<dyn FnMut() -> Result<R, E> + Send>>,
name: Option<String>,
}
impl<R, E> Clone for ArcCallable<R, E> {
#[inline]
fn clone(&self) -> Self {
Self {
function: Arc::clone(&self.function),
name: self.name.clone(),
}
}
}
impl<R, E> ArcCallable<R, E> {
impl_common_new_methods!(
(FnMut() -> Result<R, E> + Send + 'static),
|function| Arc::new(Mutex::new(function)),
"callable"
);
#[inline]
pub fn from_supplier<S>(supplier: S) -> Self
where
S: Supplier<Result<R, E>> + Send + 'static,
{
Self::new(move || supplier.get())
}
impl_common_name_methods!("callable");
}
impl<R, E> Callable<R, E> for ArcCallable<R, E> {
#[inline]
fn call(&mut self) -> Result<R, E> {
(self.function.lock())()
}
impl_arc_conversions!(
ArcCallable<R, E>,
BoxCallable,
RcCallable,
BoxCallableOnce,
FnMut() -> 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.lock())().map(|_| ()), name)
}
}
impl_closure_trait!(
Callable<R, E>,
call,
BoxCallableOnce,
FnMut() -> Result<R, E>
);
impl_function_debug_display!(BoxCallable<R, E>);
impl_function_debug_display!(RcCallable<R, E>);
impl_function_debug_display!(ArcCallable<R, E>);