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