Skip to main content

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