Skip to main content

qubit_function/tasks/runnable_with/
rc_runnable_with.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026.
4 *    Haixing Hu, Qubit Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9//! Defines the `RcRunnableWith` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15/// Single-threaded shared runnable with mutable input.
16///
17/// `RcRunnableWith<T, E>` stores a
18/// `Rc<RefCell<dyn FnMut(&mut T) -> Result<(), E>>>`.
19///
20/// # Author
21///
22/// Haixing Hu
23pub struct RcRunnableWith<T, E> {
24    /// The stateful closure executed by this runnable.
25    pub(super) function: Rc<RefCell<dyn FnMut(&mut T) -> Result<(), E>>>,
26    /// The optional name of this runnable.
27    pub(super) name: Option<String>,
28}
29
30impl<T, E> Clone for RcRunnableWith<T, 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, E> RcRunnableWith<T, E> {
41    impl_common_new_methods!(
42        (FnMut(&mut T) -> Result<(), E> + 'static),
43        |function| Rc::new(RefCell::new(function)),
44        "runnable-with"
45    );
46
47    impl_common_name_methods!("runnable-with");
48}
49
50impl<T, E> RunnableWith<T, E> for RcRunnableWith<T, E> {
51    /// Executes the shared runnable with mutable input.
52    #[inline]
53    fn run_with(&mut self, input: &mut T) -> Result<(), E> {
54        (self.function.borrow_mut())(input)
55    }
56
57    impl_rc_conversions!(
58        RcRunnableWith<T, E>,
59        BoxRunnableWith,
60        FnMut(input: &mut T) -> Result<(), E>
61    );
62}