Skip to main content

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
13#![allow(unused_imports)]
14
15use super::*;
16
17// =======================================================================
18// 8. ArcConditionalStatefulBiConsumer - Arc-based Conditional BiConsumer
19// =======================================================================
20
21/// ArcConditionalStatefulBiConsumer struct
22///
23/// A thread-safe conditional bi-consumer that only executes when a predicate is
24/// satisfied. Uses `ArcStatefulBiConsumer` and `ArcBiPredicate` for shared ownership across
25/// threads.
26///
27/// This type is typically created by calling `ArcStatefulBiConsumer::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::{BiConsumer, ArcStatefulBiConsumer, StatefulBiConsumer};
41/// use std::sync::{Arc, Mutex};
42///
43/// let log = Arc::new(Mutex::new(Vec::new()));
44/// let l = log.clone();
45/// let conditional = ArcStatefulBiConsumer::new(move |x: &i32, y: &i32| {
46///     l.lock().unwrap().push(*x + *y);
47/// }).when(|x: &i32, y: &i32| *x > 0 && *y > 0);
48///
49/// let conditional_clone = conditional.clone();
50///
51/// let mut value = 5;
52/// let mut m = conditional;
53/// m.accept(&value, &3);
54/// assert_eq!(*log.lock().unwrap(), vec![8]);
55/// ```
56///
57pub struct ArcConditionalStatefulBiConsumer<T, U> {
58    pub(super) consumer: ArcStatefulBiConsumer<T, U>,
59    pub(super) predicate: ArcBiPredicate<T, U>,
60}
61
62// Use macro to generate and_then and or_else methods
63impl_shared_conditional_consumer!(
64    ArcConditionalStatefulBiConsumer<T, U>,
65    ArcStatefulBiConsumer,
66    StatefulBiConsumer,
67    into_arc,
68    Send + Sync + 'static
69);
70
71impl<T, U> StatefulBiConsumer<T, U> for ArcConditionalStatefulBiConsumer<T, U> {
72    fn accept(&mut self, first: &T, second: &U) {
73        if self.predicate.test(first, second) {
74            self.consumer.accept(first, second);
75        }
76    }
77
78    // Generates: into_box(), into_rc(), into_fn()
79    impl_conditional_consumer_conversions!(
80        BoxStatefulBiConsumer<T, U>,
81        RcStatefulBiConsumer,
82        FnMut
83    );
84}
85
86// Use macro to generate Clone implementation
87impl_conditional_consumer_clone!(ArcConditionalStatefulBiConsumer<T, U>);
88
89// Use macro to generate Debug and Display implementations
90impl_conditional_consumer_debug_display!(ArcConditionalStatefulBiConsumer<T, U>);