qubit_function/consumers/stateful_consumer/arc_conditional_stateful_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 `ArcConditionalStatefulConsumer` public type.
12
13use super::{
14 ArcPredicate,
15 ArcStatefulConsumer,
16 BoxStatefulConsumer,
17 Predicate,
18 RcStatefulConsumer,
19 StatefulConsumer,
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. ArcConditionalStatefulConsumer - Arc-based Conditional Consumer
28// ============================================================================
29
30/// ArcConditionalStatefulConsumer struct
31///
32/// A thread-safe conditional consumer that only executes when a predicate is
33/// satisfied. Uses `ArcStatefulConsumer` and `ArcPredicate` for shared ownership across
34/// threads.
35///
36/// This type is typically created by calling `ArcStatefulConsumer::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::{Consumer, StatefulConsumer, ArcStatefulConsumer};
50/// use std::sync::{Arc, Mutex};
51///
52/// let log = Arc::new(Mutex::new(Vec::new()));
53/// let l = log.clone();
54/// let conditional = ArcStatefulConsumer::new(move |x: &i32| {
55/// l.lock().expect("mutex should not be poisoned").push(*x);
56/// })
57/// .when(|x: &i32| *x > 0);
58///
59/// let conditional_clone = conditional.clone();
60///
61/// let mut value = 5;
62/// let mut m = conditional;
63/// m.accept(&value);
64/// assert_eq!(*log.lock().expect("mutex should not be poisoned"), vec![5]);
65/// ```
66///
67pub struct ArcConditionalStatefulConsumer<T> {
68 pub(super) consumer: ArcStatefulConsumer<T>,
69 pub(super) predicate: ArcPredicate<T>,
70}
71
72// Use macro to generate and_then and or_else methods
73impl_shared_conditional_consumer!(
74 ArcConditionalStatefulConsumer<T>,
75 ArcStatefulConsumer,
76 StatefulConsumer,
77 into_arc,
78 Send + Sync + 'static
79);
80
81impl<T> StatefulConsumer<T> for ArcConditionalStatefulConsumer<T> {
82 fn accept(&mut self, value: &T) {
83 if self.predicate.test(value) {
84 self.consumer.accept(value);
85 }
86 }
87
88 // Generates: into_box(), into_rc(), into_fn()
89 impl_conditional_consumer_conversions!(BoxStatefulConsumer<T>, RcStatefulConsumer, FnMut);
90}
91
92// Use macro to generate Clone implementation
93impl_conditional_consumer_clone!(ArcConditionalStatefulConsumer<T>);
94
95// Use macro to generate Debug and Display implementations
96impl_conditional_consumer_debug_display!(ArcConditionalStatefulConsumer<T>);