qubit_function/consumers/bi_consumer/box_conditional_bi_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 `BoxConditionalBiConsumer` public type.
12
13use super::{
14 BiConsumer,
15 BiPredicate,
16 BoxBiConsumer,
17 BoxBiPredicate,
18 RcBiConsumer,
19 impl_box_conditional_consumer,
20 impl_conditional_consumer_conversions,
21 impl_conditional_consumer_debug_display,
22};
23
24// =======================================================================
25// 7. BoxConditionalBiConsumer - Box-based Conditional BiConsumer
26// =======================================================================
27
28/// BoxConditionalBiConsumer struct
29///
30/// A conditional non-mutating bi-consumer that only executes when a predicate is satisfied.
31/// Uses `BoxBiConsumer` and `BoxBiPredicate` for single ownership semantics.
32///
33/// This type is typically created by calling `BoxBiConsumer::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 BiConsumer**: Can be used anywhere a `BiConsumer` 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::{BiConsumer, BoxBiConsumer};
50///
51/// let consumer = BoxBiConsumer::new(|x: &i32, y: &i32| {
52/// println!("Both positive: {} + {} = {}", x, y, x + y);
53/// });
54/// let conditional = consumer.when(|x: &i32, y: &i32| *x > 0 && *y > 0);
55///
56/// conditional.accept(&5, &3); // Prints: Both positive: 5 + 3 = 8
57/// conditional.accept(&-5, &3); // Does nothing
58/// ```
59///
60/// ## With or_else Branch
61///
62/// ```rust
63/// use qubit_function::{BiConsumer, BoxBiConsumer};
64///
65/// let consumer = BoxBiConsumer::new(|x: &i32, y: &i32| {
66/// println!("Both positive: {} + {} = {}", x, y, x + y);
67/// })
68/// .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
69/// .or_else(|x: &i32, y: &i32| {
70/// println!("Not both positive: {} and {}", x, y);
71/// });
72///
73/// consumer.accept(&5, &3); // Prints: Both positive: 5 + 3 = 8
74/// consumer.accept(&-5, &3); // Prints: Not both positive: -5 and 3
75/// ```
76///
77pub struct BoxConditionalBiConsumer<T, U> {
78 pub(super) consumer: BoxBiConsumer<T, U>,
79 pub(super) predicate: BoxBiPredicate<T, U>,
80}
81
82// Use macro to generate conditional bi-consumer implementations
83impl_box_conditional_consumer!(
84 BoxConditionalBiConsumer<T, U>,
85 BoxBiConsumer,
86 BiConsumer
87);
88
89// Hand-written BiConsumer trait implementation
90impl<T, U> BiConsumer<T, U> for BoxConditionalBiConsumer<T, U> {
91 fn accept(&self, first: &T, second: &U) {
92 if self.predicate.test(first, second) {
93 self.consumer.accept(first, second);
94 }
95 }
96
97 // Generates: into_box(), into_rc(), into_fn()
98 impl_conditional_consumer_conversions!(
99 BoxBiConsumer<T, U>,
100 RcBiConsumer,
101 Fn
102 );
103}
104
105// Use macro to generate Debug and Display implementations
106impl_conditional_consumer_debug_display!(BoxConditionalBiConsumer<T, U>);