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