Skip to main content

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