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