qubit_function/consumers/bi_consumer/rc_conditional_bi_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 `RcConditionalBiConsumer` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// =======================================================================
16// 9. RcConditionalBiConsumer - Rc-based Conditional BiConsumer
17// =======================================================================
18
19/// RcConditionalBiConsumer struct
20///
21/// A conditional bi-consumer that wraps an `RcBiConsumer` and only executes
22/// when a predicate is satisfied. Based on `Rc` for single-threaded shared ownership.
23///
24/// # Features
25///
26/// - **Shared Ownership**: Cloneable through `Rc`, allows multiple owners
27/// - **Single-Threaded**: Not thread-safe, more efficient than Arc in single-threaded contexts
28/// - **Conditional Execution**: Only consumes when predicate returns `true`
29/// - **Implements BiConsumer**: Can be used anywhere a `BiConsumer` is expected
30/// - **Non-mutating**: Neither modifies itself nor input values
31///
32/// # Author
33///
34/// Haixing Hu
35pub struct RcConditionalBiConsumer<T, U> {
36 pub(super) consumer: RcBiConsumer<T, U>,
37 pub(super) predicate: RcBiPredicate<T, U>,
38}
39
40// Use macro to generate conditional bi-consumer implementations
41impl_shared_conditional_consumer!(
42 RcConditionalBiConsumer<T, U>,
43 RcBiConsumer,
44 BiConsumer,
45 into_rc,
46 'static
47);
48
49// Hand-written BiConsumer trait implementation
50impl<T, U> BiConsumer<T, U> for RcConditionalBiConsumer<T, U> {
51 fn accept(&self, first: &T, second: &U) {
52 if self.predicate.test(first, second) {
53 self.consumer.accept(first, second);
54 }
55 }
56
57 // Generates: into_box(), into_rc(), into_fn()
58 impl_conditional_consumer_conversions!(
59 BoxBiConsumer<T, U>,
60 RcBiConsumer,
61 Fn
62 );
63}
64
65// Use macro to generate Clone implementation
66impl_conditional_consumer_clone!(RcConditionalBiConsumer<T, U>);
67
68// Use macro to generate Debug and Display implementations
69impl_conditional_consumer_debug_display!(RcConditionalBiConsumer<T, U>);