use std::cell::RefCell;
use std::rc::Rc;
use crate::{
macros::{
impl_common_name_methods,
impl_common_new_methods,
impl_rc_conversions,
},
tasks::runnable_with::{
BoxRunnableWith,
RunnableWith,
},
};
type RcRunnableWithFn<T, E> = Rc<RefCell<dyn FnMut(&mut T) -> Result<(), E>>>;
pub struct RcRunnableWith<T, E> {
pub(super) function: RcRunnableWithFn<T, E>,
pub(super) 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>
);
}