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