Skip to main content

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