Skip to main content

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