Skip to main content

qubit_function/consumers/bi_consumer_once/
box_bi_consumer_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 `BoxBiConsumerOnce` public type.
12
13use super::{
14    BiConsumerOnce,
15    BiConsumerOnceFn,
16    BiPredicate,
17    BoxConditionalBiConsumerOnce,
18    impl_box_consumer_methods,
19    impl_box_once_conversions,
20    impl_closure_once_trait,
21    impl_consumer_common_methods,
22    impl_consumer_debug_display,
23};
24
25// =======================================================================
26// 2. BoxBiConsumerOnce - Single Ownership Implementation
27// =======================================================================
28
29/// BoxBiConsumerOnce struct
30///
31/// A one-time bi-consumer implementation based on
32/// `Box<dyn FnOnce(&T, &U)>` for single ownership scenarios. This is the
33/// simplest one-time bi-consumer type for truly one-time use.
34///
35/// # Features
36///
37/// - **Single Ownership**: Not cloneable, ownership moves on use
38/// - **Zero Overhead**: No reference counting or locking
39/// - **One-Time Use**: Consumes self on first call
40/// - **Builder Pattern**: Method chaining consumes `self` naturally
41///
42/// # Use Cases
43///
44/// Choose `BoxBiConsumerOnce` when:
45/// - The bi-consumer is truly used only once
46/// - Building pipelines where ownership naturally flows
47/// - The consumer captures values that should be consumed
48/// - Performance is critical and sharing overhead is unacceptable
49///
50/// # Performance
51///
52/// `BoxBiConsumerOnce` has the best performance:
53/// - No reference counting overhead
54/// - No lock acquisition or runtime borrow checking
55/// - Direct function call through vtable
56/// - Minimal memory footprint (single pointer)
57///
58/// # Examples
59///
60/// ```rust
61/// use qubit_function::{BiConsumerOnce, BoxBiConsumerOnce};
62///
63/// let consumer = BoxBiConsumerOnce::new(|x: &i32, y: &i32| {
64///     println!("Sum: {}", x + y);
65/// });
66/// consumer.accept(&5, &3);
67/// ```
68///
69pub struct BoxBiConsumerOnce<T, U> {
70    pub(super) function: Box<BiConsumerOnceFn<T, U>>,
71    pub(super) name: Option<String>,
72}
73
74// All methods require T: 'static and U: 'static because
75// Box<dyn FnOnce(&T, &U)> requires it
76impl<T, U> BoxBiConsumerOnce<T, U> {
77    // Generates: new(), new_with_name(), name(), set_name(), noop()
78    impl_consumer_common_methods!(
79        BoxBiConsumerOnce<T, U>,
80        (FnOnce(&T, &U) + 'static),
81        |f| Box::new(f)
82    );
83
84    // Generates: when() and and_then() methods that consume self
85    impl_box_consumer_methods!(
86        BoxBiConsumerOnce<T, U>,
87        BoxConditionalBiConsumerOnce,
88        BiConsumerOnce
89    );
90}
91
92impl<T, U> BiConsumerOnce<T, U> for BoxBiConsumerOnce<T, U> {
93    fn accept(self, first: &T, second: &U) {
94        (self.function)(first, second)
95    }
96
97    impl_box_once_conversions!(
98        BoxBiConsumerOnce<T, U>,
99        BiConsumerOnce,
100        FnOnce(&T, &U)
101    );
102}
103
104// Use macro to generate Debug and Display implementations
105impl_consumer_debug_display!(BoxBiConsumerOnce<T, U>);
106
107// =======================================================================
108// 3. Implement BiConsumerOnce trait for closures
109// =======================================================================
110
111// Implement BiConsumerOnce for all FnOnce(&T, &U) using macro
112impl_closure_once_trait!(
113    BiConsumerOnce<T, U>,
114    accept,
115    BoxBiConsumerOnce,
116    FnOnce(first: &T, second: &U)
117);