Skip to main content

qubit_function/consumers/stateful_bi_consumer/
rc_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 `RcConditionalStatefulBiConsumer` public type.
12
13use super::{
14    BiPredicate,
15    BoxStatefulBiConsumer,
16    RcBiPredicate,
17    RcStatefulBiConsumer,
18    StatefulBiConsumer,
19    impl_conditional_consumer_clone,
20    impl_conditional_consumer_conversions,
21    impl_conditional_consumer_debug_display,
22    impl_shared_conditional_consumer,
23};
24
25// =======================================================================
26// 9. RcConditionalStatefulBiConsumer - Rc-based Conditional BiConsumer
27// =======================================================================
28
29/// RcConditionalStatefulBiConsumer struct
30///
31/// A single-threaded conditional bi-consumer that only executes when a predicate is
32/// satisfied. Uses `RcStatefulBiConsumer` and `RcBiPredicate` for shared ownership within a
33/// single thread.
34///
35/// This type is typically created by calling `RcStatefulBiConsumer::when()` and is
36/// designed to work with the `or_else()` method to create if-then-else logic.
37///
38/// # Features
39///
40/// - **Shared Ownership**: Cloneable via `Rc`, multiple owners allowed
41/// - **Single-Threaded**: Not thread-safe, cannot be sent across threads
42/// - **Conditional Execution**: Only consumes when predicate returns `true`
43/// - **No Lock Overhead**: More efficient than `ArcConditionalStatefulBiConsumer`
44///
45/// # Examples
46///
47/// ```rust
48/// use qubit_function::{BiConsumer, RcStatefulBiConsumer, StatefulBiConsumer};
49/// use std::rc::Rc;
50/// use std::cell::RefCell;
51///
52/// let log = Rc::new(RefCell::new(Vec::new()));
53/// let l = log.clone();
54/// let conditional = RcStatefulBiConsumer::new(move |x: &i32, y: &i32| {
55///     l.borrow_mut().push(*x + *y);
56/// }).when(|x: &i32, y: &i32| *x > 0 && *y > 0);
57///
58/// let conditional_clone = conditional.clone();
59///
60/// let mut value = 5;
61/// let mut m = conditional;
62/// m.accept(&value, &3);
63/// assert_eq!(*log.borrow(), vec![8]);
64/// ```
65///
66pub struct RcConditionalStatefulBiConsumer<T, U> {
67    pub(super) consumer: RcStatefulBiConsumer<T, U>,
68    pub(super) predicate: RcBiPredicate<T, U>,
69}
70
71// Use macro to generate and_then and or_else methods
72impl_shared_conditional_consumer!(
73    RcConditionalStatefulBiConsumer<T, U>,
74    RcStatefulBiConsumer,
75    StatefulBiConsumer,
76    into_rc,
77    'static
78);
79
80impl<T, U> StatefulBiConsumer<T, U> for RcConditionalStatefulBiConsumer<T, U> {
81    fn accept(&mut self, first: &T, second: &U) {
82        if self.predicate.test(first, second) {
83            self.consumer.accept(first, second);
84        }
85    }
86
87    // Generates: into_box(), into_rc(), into_fn()
88    impl_conditional_consumer_conversions!(
89        BoxStatefulBiConsumer<T, U>,
90        RcStatefulBiConsumer,
91        FnMut
92    );
93}
94
95// Use macro to generate Clone implementation
96impl_conditional_consumer_clone!(RcConditionalStatefulBiConsumer<T, U>);
97
98// Use macro to generate Debug and Display implementations
99impl_conditional_consumer_debug_display!(RcConditionalStatefulBiConsumer<T, U>);