Skip to main content

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