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!(ArcStatefulMutator<T>, (FnMut(&mut T) + Send + 'static), |f| Arc::new(
77 Mutex::new(f)
78 ));
79
80 // Generate shared mutator methods (when, and_then, or_else, conversions)
81 impl_shared_mutator_methods!(
82 ArcStatefulMutator<T>,
83 ArcConditionalStatefulMutator,
84 into_arc,
85 StatefulMutator,
86 Send + Sync + 'static
87 );
88}
89
90impl<T> StatefulMutator<T> for ArcStatefulMutator<T> {
91 fn apply(&mut self, value: &mut T) {
92 (self.function.lock())(value)
93 }
94
95 // Use macro to implement conversion methods
96 impl_arc_conversions!(
97 ArcStatefulMutator<T>,
98 BoxStatefulMutator,
99 RcStatefulMutator,
100 BoxMutatorOnce,
101 FnMut(input: &mut T)
102 );
103}
104
105// Use macro to generate Clone implementation
106impl_mutator_clone!(ArcStatefulMutator<T>);
107
108// Generate Debug and Display trait implementations
109impl_mutator_debug_display!(ArcStatefulMutator<T>);
110
111// ============================================================================
112// 5. Implement Mutator trait for closures
113// ============================================================================
114
115impl_closure_trait!(
116 StatefulMutator<T>,
117 apply,
118 BoxMutatorOnce,
119 FnMut(value: &mut T)
120);