1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// =============================================================================
// Copyright (c) 2025 - 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Defines the `ArcConditionalBiConsumer` public type.
use crateArcBiConsumer;
use crateArcBiPredicate;
use crateBiConsumer;
use crateBiPredicate;
use crateimpl_conditional_consumer_clone;
use crateimpl_conditional_consumer_debug_display;
use crateimpl_shared_conditional_consumer;
// =======================================================================
// 8. ArcConditionalBiConsumer - Arc-based Conditional BiConsumer
// =======================================================================
/// A conditional bi-consumer that shares its callback and predicate through
/// `Arc`, and invokes the callback only when the predicate accepts both inputs.
/// The callback and predicate must be thread-safe and `'static`.
///
/// # Type Parameters
///
/// * `T` - The type of the first borrowed input.
/// * `U` - The type of the second borrowed input.
///
/// # Features
///
/// - **Shared Ownership**: Cloneable through `Arc`, allows multiple owners
/// - **Thread Safe**: Implements `Send + Sync`, can be safely used concurrently
/// - **Conditional Execution**: Only consumes when predicate returns `true`
/// - **Implements BiConsumer**: Can be used anywhere a `BiConsumer` is expected
/// - **Shared access**: Receives shared references; interior mutability and
/// external side effects remain possible
///
/// # Examples
///
/// ```rust
/// use std::sync::Arc;
/// use std::sync::atomic::AtomicI32;
/// use std::sync::atomic::Ordering;
///
/// use qubit_function::ArcBiConsumer;
/// use qubit_function::BiConsumer;
///
/// let total = Arc::new(AtomicI32::new(0));
/// let callback_total = Arc::clone(&total);
/// let consumer = ArcBiConsumer::new(move |first: &i32, second: &i32| {
/// callback_total.fetch_add(first + second, Ordering::SeqCst);
/// })
/// .when(|first: &i32, second: &i32| *first > 0 && *second > 0);
///
/// consumer.accept(&2, &3);
/// consumer.accept(&-1, &5);
/// assert_eq!(total.load(Ordering::SeqCst), 5);
/// ```
// Use macro to generate conditional bi-consumer implementations
impl_shared_conditional_consumer!;
// Hand-written BiConsumer trait implementation
// Use macro to generate Clone implementation
impl_conditional_consumer_clone!;
// Use macro to generate Debug and Display implementations
impl_conditional_consumer_debug_display!;