Skip to main content

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