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