use std::sync::Arc;
use parking_lot::Mutex;
use crate::{
functions::macros::impl_function_debug_display,
macros::{
impl_common_name_methods,
impl_common_new_methods,
},
tasks::runnable_with::RunnableWith,
};
type ArcRunnableWithFn<T, E> =
Arc<Mutex<dyn FnMut(&mut T) -> Result<(), E> + Send>>;
#[must_use = "callback wrappers do nothing unless stored or invoked"]
pub struct ArcRunnableWith<T, E> {
pub(super) function: ArcRunnableWithFn<T, E>,
pub(super) metadata: crate::internal::CallbackMetadata,
}
impl<T, E> Clone for ArcRunnableWith<T, E> {
#[inline]
fn clone(&self) -> Self {
Self {
function: Arc::clone(&self.function),
metadata: self.metadata.clone(),
}
}
}
impl<T, E> ArcRunnableWith<T, E> {
impl_common_new_methods!(
semantic_mut(RunnableWith<T, E> + Send + 'static),
|source| move |input: &mut T| source.run_with(input),
|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> {
let mut function = self.function.lock();
function(input)
}
}
impl_function_debug_display!(ArcRunnableWith<T, E>);