Skip to main content

qubit_function/functions/function/
arc_function.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026.
4 *    Haixing Hu, Qubit Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9//! Defines the `ArcFunction` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// ============================================================================
16// ArcFunction - Arc<dyn Fn(&T) -> R + Send + Sync>
17// ============================================================================
18
19/// ArcFunction - thread-safe function wrapper
20///
21/// A thread-safe, clonable function wrapper suitable for multi-threaded
22/// scenarios. Can be called multiple times and shared across threads.
23///
24/// # Features
25///
26/// - **Based on**: `Arc<dyn Fn(&T) -> R + Send + Sync>`
27/// - **Ownership**: Shared ownership via reference counting
28/// - **Reusability**: Can be called multiple times (each call consumes its
29///   input)
30/// - **Thread Safety**: Thread-safe (`Send + Sync` required)
31/// - **Clonable**: Cheap cloning via `Arc::clone`
32///
33/// # Author
34///
35/// Haixing Hu
36pub struct ArcFunction<T, R> {
37    pub(super) function: Arc<dyn Fn(&T) -> R + Send + Sync>,
38    pub(super) name: Option<String>,
39}
40
41impl<T, R> ArcFunction<T, R> {
42    // Generates: new(), new_with_name(), new_with_optional_name(), name(), set_name()
43    impl_function_common_methods!(
44        ArcFunction<T, R>,
45        (Fn(&T) -> R + Send + Sync + 'static),
46        |f| Arc::new(f)
47    );
48
49    // Generates: when(), and_then(), compose()
50    impl_shared_function_methods!(
51        ArcFunction<T, R>,
52        ArcConditionalFunction,
53        into_arc,
54        Function,
55        Send + Sync + 'static
56    );
57}
58
59// Generates: constant() method for ArcFunction<T, R>
60impl_function_constant_method!(ArcFunction<T, R>, Send + Sync + 'static);
61
62// Generates: identity() method for ArcFunction<T, T>
63impl_function_identity_method!(ArcFunction<T, T>);
64
65// Generates: Clone implementation for ArcFunction<T, R>
66impl_function_clone!(ArcFunction<T, R>);
67
68// Generates: Debug and Display implementations for ArcFunction<T, R>
69impl_function_debug_display!(ArcFunction<T, R>);
70
71// Implement Function trait for ArcFunction<T, R>
72impl<T, R> Function<T, R> for ArcFunction<T, R> {
73    fn apply(&self, t: &T) -> R {
74        (self.function)(t)
75    }
76
77    // Use macro to implement conversion methods
78    impl_arc_conversions!(
79        ArcFunction<T, R>,
80        BoxFunction,
81        RcFunction,
82        BoxFunctionOnce,
83        Fn(t: &T) -> R
84    );
85}
86
87// ============================================================================
88// Blanket implementation for standard Fn trait
89// ============================================================================
90
91// Implement Function<T, R> for any type that implements Fn(&T) -> R
92impl_closure_trait!(
93    Function<T, R>,
94    apply,
95    BoxFunctionOnce,
96    Fn(input: &T) -> R
97);