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