Skip to main content

qubit_function/tasks/callable/
rc_callable.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026 Haixing Hu.
4 *
5 *    SPDX-License-Identifier: Apache-2.0
6 *
7 *    Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10// qubit-style: allow explicit-imports
11//! Defines the `RcCallable` public type.
12
13#![allow(unused_imports)]
14
15use super::*;
16
17// ============================================================================
18// RcCallable
19// ============================================================================
20
21/// Single-threaded shared callable.
22///
23/// `RcCallable<R, E>` stores a `Rc<RefCell<dyn FnMut() -> Result<R, E>>>` and
24/// can be called repeatedly through shared ownership.
25///
26/// # Type Parameters
27///
28/// * `R` - The success value returned by the computation.
29/// * `E` - The error value returned when the computation fails.
30///
31pub struct RcCallable<R, E> {
32    /// The stateful closure executed by this callable.
33    pub(super) function: Rc<RefCell<dyn FnMut() -> Result<R, E>>>,
34    /// The optional name of this callable.
35    pub(super) name: Option<String>,
36}
37
38impl<R, E> Clone for RcCallable<R, E> {
39    #[inline]
40    fn clone(&self) -> Self {
41        Self {
42            function: Rc::clone(&self.function),
43            name: self.name.clone(),
44        }
45    }
46}
47
48impl<R, E> RcCallable<R, E> {
49    impl_common_new_methods!(
50        (FnMut() -> Result<R, E> + 'static),
51        |function| Rc::new(RefCell::new(function)),
52        "callable"
53    );
54
55    /// Creates an `RcCallable` from a reusable supplier.
56    ///
57    /// # Parameters
58    ///
59    /// * `supplier` - The supplier that produces the callable result.
60    ///
61    /// # Returns
62    ///
63    /// A new `RcCallable<R, E>`.
64    #[inline]
65    pub fn from_supplier<S>(supplier: S) -> Self
66    where
67        S: Supplier<Result<R, E>> + 'static,
68    {
69        Self::new(move || supplier.get())
70    }
71
72    impl_common_name_methods!("callable");
73}
74
75impl<R, E> Callable<R, E> for RcCallable<R, E> {
76    /// Executes the shared callable.
77    #[inline]
78    fn call(&mut self) -> Result<R, E> {
79        (self.function.borrow_mut())()
80    }
81
82    impl_rc_conversions!(
83        RcCallable<R, E>,
84        BoxCallable,
85        BoxCallableOnce,
86        FnMut() -> Result<R, E>
87    );
88
89    /// Converts this shared callable into a boxed runnable while preserving its
90    /// name.
91    #[inline]
92    fn into_runnable(self) -> BoxRunnable<E>
93    where
94        Self: Sized + 'static,
95    {
96        let name = self.name;
97        let function = self.function;
98        BoxRunnable::new_with_optional_name(move || (function.borrow_mut())().map(|_| ()), name)
99    }
100}