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