Skip to main content

qubit_function/consumers/bi_consumer/
arc_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 `ArcConditionalBiConsumer` public type.
12
13#![allow(unused_imports)]
14
15use super::*;
16
17// =======================================================================
18// 8. ArcConditionalBiConsumer - Arc-based Conditional BiConsumer
19// =======================================================================
20
21/// ArcConditionalBiConsumer struct
22///
23/// A conditional bi-consumer that wraps an `ArcBiConsumer` and only executes
24/// when a predicate is satisfied. Based on `Arc` for thread-safe shared ownership.
25///
26/// # Features
27///
28/// - **Shared Ownership**: Cloneable through `Arc`, allows multiple owners
29/// - **Thread Safe**: Implements `Send + Sync`, can be safely used concurrently
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 ArcConditionalBiConsumer<T, U> {
35    pub(super) consumer: ArcBiConsumer<T, U>,
36    pub(super) predicate: ArcBiPredicate<T, U>,
37}
38
39// Use macro to generate conditional bi-consumer implementations
40impl_shared_conditional_consumer!(
41    ArcConditionalBiConsumer<T, U>,
42    ArcBiConsumer,
43    BiConsumer,
44    into_arc,
45    Send + Sync + 'static
46);
47
48// Hand-written BiConsumer trait implementation
49impl<T, U> BiConsumer<T, U> for ArcConditionalBiConsumer<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!(ArcConditionalBiConsumer<T, U>);
66
67// Use macro to generate Debug and Display implementations
68impl_conditional_consumer_debug_display!(ArcConditionalBiConsumer<T, U>);