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