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