Skip to main content

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