use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;
use parking_lot::Mutex;
use crate::{
macros::{
impl_arc_conversions,
impl_box_conversions,
impl_closure_trait,
impl_common_name_methods,
impl_common_new_methods,
impl_rc_conversions,
},
suppliers::macros::impl_supplier_debug_display,
suppliers::supplier::Supplier,
suppliers::supplier_once::SupplierOnce,
tasks::callable::BoxCallable,
};
pub trait Runnable<E> {
fn run(&mut self) -> Result<(), E>;
fn into_box(mut self) -> BoxRunnable<E>
where
Self: Sized + 'static,
{
BoxRunnable::new(move || self.run())
}
fn into_rc(mut self) -> RcRunnable<E>
where
Self: Sized + 'static,
{
RcRunnable::new(move || self.run())
}
fn into_arc(mut self) -> ArcRunnable<E>
where
Self: Sized + Send + 'static,
{
ArcRunnable::new(move || self.run())
}
fn into_fn(mut self) -> impl FnMut() -> Result<(), E>
where
Self: Sized + 'static,
{
move || self.run()
}
fn to_box(&self) -> BoxRunnable<E>
where
Self: Clone + Sized + 'static,
{
self.clone().into_box()
}
fn to_fn(&self) -> impl FnMut() -> Result<(), E>
where
Self: Clone + Sized + 'static,
{
self.clone().into_fn()
}
fn to_rc(&self) -> RcRunnable<E>
where
Self: Clone + Sized + 'static,
{
self.clone().into_rc()
}
fn to_arc(&self) -> ArcRunnable<E>
where
Self: Clone + Send + Sized + 'static,
{
self.clone().into_arc()
}
fn into_callable(self) -> BoxCallable<(), E>
where
Self: Sized + 'static,
{
let mut runnable = self;
BoxCallable::new(move || runnable.run())
}
}
pub struct BoxRunnable<E> {
function: Box<dyn FnMut() -> Result<(), E>>,
name: Option<String>,
}
impl<E> BoxRunnable<E> {
impl_common_new_methods!(
(FnMut() -> Result<(), E> + 'static),
|function| Box::new(function),
"runnable"
);
#[inline]
pub fn from_supplier<S>(supplier: S) -> Self
where
S: Supplier<Result<(), E>> + 'static,
{
Self::new(move || supplier.get())
}
impl_common_name_methods!("runnable");
#[inline]
pub fn and_then<N>(self, next: N) -> BoxRunnable<E>
where
N: Runnable<E> + 'static,
E: 'static,
{
let name = self.name;
let mut function = self.function;
let mut next = next;
BoxRunnable::new_with_optional_name(
move || {
function()?;
next.run()
},
name,
)
}
#[inline]
pub fn then_callable<R, C>(self, callable: C) -> BoxCallable<R, E>
where
C: crate::tasks::callable::Callable<R, E> + 'static,
R: 'static,
E: 'static,
{
let name = self.name;
let mut function = self.function;
let mut callable = callable;
BoxCallable::new_with_optional_name(
move || {
function()?;
callable.call()
},
name,
)
}
}
impl<E> Runnable<E> for BoxRunnable<E> {
#[inline]
fn run(&mut self) -> Result<(), E> {
(self.function)()
}
impl_box_conversions!(
BoxRunnable<E>,
RcRunnable,
FnMut() -> Result<(), E>
);
#[inline]
fn into_callable(self) -> BoxCallable<(), E>
where
Self: Sized + 'static,
{
let name = self.name;
let mut function = self.function;
BoxCallable::new_with_optional_name(
move || {
function()?;
Ok(())
},
name,
)
}
}
pub struct RcRunnable<E> {
function: Rc<RefCell<dyn FnMut() -> Result<(), E>>>,
name: Option<String>,
}
impl<E> Clone for RcRunnable<E> {
#[inline]
fn clone(&self) -> Self {
Self {
function: Rc::clone(&self.function),
name: self.name.clone(),
}
}
}
impl<E> RcRunnable<E> {
impl_common_new_methods!(
(FnMut() -> Result<(), E> + 'static),
|function| Rc::new(RefCell::new(function)),
"runnable"
);
#[inline]
pub fn from_supplier<S>(supplier: S) -> Self
where
S: Supplier<Result<(), E>> + 'static,
{
Self::new(move || supplier.get())
}
impl_common_name_methods!("runnable");
}
impl<E> Runnable<E> for RcRunnable<E> {
#[inline]
fn run(&mut self) -> Result<(), E> {
(self.function.borrow_mut())()
}
impl_rc_conversions!(
RcRunnable<E>,
BoxRunnable,
FnMut() -> Result<(), E>
);
}
pub struct ArcRunnable<E> {
function: Arc<Mutex<dyn FnMut() -> Result<(), E> + Send>>,
name: Option<String>,
}
impl<E> Clone for ArcRunnable<E> {
#[inline]
fn clone(&self) -> Self {
Self {
function: Arc::clone(&self.function),
name: self.name.clone(),
}
}
}
impl<E> ArcRunnable<E> {
impl_common_new_methods!(
(FnMut() -> Result<(), E> + Send + 'static),
|function| Arc::new(Mutex::new(function)),
"runnable"
);
#[inline]
pub fn from_supplier<S>(supplier: S) -> Self
where
S: Supplier<Result<(), E>> + Send + 'static,
{
Self::new(move || supplier.get())
}
impl_common_name_methods!("runnable");
}
impl<E> Runnable<E> for ArcRunnable<E> {
#[inline]
fn run(&mut self) -> Result<(), E> {
(self.function.lock())()
}
impl_arc_conversions!(
ArcRunnable<E>,
BoxRunnable,
RcRunnable,
FnMut() -> Result<(), E>
);
}
impl<E> SupplierOnce<Result<(), E>> for BoxRunnable<E> {
#[inline]
fn get(mut self) -> Result<(), E> {
self.run()
}
}
impl_closure_trait!(
Runnable<E>,
run,
FnMut() -> Result<(), E>
);
impl_supplier_debug_display!(BoxRunnable<E>);