Skip to main content

qubit_function/functions/bi_mutating_function/
arc_bi_mutating_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 `ArcBiMutatingFunction` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// ============================================================================
16// ArcBiMutatingFunction - Arc<dyn Fn(&mut T, &mut U) -> R + Send + Sync>
17// ============================================================================
18
19/// ArcBiMutatingFunction - thread-safe bi-mutating-function wrapper
20///
21/// A thread-safe, clonable bi-mutating-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(&mut T, &mut U) -> R + Send + Sync>`
27/// - **Ownership**: Shared ownership via reference counting
28/// - **Reusability**: Can be called multiple times (borrows inputs mutably 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 ArcBiMutatingFunction<T, U, R> {
36    pub(super) function: Arc<dyn Fn(&mut T, &mut U) -> R + Send + Sync>,
37    pub(super) name: Option<String>,
38}
39
40impl<T, U, R> ArcBiMutatingFunction<T, U, R> {
41    // Generates: new(), new_with_name(), new_with_optional_name(), name(), set_name()
42    impl_function_common_methods!(
43        ArcBiMutatingFunction<T, U, R>,
44        (Fn(&mut T, &mut U) -> R + Send + Sync + 'static),
45        |f| Arc::new(f)
46    );
47
48    // Generate into_box(), into_rc(), into_fn(), into_once(), to_box(), to_rc(), to_fn(), to_once()
49    impl_shared_function_methods!(
50        ArcBiMutatingFunction<T, U, R>,
51        ArcConditionalBiMutatingFunction,
52        into_arc,
53        MutatingFunction,
54        Send + Sync + 'static
55    );
56}
57
58// Implement BiMutatingFunction trait for ArcBiMutatingFunction
59impl<T, U, R> BiMutatingFunction<T, U, R> for ArcBiMutatingFunction<T, U, R> {
60    fn apply(&self, first: &mut T, second: &mut U) -> R {
61        (self.function)(first, second)
62    }
63
64    // Generate into_box(), into_rc(), into_fn(), into_once(), to_box(), to_rc(), to_fn(), to_once()
65    impl_arc_conversions!(
66        ArcBiMutatingFunction<T, U, R>,
67        BoxBiMutatingFunction,
68        RcBiMutatingFunction,
69        BoxBiMutatingFunctionOnce,
70        Fn(first: &mut T, second: &mut U) -> R
71    );
72}
73
74// Implement constant method for ArcBiMutatingFunction
75impl_function_constant_method!(ArcBiMutatingFunction<T, U, R>, Send + Sync + 'static);
76
77// Implement Debug and Display for ArcBiMutatingFunction
78impl_function_debug_display!(ArcBiMutatingFunction<T, U, R>);
79
80// Implement Clone for ArcBiMutatingFunction
81impl_function_clone!(ArcBiMutatingFunction<T, U, R>);
82
83// ============================================================================
84// Blanket implementation for standard Fn trait
85// ============================================================================
86
87// Implement BiMutatingFunction<T, U, R> for any type that implements Fn(&mut T, &mut U) -> R
88impl_closure_trait!(
89    BiMutatingFunction<T, U, R>,
90    apply,
91    BoxBiMutatingFunctionOnce,
92    Fn(first: &mut T, second: &mut U) -> R
93);