Skip to main content

qubit_function/consumers/consumer/
arc_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 `ArcConsumer` public type.
12
13use super::{
14    Arc,
15    ArcConditionalConsumer,
16    BoxConsumer,
17    BoxConsumerOnce,
18    Consumer,
19    Predicate,
20    RcConsumer,
21    impl_arc_conversions,
22    impl_closure_trait,
23    impl_consumer_clone,
24    impl_consumer_common_methods,
25    impl_consumer_debug_display,
26    impl_shared_consumer_methods,
27};
28
29// ============================================================================
30// 4. ArcConsumer - Thread-safe Shared Ownership Implementation
31// ============================================================================
32
33/// ArcConsumer struct
34///
35/// Non-mutating consumer implementation based on `Arc<dyn Fn(&T) + Send + Sync>`,
36/// for thread-safe shared ownership scenarios. The wrapper does not need
37/// `Mutex` because it only invokes a shared `Fn`.
38///
39/// # Features
40///
41/// - **Shared Ownership**: Cloneable through `Arc`, allows multiple owners
42/// - **Thread Safe**: Implements `Send + Sync`, can be safely used concurrently
43/// - **Lock-free Wrapper**: No Mutex protection needed by the wrapper
44/// - **Non-consuming API**: `and_then` borrows `&self`, original object remains
45///   usable
46///
47/// # Use Cases
48///
49/// Choose `ArcConsumer` when:
50/// - Need to share non-mutating consumer across multiple threads
51/// - Pure observation operations, such as logging, monitoring, notifications
52/// - Need high-concurrency reads with no lock overhead
53///
54/// # Performance Advantages
55///
56/// Compared to `ArcStatefulConsumer`, `ArcConsumer` has no Mutex lock overhead,
57/// performing better in high-concurrency observation scenarios.
58///
59/// # Examples
60///
61/// ```rust
62/// use qubit_function::{Consumer, ArcConsumer};
63///
64/// let consumer = ArcConsumer::new(|x: &i32| {
65///     println!("Observed: {}", x);
66/// });
67/// let clone = consumer.clone();
68///
69/// consumer.accept(&5);
70/// clone.accept(&10);
71/// ```
72///
73pub struct ArcConsumer<T> {
74    pub(super) function: Arc<dyn Fn(&T) + Send + Sync>,
75    pub(super) name: Option<String>,
76}
77
78impl<T> ArcConsumer<T> {
79    // Generates: new(), new_with_name(), name(), set_name(), noop()
80    impl_consumer_common_methods!(ArcConsumer<T>, (Fn(&T) + Send + Sync + 'static), |f| {
81        Arc::new(f)
82    });
83
84    // Generates: when() and and_then() methods that borrow &self (Arc can clone)
85    impl_shared_consumer_methods!(
86        ArcConsumer<T>,
87        ArcConditionalConsumer,
88        into_arc,
89        Consumer,
90        Send + Sync + 'static
91    );
92}
93
94impl<T> Consumer<T> for ArcConsumer<T> {
95    fn accept(&self, value: &T) {
96        (self.function)(value)
97    }
98
99    // Use macro to implement conversion methods
100    impl_arc_conversions!(
101        ArcConsumer<T>,
102        BoxConsumer,
103        RcConsumer,
104        BoxConsumerOnce,
105        Fn(t: &T)
106    );
107}
108
109// Use macro to generate Clone implementation
110impl_consumer_clone!(ArcConsumer<T>);
111
112// Use macro to generate Debug and Display implementations
113impl_consumer_debug_display!(ArcConsumer<T>);
114
115// ============================================================================
116// 5. Implement Consumer trait for closures
117// ============================================================================
118
119// Implement Consumer for all Fn(&T)
120impl_closure_trait!(
121    Consumer<T>,
122    accept,
123    BoxConsumerOnce,
124    Fn(value: &T)
125);