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