qubit_function/tasks/runnable_with/
rc_runnable_with.rs1use std::cell::RefCell;
14use std::rc::Rc;
15
16use crate::{
17 macros::{
18 impl_common_name_methods,
19 impl_common_new_methods,
20 impl_rc_conversions,
21 },
22 tasks::runnable_with::{
23 BoxRunnableWith,
24 RunnableWith,
25 },
26};
27
28type RcRunnableWithFn<T, E> = Rc<RefCell<dyn FnMut(&mut T) -> Result<(), E>>>;
29
30pub struct RcRunnableWith<T, E> {
36 pub(super) function: RcRunnableWithFn<T, E>,
38 pub(super) name: Option<String>,
40}
41
42impl<T, E> Clone for RcRunnableWith<T, E> {
43 #[inline]
44 fn clone(&self) -> Self {
45 Self {
46 function: Rc::clone(&self.function),
47 name: self.name.clone(),
48 }
49 }
50}
51
52impl<T, E> RcRunnableWith<T, E> {
53 impl_common_new_methods!(
54 (FnMut(&mut T) -> Result<(), E> + 'static),
55 |function| Rc::new(RefCell::new(function)),
56 "runnable-with"
57 );
58
59 impl_common_name_methods!("runnable-with");
60}
61
62impl<T, E> RunnableWith<T, E> for RcRunnableWith<T, E> {
63 #[inline]
65 fn run_with(&mut self, input: &mut T) -> Result<(), E> {
66 (self.function.borrow_mut())(input)
67 }
68
69 impl_rc_conversions!(
70 RcRunnableWith<T, E>,
71 BoxRunnableWith,
72 FnMut(input: &mut T) -> Result<(), E>
73 );
74}