Skip to main content

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