Skip to main content

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