#![allow(unused_imports)]
use super::*;
pub struct RcCallable<R, E> {
pub(super) function: Rc<RefCell<dyn FnMut() -> Result<R, E>>>,
pub(super) name: Option<String>,
}
impl<R, E> Clone for RcCallable<R, E> {
#[inline]
fn clone(&self) -> Self {
Self {
function: Rc::clone(&self.function),
name: self.name.clone(),
}
}
}
impl<R, E> RcCallable<R, E> {
impl_common_new_methods!(
(FnMut() -> Result<R, E> + 'static),
|function| Rc::new(RefCell::new(function)),
"callable"
);
#[inline]
pub fn from_supplier<S>(supplier: S) -> Self
where
S: Supplier<Result<R, E>> + 'static,
{
Self::new(move || supplier.get())
}
impl_common_name_methods!("callable");
}
impl<R, E> Callable<R, E> for RcCallable<R, E> {
#[inline]
fn call(&mut self) -> Result<R, E> {
(self.function.borrow_mut())()
}
impl_rc_conversions!(
RcCallable<R, E>,
BoxCallable,
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.borrow_mut())().map(|_| ()), name)
}
}