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