Skip to main content

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