Skip to main content

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