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