qubit_function/consumers/stateful_bi_consumer/box_conditional_stateful_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 `BoxConditionalStatefulBiConsumer` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// =======================================================================
16// 7. BoxConditionalBiConsumer - Box-based Conditional BiConsumer
17// =======================================================================
18
19/// BoxConditionalBiConsumer struct
20///
21/// A conditional bi-consumer that only executes when a predicate is satisfied.
22/// Uses `BoxStatefulBiConsumer` and `BoxBiPredicate` for single ownership semantics.
23///
24/// This type is typically created by calling `BoxStatefulBiConsumer::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 BiConsumer**: Can be used anywhere a `BiConsumer` is expected
33///
34/// # Examples
35///
36/// ## Basic Conditional Execution
37///
38/// ```rust
39/// use qubit_function::{BiConsumer, BoxStatefulBiConsumer, StatefulBiConsumer};
40/// use std::sync::{Arc, Mutex};
41///
42/// let log = Arc::new(Mutex::new(Vec::new()));
43/// let l = log.clone();
44/// let mut consumer = BoxStatefulBiConsumer::new(move |x: &i32, y: &i32| {
45/// l.lock().unwrap().push(*x + *y);
46/// });
47/// let mut conditional = consumer.when(|x: &i32, y: &i32| *x > 0 && *y > 0);
48///
49/// conditional.accept(&5, &3);
50/// assert_eq!(*log.lock().unwrap(), vec![8]); // Executed
51///
52/// conditional.accept(&-5, &3);
53/// assert_eq!(*log.lock().unwrap(), vec![8]); // Not executed
54/// ```
55///
56/// ## With or_else Branch
57///
58/// ```rust
59/// use qubit_function::{BiConsumer, BoxStatefulBiConsumer, StatefulBiConsumer};
60/// use std::sync::{Arc, Mutex};
61///
62/// let log = Arc::new(Mutex::new(Vec::new()));
63/// let l1 = log.clone();
64/// let l2 = log.clone();
65/// let mut consumer = BoxStatefulBiConsumer::new(move |x: &i32, y: &i32| {
66/// l1.lock().unwrap().push(*x + *y);
67/// }).when(|x: &i32, y: &i32| *x > 0 && *y > 0)
68/// .or_else(move |x: &i32, y: &i32| {
69/// l2.lock().unwrap().push(*x * *y);
70/// });
71///
72/// consumer.accept(&5, &3);
73/// assert_eq!(*log.lock().unwrap(), vec![8]); // when branch executed
74///
75/// consumer.accept(&-5, &3);
76/// assert_eq!(*log.lock().unwrap(), vec![8, -15]); // or_else branch executed
77/// ```
78///
79/// # Author
80///
81/// Haixing Hu
82pub struct BoxConditionalStatefulBiConsumer<T, U> {
83 pub(super) consumer: BoxStatefulBiConsumer<T, U>,
84 pub(super) predicate: BoxBiPredicate<T, U>,
85}
86
87// Use macro to generate conditional bi-consumer implementations
88impl_box_conditional_consumer!(
89 BoxConditionalStatefulBiConsumer<T, U>,
90 BoxStatefulBiConsumer,
91 StatefulBiConsumer
92);
93
94impl<T, U> StatefulBiConsumer<T, U> for BoxConditionalStatefulBiConsumer<T, U> {
95 fn accept(&mut self, first: &T, second: &U) {
96 if self.predicate.test(first, second) {
97 self.consumer.accept(first, second);
98 }
99 }
100
101 // Generates: into_box(), into_rc(), into_fn()
102 impl_conditional_consumer_conversions!(
103 BoxStatefulBiConsumer<T, U>,
104 RcStatefulBiConsumer,
105 FnMut
106 );
107}
108
109// Use macro to generate Debug and Display implementations
110impl_conditional_consumer_debug_display!(BoxConditionalStatefulBiConsumer<T, U>);