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