qubit_function/functions/bi_function_once/box_bi_function_once.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2025 - 2026.
4 * Haixing Hu, Qubit Co. Ltd.
5 *
6 * All rights reserved.
7 *
8 ******************************************************************************/
9//! Defines the `BoxBiFunctionOnce` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// ============================================================================
16// BoxBiFunctionOnce - Box<dyn FnOnce(&T, &U) -> R>
17// ============================================================================
18
19/// BoxBiFunctionOnce - consuming bi-function wrapper based on
20/// `Box<dyn FnOnce>`
21///
22/// A bi-function wrapper that provides single ownership with one-time use
23/// semantics. Consumes self and borrows both input values.
24///
25/// # Features
26///
27/// - **Based on**: `Box<dyn FnOnce(&T, &U) -> R>`
28/// - **Ownership**: Single ownership, cannot be cloned
29/// - **Reusability**: Can only be called once (consumes self)
30/// - **Thread Safety**: Not thread-safe (no `Send + Sync` requirement)
31///
32/// # Author
33///
34/// Haixing Hu
35pub struct BoxBiFunctionOnce<T, U, R> {
36 pub(super) function: Box<dyn FnOnce(&T, &U) -> R>,
37 pub(super) name: Option<String>,
38}
39
40// Implement BoxBiFunctionOnce
41impl<T, U, R> BoxBiFunctionOnce<T, U, R> {
42 // Generate new(), new_with_name(), new_with_optional_name(), name(), set_name()
43 impl_function_common_methods!(
44 BoxBiFunctionOnce<T, U, R>,
45 (FnOnce(&T, &U) -> R + 'static),
46 |f| Box::new(f)
47 );
48
49 // Generate when(), and_then()
50 impl_box_function_methods!(
51 BoxBiFunctionOnce<T, U, R>,
52 BoxConditionalBiFunctionOnce,
53 FunctionOnce
54 );
55}
56
57// Implement BiFunctionOnce trait for BoxBiFunctionOnce
58impl<T, U, R> BiFunctionOnce<T, U, R> for BoxBiFunctionOnce<T, U, R> {
59 fn apply(self, first: &T, second: &U) -> R {
60 (self.function)(first, second)
61 }
62
63 // Generate into_box(), into_fn(), to_box()
64 impl_box_once_conversions!(
65 BoxBiFunctionOnce<T, U, R>,
66 BiFunctionOnce,
67 FnOnce(&T, &U) -> R
68 );
69}
70
71// Implement constant method for BoxBiFunctionOnce
72impl_function_constant_method!(BoxBiFunctionOnce<T, U, R>);
73
74// Use macro to generate Debug and Display implementations
75impl_function_debug_display!(BoxBiFunctionOnce<T, U, R>);
76
77// ============================================================================
78// Blanket implementation for standard FnOnce trait
79// ============================================================================
80
81// Implement BiFunctionOnce for all FnOnce(&T, &U) -> R using macro
82impl_closure_once_trait!(
83 BiFunctionOnce<T, U, R>,
84 apply,
85 BoxBiFunctionOnce,
86 FnOnce(first: &T, second: &U) -> R
87);