Skip to main content

qubit_function/consumers/consumer/
box_conditional_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 `BoxConditionalConsumer` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// ============================================================================
16// 7. BoxConditionalConsumer - Box-based Conditional Consumer
17// ============================================================================
18
19/// BoxConditionalConsumer struct
20///
21/// A conditional non-mutating consumer that only executes when a predicate is satisfied.
22/// Uses `BoxConsumer` and `BoxPredicate` for single ownership semantics.
23///
24/// This type is typically created by calling `BoxConsumer::when()` and is
25/// designed to work with the `or_else()` method to create if-then-else logic.
26///
27/// # Features
28///
29/// - **Single Ownership**: Not cloneable, consumes `self` on use
30/// - **Conditional Execution**: Only consumes when predicate returns `true`
31/// - **Chainable**: Can add `or_else` branch to create if-then-else logic
32/// - **Implements Consumer**: Can be used anywhere a `Consumer` is expected
33/// - **Non-mutating**: Neither modifies itself nor input values
34///
35/// # Examples
36///
37/// ## Basic Conditional Execution
38///
39/// ```rust
40/// use qubit_function::{Consumer, BoxConsumer};
41///
42/// let consumer = BoxConsumer::new(|x: &i32| {
43///     println!("Positive: {}", x);
44/// });
45/// let conditional = consumer.when(|x: &i32| *x > 0);
46///
47/// conditional.accept(&5);  // Prints: Positive: 5
48/// conditional.accept(&-5); // Does nothing
49/// ```
50///
51/// ## With or_else Branch
52///
53/// ```rust
54/// use qubit_function::{Consumer, BoxConsumer};
55///
56/// let consumer = BoxConsumer::new(|x: &i32| {
57///     println!("Positive: {}", x);
58/// })
59/// .when(|x: &i32| *x > 0)
60/// .or_else(|x: &i32| {
61///     println!("Non-positive: {}", x);
62/// });
63///
64/// consumer.accept(&5);  // Prints: Positive: 5
65/// consumer.accept(&-5); // Prints: Non-positive: -5
66/// ```
67///
68/// # Author
69///
70/// Haixing Hu
71pub struct BoxConditionalConsumer<T> {
72    pub(super) consumer: BoxConsumer<T>,
73    pub(super) predicate: BoxPredicate<T>,
74}
75
76// Use macro to generate conditional consumer implementations
77impl_box_conditional_consumer!(BoxConditionalConsumer<T>, BoxConsumer, Consumer);
78
79// Consumer trait implementation
80impl<T> Consumer<T> for BoxConditionalConsumer<T> {
81    fn accept(&self, value: &T) {
82        if self.predicate.test(value) {
83            self.consumer.accept(value);
84        }
85    }
86
87    // Generates: into_box(), into_rc(), into_fn()
88    impl_conditional_consumer_conversions!(BoxConsumer<T>, RcConsumer, Fn);
89}
90
91// Use macro to generate Debug and Display implementations
92impl_conditional_consumer_debug_display!(BoxConditionalConsumer<T>);