Skip to main content

qubit_function/tasks/callable/
arc_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 `ArcCallable` public type.
12
13#![allow(unused_imports)]
14
15use super::*;
16
17// ============================================================================
18// ArcCallable
19// ============================================================================
20
21/// Thread-safe callable.
22///
23/// `ArcCallable<R, E>` stores a `Arc<Mutex<dyn FnMut() -> Result<R, E> + Send>>`
24/// and can be called repeatedly across threads.
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 ArcCallable<R, E> {
32    /// The stateful closure executed by this callable.
33    pub(super) function: Arc<Mutex<dyn FnMut() -> Result<R, E> + Send>>,
34    /// The optional name of this callable.
35    pub(super) name: Option<String>,
36}
37
38impl<R, E> Clone for ArcCallable<R, E> {
39    #[inline]
40    fn clone(&self) -> Self {
41        Self {
42            function: Arc::clone(&self.function),
43            name: self.name.clone(),
44        }
45    }
46}
47
48impl<R, E> ArcCallable<R, E> {
49    impl_common_new_methods!(
50        (FnMut() -> Result<R, E> + Send + 'static),
51        |function| Arc::new(Mutex::new(function)),
52        "callable"
53    );
54
55    /// Creates an `ArcCallable` from a reusable supplier.
56    ///
57    /// # Parameters
58    ///
59    /// * `supplier` - The supplier that produces the callable result.
60    ///
61    /// # Returns
62    ///
63    /// A new `ArcCallable<R, E>`.
64    #[inline]
65    pub fn from_supplier<S>(supplier: S) -> Self
66    where
67        S: Supplier<Result<R, E>> + Send + '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 ArcCallable<R, E> {
76    /// Executes the thread-safe callable.
77    #[inline]
78    fn call(&mut self) -> Result<R, E> {
79        (self.function.lock())()
80    }
81
82    impl_arc_conversions!(
83        ArcCallable<R, E>,
84        BoxCallable,
85        RcCallable,
86        BoxCallableOnce,
87        FnMut() -> Result<R, E>
88    );
89
90    /// Converts this shared callable into a boxed runnable while preserving its
91    /// name.
92    #[inline]
93    fn into_runnable(self) -> BoxRunnable<E>
94    where
95        Self: Sized + 'static,
96    {
97        let name = self.name;
98        let function = self.function;
99        BoxRunnable::new_with_optional_name(move || (function.lock())().map(|_| ()), name)
100    }
101}
102
103impl_closure_trait!(
104    Callable<R, E>,
105    call,
106    BoxCallableOnce,
107    FnMut() -> Result<R, E>
108);
109
110impl_function_debug_display!(BoxCallable<R, E>);
111impl_function_debug_display!(RcCallable<R, E>);
112impl_function_debug_display!(ArcCallable<R, E>);