qubit_function/mutators/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//! # Mutator Types
11//!
12//! Provides Java-like `Mutator` interface implementations for performing
13//! operations that accept a single mutable input parameter and return no result.
14//!
15//! This module provides a unified `Mutator` trait and three concrete
16//! implementations based on different ownership models:
17//!
18//! - **`BoxMutator<T>`**: Box-based single ownership implementation for
19//! one-time use scenarios and builder patterns
20//! - **`ArcMutator<T>`**: Arc<Mutex<>>-based thread-safe shared ownership
21//! implementation for multi-threaded scenarios
22//! - **`RcMutator<T>`**: Rc<RefCell<>>-based single-threaded shared
23//! ownership implementation with no lock overhead
24//!
25//! It is similar to the `FnMut(&mut T)` trait in the standard library.
26//!
27//! # Design Philosophy
28//!
29//! Unlike `Consumer` which observes values without modifying them (`FnMut(&T)`),
30//! `Mutator` is designed to **modify input values** using `FnMut(&mut T)`.
31//!
32//! ## Mutator vs Consumer
33//!
34//! | Type | Input | Modifies Input? | Modifies Self? | Use Cases |
35//! |------|-------|----------------|----------------|-----------|
36//! | **Consumer** | `&T` | ❌ | ✅ | Observe, log, count, notify |
37//! | **Mutator** | `&mut T` | ✅ | ✅ | Modify, transform, update |
38//!
39//! **Key Insight**: If you need to modify input values, use `Mutator`.
40//! If you only need to observe or accumulate state, use `Consumer`.
41//!
42//! # Comparison Table
43//!
44//! | Feature | BoxMutator | ArcMutator | RcMutator |
45//! |------------------|------------|------------|-----------|
46//! | Ownership | Single | Shared | Shared |
47//! | Cloneable | ❌ | ✅ | ✅ |
48//! | Thread-Safe | ❌ | ✅ | ❌ |
49//! | Interior Mut. | N/A | Mutex | RefCell |
50//! | `and_then` API | `self` | `&self` | `&self` |
51//! | Lock Overhead | None | Yes | None |
52//!
53//! # Use Cases
54//!
55//! ## BoxMutator
56//!
57//! - One-time operations that don't require sharing
58//! - Builder patterns where ownership naturally flows
59//! - Simple scenarios with no reuse requirements
60//!
61//! ## ArcMutator
62//!
63//! - Multi-threaded shared operations
64//! - Concurrent task processing (e.g., thread pools)
65//! - Situations requiring the same mutator across threads
66//!
67//! ## RcMutator
68//!
69//! - Single-threaded operations with multiple uses
70//! - Event handling in single-threaded UI frameworks
71//! - Performance-critical single-threaded scenarios
72//!
73//! # Examples
74//!
75//! ## Basic Usage
76//!
77//! ```rust
78//! use qubit_function::{BoxMutator, ArcMutator, RcMutator, Mutator};
79//!
80//! // BoxMutator: Single ownership, consumes self
81//! let mut mutator = BoxMutator::new(|x: &mut i32| *x *= 2);
82//! let mut value = 5;
83//! mutator.apply(&mut value);
84//! assert_eq!(value, 10);
85//!
86//! // ArcMutator: Shared ownership, cloneable, thread-safe
87//! let shared = ArcMutator::new(|x: &mut i32| *x *= 2);
88//! let clone = shared.clone();
89//! let mut value = 5;
90//! let mut m = shared;
91//! m.apply(&mut value);
92//! assert_eq!(value, 10);
93//!
94//! // RcMutator: Shared ownership, cloneable, single-threaded
95//! let rc = RcMutator::new(|x: &mut i32| *x *= 2);
96//! let clone = rc.clone();
97//! let mut value = 5;
98//! let mut m = rc;
99//! m.apply(&mut value);
100//! assert_eq!(value, 10);
101//! ```
102//!
103//! ## Method Chaining
104//!
105//! ```rust
106//! use qubit_function::{Mutator, BoxMutator, ArcMutator};
107//!
108//! // BoxMutator: Consumes self
109//! let mut chained = BoxMutator::new(|x: &mut i32| *x *= 2)
110//! .and_then(|x: &mut i32| *x += 10);
111//! let mut value = 5;
112//! chained.apply(&mut value);
113//! assert_eq!(value, 20); // (5 * 2) + 10
114//!
115//! // ArcMutator: Borrows &self, original still usable
116//! let first = ArcMutator::new(|x: &mut i32| *x *= 2);
117//! let second = ArcMutator::new(|x: &mut i32| *x += 10);
118//! let combined = first.clone().and_then(second.clone());
119//! let mut value = 5;
120//! combined.apply(&mut value);
121//! assert_eq!(value, 20);
122//! // first and second are still usable here
123//! ```
124//!
125//! ## Working with Closures
126//!
127//! All closures automatically implement the `Mutator` trait:
128//!
129//! ```rust
130//! use qubit_function::{Mutator, FnMutatorOps};
131//!
132//! // Closures can use .apply() directly
133//! let mut closure = |x: &mut i32| *x *= 2;
134//! let mut value = 5;
135//! closure.apply(&mut value);
136//! assert_eq!(value, 10);
137//!
138//! // Closures can be chained, returning BoxMutator
139//! let mut chained = (|x: &mut i32| *x *= 2)
140//! .and_then(|x: &mut i32| *x += 10);
141//! let mut value = 5;
142//! chained.apply(&mut value);
143//! assert_eq!(value, 20);
144//! ```
145//!
146//! ## Type Conversions
147//!
148//! ```rust
149//! use qubit_function::Mutator;
150//!
151//! // Convert closure to concrete type
152//! let closure = |x: &mut i32| *x *= 2;
153//! let mut box_mutator = closure.into_box();
154//!
155//! let closure = |x: &mut i32| *x *= 2;
156//! let mut rc_mutator = closure.into_rc();
157//!
158//! let closure = |x: &mut i32| *x *= 2;
159//! let mut arc_mutator = closure.into_arc();
160//! ```
161//!
162//! ## Conditional Execution
163//!
164//! All mutator types support conditional execution through the `when` method,
165//! which returns a `ConditionalMutator`. You can optionally add an `or_else`
166//! branch to create if-then-else logic:
167//!
168//! ```rust
169//! use qubit_function::{Mutator, BoxMutator};
170//!
171//! // Simple conditional (if-then)
172//! let mut conditional = BoxMutator::new(|x: &mut i32| *x *= 2)
173//! .when(|x: &i32| *x > 0);
174//!
175//! let mut positive = 5;
176//! conditional.apply(&mut positive);
177//! assert_eq!(positive, 10); // Executed
178//!
179//! let mut negative = -5;
180//! conditional.apply(&mut negative);
181//! assert_eq!(negative, -5); // Not executed
182//!
183//! // Conditional with else branch (if-then-else)
184//! let mut branched = BoxMutator::new(|x: &mut i32| *x *= 2)
185//! .when(|x: &i32| *x > 0)
186//! .or_else(|x: &mut i32| *x -= 1);
187//!
188//! let mut positive = 5;
189//! branched.apply(&mut positive);
190//! assert_eq!(positive, 10); // when branch
191//!
192//! let mut negative = -5;
193//! branched.apply(&mut negative);
194//! assert_eq!(negative, -6); // or_else branch
195//! ```
196//!
197use std::cell::RefCell;
198use std::rc::Rc;
199use std::sync::Arc;
200
201use parking_lot::Mutex;
202
203use crate::macros::{
204 impl_arc_conversions,
205 impl_box_conversions,
206 impl_closure_trait,
207 impl_rc_conversions,
208};
209use crate::mutators::{
210 macros::{
211 impl_box_conditional_mutator,
212 impl_box_mutator_methods,
213 impl_conditional_mutator_clone,
214 impl_conditional_mutator_conversions,
215 impl_conditional_mutator_debug_display,
216 impl_mutator_clone,
217 impl_mutator_common_methods,
218 impl_mutator_debug_display,
219 impl_shared_conditional_mutator,
220 impl_shared_mutator_methods,
221 },
222 mutator_once::BoxMutatorOnce,
223};
224use crate::predicates::predicate::{
225 ArcPredicate,
226 BoxPredicate,
227 Predicate,
228 RcPredicate,
229};
230
231// ============================================================================
232// 1. Type Aliases
233// ============================================================================
234
235/// Type alias for Arc-wrapped mutable mutator function
236type ArcMutMutatorFn<T> = Arc<Mutex<dyn FnMut(&mut T) + Send>>;
237
238/// Type alias for Rc-wrapped mutable mutator function
239type RcMutMutatorFn<T> = Rc<RefCell<dyn FnMut(&mut T)>>;
240
241mod box_stateful_mutator;
242pub use box_stateful_mutator::BoxStatefulMutator;
243mod rc_stateful_mutator;
244pub use rc_stateful_mutator::RcStatefulMutator;
245mod arc_stateful_mutator;
246pub use arc_stateful_mutator::ArcStatefulMutator;
247mod fn_mut_stateful_mutator_ops;
248pub use fn_mut_stateful_mutator_ops::FnMutStatefulMutatorOps;
249mod box_conditional_stateful_mutator;
250pub use box_conditional_stateful_mutator::BoxConditionalStatefulMutator;
251mod rc_conditional_stateful_mutator;
252pub use rc_conditional_stateful_mutator::RcConditionalStatefulMutator;
253mod arc_conditional_stateful_mutator;
254pub use arc_conditional_stateful_mutator::ArcConditionalStatefulMutator;
255
256// ============================================================================
257// 2. Mutator Trait - Unified Mutator Interface
258// ============================================================================
259
260/// Mutator trait - Unified mutator interface
261///
262/// Defines the core behavior of all mutator types. Performs operations that
263/// accept a mutable reference and modify the input value (not just side effects).
264///
265/// This trait is automatically implemented by:
266/// - All closures implementing `FnMut(&mut T)`
267/// - `BoxMutator<T>`, `ArcMutator<T>`, and `RcMutator<T>`
268///
269/// # Design Rationale
270///
271/// The trait provides a unified abstraction over different ownership models,
272/// allowing generic code to work with any mutator type. Type conversion
273/// methods (`into_box`, `into_arc`, `into_rc`) enable flexible ownership
274/// transitions based on usage requirements.
275///
276/// # Features
277///
278/// - **Unified Interface**: All mutator types share the same `mutate`
279/// method signature
280/// - **Automatic Implementation**: Closures automatically implement this
281/// trait with zero overhead
282/// - **Type Conversions**: Easy conversion between ownership models
283/// - **Generic Programming**: Write functions that work with any mutator
284/// type
285///
286/// # Examples
287///
288/// ## Generic Mutator Function
289///
290/// ```rust
291/// use qubit_function::{Mutator, BoxMutator, ArcMutator};
292///
293/// fn apply_mutator<M: Mutator<i32>>(
294/// mutator: &mut M,
295/// value: i32
296/// ) -> i32 {
297/// let mut val = value;
298/// mutator.apply(&mut val);
299/// val
300/// }
301///
302/// // Works with any mutator type
303/// let mut box_mut = BoxMutator::new(|x: &mut i32| *x *= 2);
304/// assert_eq!(apply_mutator(&mut box_mut, 5), 10);
305///
306/// let mut arc_mut = ArcMutator::new(|x: &mut i32| *x *= 2);
307/// assert_eq!(apply_mutator(&mut arc_mut, 5), 10);
308///
309/// let mut closure = |x: &mut i32| *x *= 2;
310/// assert_eq!(apply_mutator(&mut closure, 5), 10);
311/// ```
312///
313/// ## Type Conversion
314///
315/// ```rust
316/// use qubit_function::Mutator;
317///
318/// let closure = |x: &mut i32| *x *= 2;
319///
320/// // Convert to different ownership models
321/// let box_mutator = closure.into_box();
322/// // let rc_mutator = closure.into_rc(); // closure moved
323/// // let arc_mutator = closure.into_arc(); // closure moved
324/// ```
325///
326pub trait StatefulMutator<T> {
327 /// Performs the mutation operation
328 ///
329 /// Executes an operation on the given mutable reference. The operation
330 /// typically modifies the input value or produces side effects.
331 ///
332 /// # Parameters
333 ///
334 /// * `value` - A mutable reference to the value to be mutated
335 ///
336 /// # Examples
337 ///
338 /// ```rust
339 /// use qubit_function::{Mutator, BoxMutator};
340 ///
341 /// let mut mutator = BoxMutator::new(|x: &mut i32| *x *= 2);
342 /// let mut value = 5;
343 /// mutator.apply(&mut value);
344 /// assert_eq!(value, 10);
345 /// ```
346 fn apply(&mut self, value: &mut T);
347
348 /// Convert this mutator into a `BoxMutator<T>`.
349 ///
350 /// This consuming conversion takes ownership of `self` and returns a
351 /// boxed implementation that forwards calls to the original mutator.
352 /// Types that can provide a more efficient conversion may override the
353 /// default implementation.
354 ///
355 /// # Consumption
356 ///
357 /// This method consumes the mutator: the original value will no longer
358 /// be available after the call. For cloneable mutators call `.clone()`
359 /// before converting if you need to retain the original instance.
360 ///
361 /// # Returns
362 ///
363 /// A `BoxMutator<T>` that forwards to the original mutator.
364 ///
365 /// # Examples
366 ///
367 /// ```rust
368 /// use qubit_function::Mutator;
369 ///
370 /// let closure = |x: &mut i32| *x *= 2;
371 /// let mut boxed = closure.into_box();
372 /// let mut value = 5;
373 /// boxed.apply(&mut value);
374 /// assert_eq!(value, 10);
375 /// ```
376 fn into_box(mut self) -> BoxStatefulMutator<T>
377 where
378 Self: Sized + 'static,
379 {
380 BoxStatefulMutator::new(move |t| self.apply(t))
381 }
382
383 /// Convert this mutator into an `RcMutator<T>`.
384 ///
385 /// This consuming conversion takes ownership of `self` and returns an
386 /// `Rc`-backed mutator that forwards calls to the original. Override to
387 /// provide a more direct or efficient conversion when available.
388 ///
389 /// # Consumption
390 ///
391 /// This method consumes the mutator. If you need to keep the original
392 /// instance, clone it prior to calling this method.
393 ///
394 /// # Returns
395 ///
396 /// An `RcMutator<T>` forwarding to the original mutator.
397 ///
398 /// # Examples
399 ///
400 /// ```rust
401 /// use qubit_function::Mutator;
402 ///
403 /// let closure = |x: &mut i32| *x *= 2;
404 /// let mut rc = closure.into_rc();
405 /// let mut value = 5;
406 /// rc.apply(&mut value);
407 /// assert_eq!(value, 10);
408 /// ```
409 fn into_rc(mut self) -> RcStatefulMutator<T>
410 where
411 Self: Sized + 'static,
412 {
413 RcStatefulMutator::new(move |t| self.apply(t))
414 }
415
416 /// Convert this mutator into an `ArcMutator<T>`.
417 ///
418 /// This consuming conversion takes ownership of `self` and returns an
419 /// `Arc`-wrapped, thread-safe mutator. Types may override the default
420 /// implementation to provide a more efficient conversion.
421 ///
422 /// # Consumption
423 ///
424 /// This method consumes the mutator. Clone the instance first if you
425 /// need to retain the original for further use.
426 ///
427 /// # Returns
428 ///
429 /// An `ArcMutator<T>` that forwards to the original mutator.
430 ///
431 /// # Examples
432 ///
433 /// ```rust
434 /// use qubit_function::Mutator;
435 ///
436 /// let closure = |x: &mut i32| *x *= 2;
437 /// let mut arc = closure.into_arc();
438 /// let mut value = 5;
439 /// arc.apply(&mut value);
440 /// assert_eq!(value, 10);
441 /// ```
442 fn into_arc(mut self) -> ArcStatefulMutator<T>
443 where
444 Self: Sized + Send + 'static,
445 {
446 ArcStatefulMutator::new(move |t| self.apply(t))
447 }
448
449 /// Consume the mutator and return an `FnMut(&mut T)` closure.
450 ///
451 /// The returned closure forwards calls to the original mutator and is
452 /// suitable for use with iterator adapters such as `for_each`.
453 ///
454 /// # Consumption
455 ///
456 /// This method consumes the mutator. The original instance will not be
457 /// available after calling this method.
458 ///
459 /// # Returns
460 ///
461 /// A closure implementing `FnMut(&mut T)` which forwards to the
462 /// original mutator.
463 ///
464 /// # Examples
465 ///
466 /// ```rust
467 /// use qubit_function::{Mutator, BoxMutator};
468 ///
469 /// let mutator = BoxMutator::new(|x: &mut i32| *x *= 2);
470 /// let mut values = vec![1, 2, 3, 4, 5];
471 /// values.iter_mut().for_each(mutator.into_fn());
472 /// assert_eq!(values, vec![2, 4, 6, 8, 10]);
473 /// ```
474 fn into_fn(mut self) -> impl FnMut(&mut T)
475 where
476 Self: Sized + 'static,
477 {
478 move |t| self.apply(t)
479 }
480
481 /// Create a non-consuming `BoxMutator<T>` that forwards to `self`.
482 ///
483 /// The default implementation clones `self` (requires `Clone`) and
484 /// returns a boxed mutator that calls the cloned instance. Override this
485 /// method if a more efficient conversion exists.
486 ///
487 /// # Returns
488 ///
489 /// A `BoxMutator<T>` that forwards to a clone of `self`.
490 fn to_box(&self) -> BoxStatefulMutator<T>
491 where
492 Self: Sized + Clone + 'static,
493 {
494 self.clone().into_box()
495 }
496
497 /// Create a non-consuming `RcMutator<T>` that forwards to `self`.
498 ///
499 /// The default implementation clones `self` (requires `Clone`) and
500 /// returns an `Rc`-backed mutator that forwards calls to the clone.
501 /// Override to provide a more direct or efficient conversion if needed.
502 ///
503 /// # Returns
504 ///
505 /// An `RcMutator<T>` that forwards to a clone of `self`.
506 fn to_rc(&self) -> RcStatefulMutator<T>
507 where
508 Self: Sized + Clone + 'static,
509 {
510 self.clone().into_rc()
511 }
512
513 /// Create a non-consuming `ArcMutator<T>` that forwards to `self`.
514 ///
515 /// The default implementation clones `self` (requires `Clone + Send`) and
516 /// returns an `Arc`-wrapped mutator that forwards calls to the clone.
517 /// Override when a more efficient conversion is available.
518 ///
519 /// # Returns
520 ///
521 /// An `ArcMutator<T>` that forwards to a clone of `self`.
522 fn to_arc(&self) -> ArcStatefulMutator<T>
523 where
524 Self: Sized + Clone + Send + 'static,
525 {
526 self.clone().into_arc()
527 }
528
529 /// Create a boxed `FnMut(&mut T)` closure that forwards to `self`.
530 ///
531 /// The default implementation clones `self` (requires `Clone`) and
532 /// returns a boxed closure that invokes the cloned instance. Override to
533 /// provide a more efficient conversion when possible.
534 ///
535 /// # Returns
536 ///
537 /// A closure implementing `FnMut(&mut T)` which forwards to the
538 /// original mutator.
539 fn to_fn(&self) -> impl FnMut(&mut T)
540 where
541 Self: Sized + Clone + 'static,
542 {
543 self.clone().into_fn()
544 }
545
546 /// Convert this mutator into a `BoxMutatorOnce<T>` (consuming).
547 ///
548 /// This consuming conversion takes ownership of `self` and returns a
549 /// boxed one-time mutator that forwards calls to the original mutator.
550 /// The returned mutator can only be used once.
551 ///
552 /// # Consumption
553 ///
554 /// This method consumes the mutator: the original value will no longer
555 /// be available after the call. For cloneable mutators call `.clone()`
556 /// before converting if you need to retain the original instance.
557 ///
558 /// # Returns
559 ///
560 /// A `BoxMutatorOnce<T>` that forwards to the original mutator.
561 ///
562 /// # Examples
563 ///
564 /// ```rust
565 /// use qubit_function::{StatefulMutator, MutatorOnce, BoxStatefulMutator,
566 /// BoxMutatorOnce};
567 ///
568 /// let mutator = BoxStatefulMutator::new(|x: &mut i32| *x *= 2);
569 /// let once_mutator = mutator.into_once();
570 /// let mut value = 5;
571 /// once_mutator.apply(&mut value);
572 /// assert_eq!(value, 10);
573 /// ```
574 fn into_once(mut self) -> BoxMutatorOnce<T>
575 where
576 Self: Sized + 'static,
577 {
578 BoxMutatorOnce::new(move |t| self.apply(t))
579 }
580
581 /// Create a non-consuming `BoxMutatorOnce<T>` that forwards to `self`.
582 ///
583 /// The default implementation clones `self` (requires `Clone`) and
584 /// returns a boxed one-time mutator that calls the cloned instance.
585 /// Override this method if a more efficient conversion exists.
586 ///
587 /// # Returns
588 ///
589 /// A `BoxMutatorOnce<T>` that forwards to a clone of `self`.
590 fn to_once(&self) -> BoxMutatorOnce<T>
591 where
592 Self: Sized + Clone + 'static,
593 {
594 self.clone().into_once()
595 }
596}