qubit_function/tasks/callable_with/
rc_callable_with.rs1#![allow(unused_imports)]
14
15use super::*;
16
17pub struct RcCallableWith<T, R, E> {
23 pub(super) function: Rc<RefCell<dyn FnMut(&mut T) -> Result<R, E>>>,
25 pub(super) name: Option<String>,
27}
28
29impl<T, R, E> Clone for RcCallableWith<T, R, E> {
30 #[inline]
31 fn clone(&self) -> Self {
32 Self {
33 function: Rc::clone(&self.function),
34 name: self.name.clone(),
35 }
36 }
37}
38
39impl<T, R, E> RcCallableWith<T, R, E> {
40 impl_common_new_methods!(
41 (FnMut(&mut T) -> Result<R, E> + 'static),
42 |function| Rc::new(RefCell::new(function)),
43 "callable-with"
44 );
45
46 impl_common_name_methods!("callable-with");
47}
48
49impl<T, R, E> CallableWith<T, R, E> for RcCallableWith<T, R, E> {
50 #[inline]
52 fn call_with(&mut self, input: &mut T) -> Result<R, E> {
53 (self.function.borrow_mut())(input)
54 }
55
56 impl_rc_conversions!(
57 RcCallableWith<T, R, E>,
58 BoxCallableWith,
59 FnMut(input: &mut T) -> Result<R, E>
60 );
61
62 #[inline]
65 fn into_runnable_with(self) -> BoxRunnableWith<T, E>
66 where
67 Self: Sized + 'static,
68 {
69 let name = self.name;
70 let function = self.function;
71 BoxRunnableWith::new_with_optional_name(
72 move |input| (function.borrow_mut())(input).map(|_| ()),
73 name,
74 )
75 }
76}