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