qubit_function/consumers/consumer/rc_conditional_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 `RcConditionalConsumer` public type.
12
13use super::{
14 BoxConsumer,
15 Consumer,
16 Predicate,
17 RcConsumer,
18 RcPredicate,
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// 8. RcConditionalConsumer - Rc-based Conditional Consumer
27// ============================================================================
28
29/// RcConditionalConsumer struct
30///
31/// A conditional non-mutating consumer that only executes when a predicate is satisfied.
32/// Uses `RcConsumer` and `RcPredicate` for single-threaded shared ownership semantics.
33///
34/// This type is typically created by calling `RcConsumer::when()` and is
35/// designed to work with the `or_else()` method to create if-then-else logic.
36///
37/// # Features
38///
39/// - **Shared Ownership**: Cloneable through `Rc`, allows multiple owners
40/// - **Single-threaded**: Not thread-safe, cannot be sent across threads
41/// - **Conditional Execution**: Only consumes when predicate returns `true`
42/// - **Chainable**: Can add `or_else` branch to create if-then-else logic
43/// - **Implements Consumer**: Can be used anywhere a `Consumer` is expected
44/// - **Non-mutating**: Neither modifies itself nor input values
45///
46/// # Examples
47///
48/// ## Basic Conditional Execution
49///
50/// ```rust
51/// use qubit_function::{Consumer, RcConsumer};
52///
53/// let consumer = RcConsumer::new(|x: &i32| {
54/// println!("Positive: {}", x);
55/// });
56/// let conditional = consumer.when(|x: &i32| *x > 0);
57///
58/// conditional.accept(&5); // Prints: Positive: 5
59/// conditional.accept(&-5); // Does nothing
60/// ```
61///
62/// ## With or_else Branch
63///
64/// ```rust
65/// use qubit_function::{Consumer, RcConsumer};
66///
67/// let consumer = RcConsumer::new(|x: &i32| {
68/// println!("Positive: {}", x);
69/// })
70/// .when(|x: &i32| *x > 0)
71/// .or_else(|x: &i32| {
72/// println!("Non-positive: {}", x);
73/// });
74///
75/// consumer.accept(&5); // Prints: Positive: 5
76/// consumer.accept(&-5); // Prints: Non-positive: -5
77/// ```
78///
79pub struct RcConditionalConsumer<T> {
80 pub(super) consumer: RcConsumer<T>,
81 pub(super) predicate: RcPredicate<T>,
82}
83
84// Use macro to generate conditional consumer implementations
85impl_shared_conditional_consumer!(
86 RcConditionalConsumer<T>,
87 RcConsumer,
88 Consumer,
89 into_rc,
90 'static
91);
92
93// Hand-written Consumer trait implementation
94impl<T> Consumer<T> for RcConditionalConsumer<T> {
95 fn accept(&self, value: &T) {
96 if self.predicate.test(value) {
97 self.consumer.accept(value);
98 }
99 }
100
101 // Generates: into_box(), into_rc(), into_fn()
102 impl_conditional_consumer_conversions!(BoxConsumer<T>, RcConsumer, Fn);
103}
104
105// Use macro to generate Clone implementation
106impl_conditional_consumer_clone!(RcConditionalConsumer<T>);
107
108// Use macro to generate Debug and Display implementations
109impl_conditional_consumer_debug_display!(RcConditionalConsumer<T>);