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