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