1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// =============================================================================
// Copyright (c) 2025 - 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! # Stateful Mutator Types
//!
//! Provides `StatefulMutator` implementations for operations that accept a
//! mutable input, may update captured state, and return no result.
//!
//! This module provides a unified `StatefulMutator` trait and three concrete
//! implementations based on different ownership models:
//!
//! - **`BoxStatefulMutator<T>`**: Box-based single ownership implementation for
//! stored callbacks and builder patterns
//! - **`ArcStatefulMutator<T>`**: `Arc<Mutex<_>>`-based thread-safe shared
//! ownership implementation for multi-threaded scenarios
//! - **`RcStatefulMutator<T>`**: `Rc<RefCell<_>>`-based single-threaded shared
//! ownership implementation with no lock overhead
//!
//! It is similar to the `FnMut(&mut T)` trait in the standard library.
//!
//! # Design Philosophy
//!
//! Unlike `Mutator`, which uses `Fn(&mut T)`, `StatefulMutator` uses
//! `FnMut(&mut T)` so the operation can update both the input and its captured
//! environment.
//!
//! | Type | Callback | Modifies Input? | Modifies Captured State? |
//! |------|----------|-----------------|--------------------------|
//! | **Mutator** | `Fn(&mut T)` | ✅ | ❌ |
//! | **StatefulMutator** | `FnMut(&mut T)` | ✅ | ✅ |
//!
//! # Comparison Table
//!
//! | Feature | BoxStatefulMutator | ArcStatefulMutator | RcStatefulMutator |
//! |---------|--------------------|--------------------|-------------------|
//! | Ownership | Single | Shared | Shared |
//! | Cloneable | ❌ | ✅ | ✅ |
//! | Thread-safe | ❌ | ✅ | ❌ |
//! | Interior mutability | None | Mutex | RefCell |
//! | `and_then` receiver | `self` | `&self` | `&self` |
//! | Lock overhead | None | Yes | None |
//!
//! # Use Cases
//!
//! ## BoxStatefulMutator
//!
//! - One-time operations that don't require sharing
//! - Builder patterns where ownership naturally flows
//! - Simple scenarios with no reuse requirements
//!
//! ## ArcStatefulMutator
//!
//! - Multi-threaded shared operations
//! - Concurrent task processing (e.g., thread pools)
//! - Situations requiring the same mutator across threads
//!
//! ## RcStatefulMutator
//!
//! - Single-threaded operations with multiple uses
//! - Event handling in single-threaded UI frameworks
//! - Performance-critical single-threaded scenarios
//!
//! # Examples
//!
//! ## Basic Usage
//!
//! ```rust
//! use qubit_function::{BoxStatefulMutator, StatefulMutator};
//!
//! let mut call_count = 0;
//! let mut mutator = BoxStatefulMutator::new(move |x: &mut i32| {
//! call_count += 1;
//! *x += call_count;
//! });
//! let mut value = 10;
//! mutator.apply(&mut value);
//! assert_eq!(value, 11);
//! mutator.apply(&mut value);
//! assert_eq!(value, 13);
//! ```
//!
//! ## Method Chaining
//!
//! ```rust
//! # {
//! use qubit_function::{BoxStatefulMutator, StatefulMutator};
//!
//! let mut call_count = 0;
//! let mut chained = BoxStatefulMutator::new(move |x: &mut i32| {
//! call_count += 1;
//! *x += call_count;
//! }).and_then(|x: &mut i32| *x *= 2);
//! let mut value = 10;
//! chained.apply(&mut value);
//! assert_eq!(value, 22);
//! # }
//! ```
//!
//! ## Working with Closures
//!
//! `FnMut(&mut T)` closures automatically implement `StatefulMutator`.
//! Chaining starts from a concrete wrapper:
//!
//! ```rust
//! # {
//! use qubit_function::{BoxStatefulMutator, StatefulMutator};
//!
//! let mut call_count = 0;
//! let mut chained = BoxStatefulMutator::new(move |x: &mut i32| {
//! call_count += 1;
//! *x += call_count;
//! }).and_then(|x: &mut i32| *x *= 2);
//! let mut value = 10;
//! chained.apply(&mut value);
//! assert_eq!(value, 22);
//! # }
//! ```
//!
//! ## Wrapper Construction
//!
//! This example additionally requires the `rc` feature.
//!
//! ```rust
//! # #[cfg(feature = "rc")]
//! # {
//! use qubit_function::{ArcStatefulMutator, BoxStatefulMutator, RcStatefulMutator};
//!
//! // Construct each ownership-specific wrapper from a closure.
//! let closure = |x: &mut i32| *x *= 2;
//! let box_mutator = BoxStatefulMutator::new(closure);
//!
//! let closure = |x: &mut i32| *x *= 2;
//! let rc_mutator = RcStatefulMutator::new(closure);
//!
//! let closure = |x: &mut i32| *x *= 2;
//! let arc_mutator = ArcStatefulMutator::new(closure);
//! # }
//! ```
//!
//! ## Conditional Execution
//!
//! Stateful mutator wrappers support conditional execution through `when` and
//! an optional `or_else` branch:
//!
//! ```rust
//! # {
//! use qubit_function::{BoxStatefulMutator, StatefulMutator};
//!
//! let mut call_count = 0;
//! let mut conditional = BoxStatefulMutator::new(move |x: &mut i32| {
//! call_count += 1;
//! *x += call_count;
//! })
//! .when(|x: &i32| *x > 0);
//!
//! let mut positive = 5;
//! conditional.apply(&mut positive);
//! assert_eq!(positive, 6);
//!
//! let mut negative = -5;
//! conditional.apply(&mut negative);
//! assert_eq!(negative, -5);
//!
//! let mut branch_calls = 0;
//! let mut branched = BoxStatefulMutator::new(move |x: &mut i32| {
//! branch_calls += 1;
//! *x += branch_calls;
//! })
//! .when(|x: &i32| *x > 0)
//! .or_else(|x: &mut i32| *x -= 1);
//!
//! let mut positive = 5;
//! branched.apply(&mut positive);
//! assert_eq!(positive, 6);
//!
//! let mut negative = -5;
//! branched.apply(&mut negative);
//! assert_eq!(negative, -6);
//! # }
//! ```
use RefCell;
use Rc;
use Arc;
use Mutex;
// ============================================================================
// 1. Type Aliases
// ============================================================================
/// Type alias for Arc-wrapped mutable mutator function
type ArcMutMutatorFn<T> = ;
/// Type alias for Rc-wrapped mutable mutator function
/// The erased callback representation used by this implementation.
type RcMutMutatorFn<T> = ;
pub use BoxStatefulMutator;
pub use RcStatefulMutator;
pub use ArcStatefulMutator;
pub use BoxConditionalStatefulMutator;
pub use RcConditionalStatefulMutator;
pub use ArcConditionalStatefulMutator;
// ============================================================================
// 2. StatefulMutator Trait - Unified Stateful Mutator Interface
// ============================================================================
/// Unified stateful mutator interface.
///
/// Defines operations that may modify both a mutable input and captured state.
///
/// This trait is automatically implemented by:
/// - All closures implementing `FnMut(&mut T)`
/// - `BoxStatefulMutator<T>`, `ArcStatefulMutator<T>`, and
/// `RcStatefulMutator<T>`
///
/// # Design Rationale
///
/// The trait provides a unified abstraction over different ownership models,
/// allowing generic code to work with any stateful mutator type.
///
/// # Features
///
/// - **Unified Interface**: All stateful mutator types share the same `apply`
/// method signature
/// - **Automatic Implementation**: Closures implement this trait directly,
/// without allocating an adapter
/// - **Generic Programming**: Write functions that work with any mutator type
///
/// # Examples
///
/// ## Generic Stateful Mutator Function
///
/// ```rust
/// use qubit_function::{BoxStatefulMutator, StatefulMutator};
///
/// fn apply_mutator<M: StatefulMutator<i32>>(
/// mutator: &mut M,
/// value: i32
/// ) -> i32 {
/// let mut val = value;
/// mutator.apply(&mut val);
/// val
/// }
///
/// let mut calls = 0;
/// let mut mutator = BoxStatefulMutator::new(move |x: &mut i32| {
/// calls += 1;
/// *x += calls;
/// });
/// assert_eq!(apply_mutator(&mut mutator, 10), 11);
/// assert_eq!(apply_mutator(&mut mutator, 10), 12);
/// ```
///
/// ## Wrapper Construction
///
/// ```rust
/// use qubit_function::BoxStatefulMutator;
///
/// let closure = |x: &mut i32| *x *= 2;
///
/// // Construct a concrete ownership wrapper.
/// let box_mutator = BoxStatefulMutator::new(closure);
/// ```
crateimpl_closure_trait!;