qubit_function/consumers/bi_consumer/box_bi_consumer.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2025 - 2026.
4 * Haixing Hu, Qubit Co. Ltd.
5 *
6 * All rights reserved.
7 *
8 ******************************************************************************/
9//! Defines the `BoxBiConsumer` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// =======================================================================
16// 2. BoxBiConsumer - Single Ownership Implementation
17// =======================================================================
18
19/// BoxBiConsumer struct
20///
21/// A non-mutating bi-consumer implementation based on `Box<dyn Fn(&T, &U)>`
22/// for single ownership scenarios.
23///
24/// # Features
25///
26/// - **Single Ownership**: Not cloneable, ownership moves on use
27/// - **Zero Overhead**: No reference counting or locking
28/// - **Shared-reference API**: Invoked through `&self` and shared input
29/// references
30/// - **No Wrapper Interior Mutability**: No need for Mutex or RefCell in the
31/// wrapper
32///
33/// # Use Cases
34///
35/// Choose `BoxBiConsumer` when:
36/// - The non-mutating bi-consumer is used only once or in a linear flow
37/// - No need to share the consumer across contexts
38/// - Pure observation operations like logging
39///
40/// # Examples
41///
42/// ```rust
43/// use qubit_function::{BiConsumer, BoxBiConsumer};
44///
45/// let consumer = BoxBiConsumer::new(|x: &i32, y: &i32| {
46/// println!("Sum: {}", x + y);
47/// });
48/// consumer.accept(&5, &3);
49/// ```
50///
51/// # Author
52///
53/// Haixing Hu
54pub struct BoxBiConsumer<T, U> {
55 pub(super) function: Box<BiConsumerFn<T, U>>,
56 pub(super) name: Option<String>,
57}
58
59impl<T, U> BoxBiConsumer<T, U> {
60 // Generates: new(), new_with_name(), name(), set_name(), noop()
61 impl_consumer_common_methods!(
62 BoxBiConsumer<T, U>,
63 (Fn(&T, &U) + 'static),
64 |f| Box::new(f)
65 );
66
67 // Generates: when() and and_then() methods that consume self
68 impl_box_consumer_methods!(
69 BoxBiConsumer<T, U>,
70 BoxConditionalBiConsumer,
71 BiConsumer
72 );
73}
74
75impl<T, U> BiConsumer<T, U> for BoxBiConsumer<T, U> {
76 fn accept(&self, first: &T, second: &U) {
77 (self.function)(first, second)
78 }
79
80 // Generates: into_box(), into_rc(), into_fn(), into_once()
81 impl_box_conversions!(
82 BoxBiConsumer<T, U>,
83 RcBiConsumer,
84 Fn(&T, &U),
85 BoxBiConsumerOnce
86 );
87}
88
89// Use macro to generate Debug and Display implementations
90impl_consumer_debug_display!(BoxBiConsumer<T, U>);