qubit_function/consumers/stateful_consumer/arc_conditional_stateful_consumer.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2025 - 2026.
4 * Haixing Hu, Qubit Co. Ltd.
5 *
6 * All rights reserved.
7 *
8 ******************************************************************************/
9//! Defines the `ArcConditionalStatefulConsumer` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// ============================================================================
16// 8. ArcConditionalStatefulConsumer - Arc-based Conditional Consumer
17// ============================================================================
18
19/// ArcConditionalStatefulConsumer struct
20///
21/// A thread-safe conditional consumer that only executes when a predicate is
22/// satisfied. Uses `ArcStatefulConsumer` and `ArcPredicate` for shared ownership across
23/// threads.
24///
25/// This type is typically created by calling `ArcStatefulConsumer::when()` and is
26/// designed to work with the `or_else()` method to create if-then-else logic.
27///
28/// # Features
29///
30/// - **Shared Ownership**: Cloneable via `Arc`, multiple owners allowed
31/// - **Thread-Safe**: Implements `Send + Sync`, safe for concurrent use
32/// - **Conditional Execution**: Only consumes when predicate returns `true`
33/// - **Chainable**: Can add `or_else` branch to create if-then-else logic
34///
35/// # Examples
36///
37/// ```rust
38/// use qubit_function::{Consumer, StatefulConsumer, ArcStatefulConsumer};
39/// use std::sync::{Arc, Mutex};
40///
41/// let log = Arc::new(Mutex::new(Vec::new()));
42/// let l = log.clone();
43/// let conditional = ArcStatefulConsumer::new(move |x: &i32| {
44/// l.lock().unwrap().push(*x);
45/// })
46/// .when(|x: &i32| *x > 0);
47///
48/// let conditional_clone = conditional.clone();
49///
50/// let mut value = 5;
51/// let mut m = conditional;
52/// m.accept(&value);
53/// assert_eq!(*log.lock().unwrap(), vec![5]);
54/// ```
55///
56/// # Author
57///
58/// Haixing Hu
59pub struct ArcConditionalStatefulConsumer<T> {
60 pub(super) consumer: ArcStatefulConsumer<T>,
61 pub(super) predicate: ArcPredicate<T>,
62}
63
64// Use macro to generate and_then and or_else methods
65impl_shared_conditional_consumer!(
66 ArcConditionalStatefulConsumer<T>,
67 ArcStatefulConsumer,
68 StatefulConsumer,
69 into_arc,
70 Send + Sync + 'static
71);
72
73impl<T> StatefulConsumer<T> for ArcConditionalStatefulConsumer<T> {
74 fn accept(&mut self, value: &T) {
75 if self.predicate.test(value) {
76 self.consumer.accept(value);
77 }
78 }
79
80 // Generates: into_box(), into_rc(), into_fn()
81 impl_conditional_consumer_conversions!(BoxStatefulConsumer<T>, RcStatefulConsumer, FnMut);
82}
83
84// Use macro to generate Clone implementation
85impl_conditional_consumer_clone!(ArcConditionalStatefulConsumer<T>);
86
87// Use macro to generate Debug and Display implementations
88impl_conditional_consumer_debug_display!(ArcConditionalStatefulConsumer<T>);