#![allow(unused_imports)]
use super::*;
pub struct ArcCallable<R, E> {
pub(super) function: Arc<Mutex<dyn FnMut() -> Result<R, E> + Send>>,
pub(super) 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>);