qubit_function/functions/bi_mutating_function/rc_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 `RcBiMutatingFunction` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// ============================================================================
16// RcBiMutatingFunction - Rc<dyn Fn(&mut T, &mut U) -> R>
17// ============================================================================
18
19/// RcBiMutatingFunction - single-threaded bi-mutating-function wrapper
20///
21/// A single-threaded, clonable bi-mutating-function wrapper optimized for scenarios
22/// that require sharing without thread-safety overhead.
23///
24/// # Features
25///
26/// - **Based on**: `Rc<dyn Fn(&mut T, &mut U) -> R>`
27/// - **Ownership**: Shared ownership via reference counting (non-atomic)
28/// - **Reusability**: Can be called multiple times (borrows inputs mutably each time)
29/// - **Thread Safety**: Not thread-safe (no `Send + Sync`)
30/// - **Clonable**: Cheap cloning via `Rc::clone`
31///
32/// # Author
33///
34/// Haixing Hu
35pub struct RcBiMutatingFunction<T, U, R> {
36 pub(super) function: Rc<dyn Fn(&mut T, &mut U) -> R>,
37 pub(super) name: Option<String>,
38}
39
40impl<T, U, R> RcBiMutatingFunction<T, U, R> {
41 // Generates: new(), new_with_name(), new_with_optional_name(), name(), set_name()
42 impl_function_common_methods!(
43 RcBiMutatingFunction<T, U, R>,
44 (Fn(&mut T, &mut U) -> R + 'static),
45 |f| Rc::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 RcBiMutatingFunction<T, U, R>,
51 RcConditionalBiMutatingFunction,
52 into_rc,
53 MutatingFunction,
54 'static
55 );
56}
57
58// Implement BiMutatingFunction trait for RcBiMutatingFunction
59impl<T, U, R> BiMutatingFunction<T, U, R> for RcBiMutatingFunction<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_rc_conversions!(
66 RcBiMutatingFunction<T, U, R>,
67 BoxBiMutatingFunction,
68 BoxBiMutatingFunctionOnce,
69 Fn(first: &mut T, second: &mut U) -> R
70 );
71}
72
73// Implement constant method for RcBiMutatingFunction
74impl_function_constant_method!(RcBiMutatingFunction<T, U, R>);
75
76// Implement Debug and Display for RcBiMutatingFunction
77impl_function_debug_display!(RcBiMutatingFunction<T, U, R>);
78
79// Implement Clone for RcBiMutatingFunction
80impl_function_clone!(RcBiMutatingFunction<T, U, R>);