Skip to main content

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