Skip to main content

qubit_function/tasks/callable_with/
arc_callable_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 `ArcCallableWith` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15/// Thread-safe shared callable with mutable input.
16///
17/// `ArcCallableWith<T, R, E>` stores an
18/// `Arc<Mutex<dyn FnMut(&mut T) -> Result<R, E> + Send>>`.
19///
20/// # Author
21///
22/// Haixing Hu
23pub struct ArcCallableWith<T, R, E> {
24    /// The stateful closure executed by this callable.
25    pub(super) function: Arc<Mutex<dyn FnMut(&mut T) -> Result<R, E> + Send>>,
26    /// The optional name of this callable.
27    pub(super) name: Option<String>,
28}
29
30impl<T, R, E> Clone for ArcCallableWith<T, R, E> {
31    #[inline]
32    fn clone(&self) -> Self {
33        Self {
34            function: Arc::clone(&self.function),
35            name: self.name.clone(),
36        }
37    }
38}
39
40impl<T, R, E> ArcCallableWith<T, R, E> {
41    impl_common_new_methods!(
42        (FnMut(&mut T) -> Result<R, E> + Send + 'static),
43        |function| Arc::new(Mutex::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 ArcCallableWith<T, R, E> {
51    /// Executes the thread-safe callable with mutable input.
52    #[inline]
53    fn call_with(&mut self, input: &mut T) -> Result<R, E> {
54        (self.function.lock())(input)
55    }
56
57    impl_arc_conversions!(
58        ArcCallableWith<T, R, E>,
59        BoxCallableWith,
60        RcCallableWith,
61        FnMut(input: &mut T) -> Result<R, E>
62    );
63
64    /// Converts this shared callable into a boxed runnable while preserving its
65    /// name.
66    #[inline]
67    fn into_runnable_with(self) -> BoxRunnableWith<T, E>
68    where
69        Self: Sized + 'static,
70    {
71        let name = self.name;
72        let function = self.function;
73        BoxRunnableWith::new_with_optional_name(
74            move |input| (function.lock())(input).map(|_| ()),
75            name,
76        )
77    }
78}
79
80impl_closure_trait!(
81    CallableWith<T, R, E>,
82    call_with,
83    FnMut(input: &mut T) -> Result<R, E>
84);
85
86impl_function_debug_display!(BoxCallableWith<T, R, E>);
87impl_function_debug_display!(RcCallableWith<T, R, E>);
88impl_function_debug_display!(ArcCallableWith<T, R, E>);