qubit_function/consumers/stateful_bi_consumer/arc_conditional_stateful_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 `ArcConditionalStatefulBiConsumer` public type.
12
13use super::{
14 ArcBiPredicate,
15 ArcStatefulBiConsumer,
16 BiPredicate,
17 BoxStatefulBiConsumer,
18 RcStatefulBiConsumer,
19 StatefulBiConsumer,
20 impl_conditional_consumer_clone,
21 impl_conditional_consumer_conversions,
22 impl_conditional_consumer_debug_display,
23 impl_shared_conditional_consumer,
24};
25
26// =======================================================================
27// 8. ArcConditionalStatefulBiConsumer - Arc-based Conditional BiConsumer
28// =======================================================================
29
30/// ArcConditionalStatefulBiConsumer struct
31///
32/// A thread-safe conditional bi-consumer that only executes when a predicate is
33/// satisfied. Uses `ArcStatefulBiConsumer` and `ArcBiPredicate` for shared ownership across
34/// threads.
35///
36/// This type is typically created by calling `ArcStatefulBiConsumer::when()` and is
37/// designed to work with the `or_else()` method to create if-then-else logic.
38///
39/// # Features
40///
41/// - **Shared Ownership**: Cloneable via `Arc`, multiple owners allowed
42/// - **Thread-Safe**: Implements `Send + Sync`, safe for concurrent use
43/// - **Conditional Execution**: Only consumes when predicate returns `true`
44/// - **Chainable**: Can add `or_else` branch to create if-then-else logic
45///
46/// # Examples
47///
48/// ```rust
49/// use qubit_function::{BiConsumer, ArcStatefulBiConsumer, StatefulBiConsumer};
50/// use std::sync::{Arc, Mutex};
51///
52/// let log = Arc::new(Mutex::new(Vec::new()));
53/// let l = log.clone();
54/// let conditional = ArcStatefulBiConsumer::new(move |x: &i32, y: &i32| {
55/// l.lock().expect("mutex should not be poisoned").push(*x + *y);
56/// }).when(|x: &i32, y: &i32| *x > 0 && *y > 0);
57///
58/// let conditional_clone = conditional.clone();
59///
60/// let mut value = 5;
61/// let mut m = conditional;
62/// m.accept(&value, &3);
63/// assert_eq!(*log.lock().expect("mutex should not be poisoned"), vec![8]);
64/// ```
65///
66pub struct ArcConditionalStatefulBiConsumer<T, U> {
67 pub(super) consumer: ArcStatefulBiConsumer<T, U>,
68 pub(super) predicate: ArcBiPredicate<T, U>,
69}
70
71// Use macro to generate and_then and or_else methods
72impl_shared_conditional_consumer!(
73 ArcConditionalStatefulBiConsumer<T, U>,
74 ArcStatefulBiConsumer,
75 StatefulBiConsumer,
76 into_arc,
77 Send + Sync + 'static
78);
79
80impl<T, U> StatefulBiConsumer<T, U> for ArcConditionalStatefulBiConsumer<T, U> {
81 fn accept(&mut self, first: &T, second: &U) {
82 if self.predicate.test(first, second) {
83 self.consumer.accept(first, second);
84 }
85 }
86
87 // Generates: into_box(), into_rc(), into_fn()
88 impl_conditional_consumer_conversions!(
89 BoxStatefulBiConsumer<T, U>,
90 RcStatefulBiConsumer,
91 FnMut
92 );
93}
94
95// Use macro to generate Clone implementation
96impl_conditional_consumer_clone!(ArcConditionalStatefulBiConsumer<T, U>);
97
98// Use macro to generate Debug and Display implementations
99impl_conditional_consumer_debug_display!(ArcConditionalStatefulBiConsumer<T, U>);