Skip to main content

qubit_function/mutators/mutator/
arc_mutator.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026.
4 *    Haixing Hu, Qubit Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9//! Defines the `ArcMutator` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// ============================================================================
16// 5. ArcMutator - Thread-Safe Shared Ownership Implementation
17// ============================================================================
18
19/// ArcMutator struct
20///
21/// A stateless mutator implementation based on `Arc<dyn Fn(&mut T) + Send + Sync>`
22/// for thread-safe shared ownership scenarios. This type allows the mutator
23/// to be safely shared and used across multiple threads.
24///
25/// # Features
26///
27/// - **Shared Ownership**: Cloneable via `Arc`, multiple owners allowed
28/// - **Thread-Safe**: Implements `Send + Sync`, safe for concurrent use
29/// - **Stateless**: Cannot modify captured environment (uses `Fn` not `FnMut`)
30/// - **Chainable**: Method chaining via `&self` (non-consuming)
31///
32/// # Use Cases
33///
34/// Choose `ArcMutator` when:
35/// - The mutator needs to be shared across multiple threads for stateless operations
36/// - Concurrent task processing (e.g., thread pools)
37/// - Thread safety is required (Send + Sync)
38///
39/// # Examples
40///
41/// ```rust
42/// use qubit_function::{Mutator, ArcMutator};
43///
44/// let mutator = ArcMutator::new(|x: &mut i32| *x *= 2);
45/// let clone = mutator.clone();
46///
47/// let mut value = 5;
48/// mutator.apply(&mut value);
49/// assert_eq!(value, 10);
50/// ```
51///
52/// # Author
53///
54/// Haixing Hu
55pub struct ArcMutator<T> {
56    pub(super) function: ArcMutatorFn<T>,
57    pub(super) name: Option<String>,
58}
59
60impl<T> ArcMutator<T> {
61    // Generate common mutator methods (new, new_with_name, name, set_name, noop)
62    impl_mutator_common_methods!(ArcMutator<T>, (Fn(&mut T) + Send + Sync + 'static), |f| {
63        Arc::new(f)
64    });
65
66    // Generate shared mutator methods (when, and_then, or_else, conversions)
67    impl_shared_mutator_methods!(
68        ArcMutator<T>,
69        ArcConditionalMutator,
70        into_arc,
71        Mutator,
72        Send + Sync + 'static
73    );
74}
75
76impl<T> Mutator<T> for ArcMutator<T> {
77    fn apply(&self, value: &mut T) {
78        (self.function)(value)
79    }
80
81    // Use macro to implement conversion methods
82    impl_arc_conversions!(
83        ArcMutator<T>,
84        BoxMutator,
85        RcMutator,
86        BoxMutatorOnce,
87        Fn(input: &mut T)
88    );
89}
90
91// Generate Clone trait implementation for mutator
92impl_mutator_clone!(ArcMutator<T>);
93
94// Generate Debug and Display trait implementations
95impl_mutator_debug_display!(ArcMutator<T>);
96
97// ============================================================================
98// 6. Implement Mutator trait for closures
99// ============================================================================
100
101impl_closure_trait!(
102    Mutator<T>,
103    apply,
104    BoxMutatorOnce,
105    Fn(value: &mut T)
106);