Skip to main content

qubit_function/mutators/stateful_mutator/
arc_stateful_mutator.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 `ArcStatefulMutator` public type.
12
13use super::{
14    Arc,
15    ArcConditionalStatefulMutator,
16    ArcMutMutatorFn,
17    BoxMutatorOnce,
18    BoxStatefulMutator,
19    Mutex,
20    Predicate,
21    RcStatefulMutator,
22    StatefulMutator,
23    impl_arc_conversions,
24    impl_closure_trait,
25    impl_mutator_clone,
26    impl_mutator_common_methods,
27    impl_mutator_debug_display,
28    impl_shared_mutator_methods,
29};
30
31// ============================================================================
32// 4. ArcMutator - Thread-Safe Shared Ownership Implementation
33// ============================================================================
34
35/// ArcMutator struct
36///
37/// A mutator implementation based on `Arc<Mutex<dyn FnMut(&mut T) + Send>>`
38/// for thread-safe shared ownership scenarios. This type allows the mutator
39/// to be safely shared and used across multiple threads.
40///
41/// # Features
42///
43/// - **Shared Ownership**: Cloneable via `Arc`, multiple owners allowed
44/// - **Thread-Safe**: Implements `Send + Sync`, safe for concurrent use
45/// - **Interior Mutability**: Uses `Mutex` for safe concurrent mutations
46/// - **Mutable State**: Can modify captured environment via `FnMut`
47/// - **Chainable**: Method chaining via `&self` (non-consuming)
48///
49/// # Use Cases
50///
51/// Choose `ArcMutator` when:
52/// - The mutator needs to be shared across multiple threads
53/// - Concurrent task processing (e.g., thread pools)
54/// - Thread safety is required (Send + Sync)
55///
56/// # Examples
57///
58/// ```rust
59/// use qubit_function::{Mutator, ArcMutator};
60///
61/// let mutator = ArcMutator::new(|x: &mut i32| *x *= 2);
62/// let clone = mutator.clone();
63///
64/// let mut value = 5;
65/// let mut m = mutator;
66/// m.apply(&mut value);
67/// assert_eq!(value, 10);
68/// ```
69///
70pub struct ArcStatefulMutator<T> {
71    pub(super) function: ArcMutMutatorFn<T>,
72    pub(super) name: Option<String>,
73}
74
75impl<T> ArcStatefulMutator<T> {
76    impl_mutator_common_methods!(
77        ArcStatefulMutator<T>,
78        (FnMut(&mut T) + Send + 'static),
79        |f| Arc::new(Mutex::new(f))
80    );
81
82    // Generate shared mutator methods (when, and_then, or_else, conversions)
83    impl_shared_mutator_methods!(
84        ArcStatefulMutator<T>,
85        ArcConditionalStatefulMutator,
86        into_arc,
87        StatefulMutator,
88        Send + Sync + 'static
89    );
90}
91
92impl<T> StatefulMutator<T> for ArcStatefulMutator<T> {
93    fn apply(&mut self, value: &mut T) {
94        (self.function.lock())(value)
95    }
96
97    // Use macro to implement conversion methods
98    impl_arc_conversions!(
99        ArcStatefulMutator<T>,
100        BoxStatefulMutator,
101        RcStatefulMutator,
102        BoxMutatorOnce,
103        FnMut(input: &mut T)
104    );
105}
106
107// Use macro to generate Clone implementation
108impl_mutator_clone!(ArcStatefulMutator<T>);
109
110// Generate Debug and Display trait implementations
111impl_mutator_debug_display!(ArcStatefulMutator<T>);
112
113// ============================================================================
114// 5. Implement Mutator trait for closures
115// ============================================================================
116
117impl_closure_trait!(
118    StatefulMutator<T>,
119    apply,
120    BoxMutatorOnce,
121    FnMut(value: &mut T)
122);