Skip to main content

qubit_function/consumers/stateful_consumer/
rc_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 `RcConditionalStatefulConsumer` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// ============================================================================
16// 9. RcConditionalStatefulConsumer - Rc-based Conditional Consumer
17// ============================================================================
18
19/// RcConditionalStatefulConsumer struct
20///
21/// A single-threaded conditional consumer that only executes when a predicate is
22/// satisfied. Uses `RcStatefulConsumer` and `RcPredicate` for shared ownership within a
23/// single thread.
24///
25/// This type is typically created by calling `RcStatefulConsumer::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 `ArcConditionalStatefulConsumer`
34///
35/// # Examples
36///
37/// ```rust
38/// use qubit_function::{Consumer, StatefulConsumer, RcStatefulConsumer};
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 = RcStatefulConsumer::new(move |x: &i32| {
45///     l.borrow_mut().push(*x);
46/// })
47/// .when(|x: &i32| *x > 0);
48///
49/// let conditional_clone = conditional.clone();
50///
51/// let mut value = 5;
52/// let mut m = conditional;
53/// m.accept(&value);
54/// assert_eq!(*log.borrow(), vec![5]);
55/// ```
56///
57/// # Author
58///
59/// Haixing Hu
60pub struct RcConditionalStatefulConsumer<T> {
61    pub(super) consumer: RcStatefulConsumer<T>,
62    pub(super) predicate: RcPredicate<T>,
63}
64
65// Use macro to generate and_then and or_else methods
66impl_shared_conditional_consumer!(
67    RcConditionalStatefulConsumer<T>,
68    RcStatefulConsumer,
69    StatefulConsumer,
70    into_rc,
71    'static
72);
73
74impl<T> StatefulConsumer<T> for RcConditionalStatefulConsumer<T> {
75    fn accept(&mut self, value: &T) {
76        if self.predicate.test(value) {
77            self.consumer.accept(value);
78        }
79    }
80
81    // Generates: into_box(), into_rc(), into_fn()
82    impl_conditional_consumer_conversions!(BoxStatefulConsumer<T>, RcStatefulConsumer, FnMut);
83}
84
85// Use macro to generate Clone implementation
86impl_conditional_consumer_clone!(RcConditionalStatefulConsumer<T>);
87
88// Use macro to generate Debug and Display implementations
89impl_conditional_consumer_debug_display!(RcConditionalStatefulConsumer<T>);