use std::cell::RefCell;
#[cfg(feature = "rc")]
use std::rc::Rc;
use crate::{
functions::macros::impl_function_debug_display,
macros::{
impl_common_name_methods,
impl_common_new_methods,
},
tasks::callable_with::CallableWith,
};
type RcCallableWithFn<T, R, E> = Rc<RefCell<dyn FnMut(&mut T) -> Result<R, E>>>;
#[must_use = "callback wrappers do nothing unless stored or invoked"]
pub struct RcCallableWith<T, R, E> {
pub(super) function: RcCallableWithFn<T, R, E>,
pub(super) metadata: crate::internal::CallbackMetadata,
}
impl<T, R, E> Clone for RcCallableWith<T, R, E> {
#[inline]
fn clone(&self) -> Self {
Self {
function: Rc::clone(&self.function),
metadata: self.metadata.clone(),
}
}
}
impl<T, R, E> RcCallableWith<T, R, E> {
impl_common_new_methods!(
semantic_mut(CallableWith<T, R, E> + 'static),
|source| move |input: &mut T| source.call_with(input),
|function| Rc::new(RefCell::new(function)),
"callable-with"
);
impl_common_name_methods!("callable-with");
}
impl<T, R, E> CallableWith<T, R, E> for RcCallableWith<T, R, E> {
#[inline]
fn call_with(&mut self, input: &mut T) -> Result<R, E> {
let mut function = self.function.borrow_mut();
function(input)
}
}
impl_function_debug_display!(RcCallableWith<T, R, E>);