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