qubit_function/functions/bi_mutating_function/box_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 `BoxBiMutatingFunction` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// ============================================================================
16// BoxBiMutatingFunction - Box<dyn Fn(&mut T, &mut U) -> R>
17// ============================================================================
18
19/// BoxBiMutatingFunction - bi-mutating-function wrapper based on `Box<dyn Fn>`
20///
21/// A bi-mutating-function wrapper that provides single ownership with reusable
22/// computation. Borrows both inputs mutably and can be called multiple times.
23///
24/// # Features
25///
26/// - **Based on**: `Box<dyn Fn(&mut T, &mut U) -> R>`
27/// - **Ownership**: Single ownership, cannot be cloned
28/// - **Reusability**: Can be called multiple times (borrows inputs mutably each time)
29/// - **Thread Safety**: Not thread-safe (no `Send + Sync` requirement)
30///
31/// # Author
32///
33/// Haixing Hu
34pub struct BoxBiMutatingFunction<T, U, R> {
35 pub(super) function: Box<dyn Fn(&mut T, &mut U) -> R>,
36 pub(super) name: Option<String>,
37}
38
39// Implement BoxBiMutatingFunction
40impl<T, U, R> BoxBiMutatingFunction<T, U, R> {
41 // Generates: new(), new_with_name(), new_with_optional_name(), name(), set_name()
42 impl_function_common_methods!(
43 BoxBiMutatingFunction<T, U, R>,
44 (Fn(&mut T, &mut U) -> R + 'static),
45 |f| Box::new(f)
46 );
47
48 // Generates: when(), and_then()
49 impl_box_function_methods!(
50 BoxBiMutatingFunction<T, U, R>,
51 BoxConditionalBiMutatingFunction,
52 MutatingFunction
53 );
54}
55
56// Implement BiMutatingFunction trait for BoxBiMutatingFunction
57impl<T, U, R> BiMutatingFunction<T, U, R> for BoxBiMutatingFunction<T, U, R> {
58 fn apply(&self, first: &mut T, second: &mut U) -> R {
59 (self.function)(first, second)
60 }
61
62 // Generates: into_box(), into_rc(), into_fn(), into_once()
63 impl_box_conversions!(
64 BoxBiMutatingFunction<T, U, R>,
65 RcBiMutatingFunction,
66 Fn(&mut T, &mut U) -> R,
67 BoxBiMutatingFunctionOnce
68 );
69}
70
71// Implement constant method for BoxBiMutatingFunction
72impl_function_constant_method!(BoxBiMutatingFunction<T, U, R>);
73
74// Implement Debug and Display for BoxBiMutatingFunction
75impl_function_debug_display!(BoxBiMutatingFunction<T, U, R>);