Skip to main content

qubit_function/functions/stateful_mutating_function/
box_stateful_mutating_function.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 `BoxStatefulMutatingFunction` public type.
12
13#![allow(unused_imports)]
14
15use super::*;
16
17// =======================================================================
18// 3. BoxStatefulMutatingFunction - Single Ownership Implementation
19// =======================================================================
20
21/// BoxStatefulMutatingFunction struct
22///
23/// A stateful mutating function implementation based on
24/// `Box<dyn FnMut(&mut T) -> R>` for single ownership scenarios. This is the
25/// simplest and most efficient stateful mutating function type when sharing
26/// is not required.
27///
28/// # Features
29///
30/// - **Single Ownership**: Not cloneable, ownership moves on use
31/// - **Zero Overhead**: No reference counting or locking
32/// - **Stateful**: Can modify captured environment (uses `FnMut`)
33/// - **Builder Pattern**: Method chaining consumes `self` naturally
34/// - **Factory Methods**: Convenient constructors for common patterns
35///
36/// # Use Cases
37///
38/// Choose `BoxStatefulMutatingFunction` when:
39/// - The function needs to maintain internal state
40/// - Building pipelines where ownership naturally flows
41/// - No need to share the function across contexts
42/// - Performance is critical and no sharing overhead is acceptable
43///
44/// # Performance
45///
46/// `BoxStatefulMutatingFunction` has the best performance among the three
47/// function types:
48/// - No reference counting overhead
49/// - No lock acquisition or runtime borrow checking
50/// - Direct function call through vtable
51/// - Minimal memory footprint (single pointer)
52///
53/// # Examples
54///
55/// ```rust
56/// use qubit_function::{StatefulMutatingFunction,
57///                       BoxStatefulMutatingFunction};
58///
59/// let mut counter = {
60///     let mut count = 0;
61///     BoxStatefulMutatingFunction::new(move |x: &mut i32| {
62///         count += 1;
63///         *x *= 2;
64///         count
65///     })
66/// };
67/// let mut value = 5;
68/// assert_eq!(counter.apply(&mut value), 1);
69/// assert_eq!(value, 10);
70/// ```
71///
72pub struct BoxStatefulMutatingFunction<T, R> {
73    pub(super) function: Box<dyn FnMut(&mut T) -> R>,
74    pub(super) name: Option<String>,
75}
76
77impl<T, R> BoxStatefulMutatingFunction<T, R> {
78    // Generates: new(), new_with_name(), new_with_optional_name(), name(), set_name()
79    impl_function_common_methods!(
80        BoxStatefulMutatingFunction<T, R>,
81        (FnMut(&mut T) -> R + 'static),
82        |f| Box::new(f)
83    );
84
85    // Generates: when(), and_then(), compose()
86    impl_box_function_methods!(
87        BoxStatefulMutatingFunction<T, R>,
88        BoxConditionalStatefulMutatingFunction,
89        Function        // chains a non-mutating function after this mutating function
90    );
91}
92
93// Generates: Debug and Display implementations for BoxStatefulMutatingFunction<T, R>
94impl_function_debug_display!(BoxStatefulMutatingFunction<T, R>);
95
96// Generates: identity() method for BoxStatefulMutatingFunction<T, T>
97impl_function_identity_method!(BoxStatefulMutatingFunction<T, T>, mutating);
98
99// Implement StatefulMutatingFunction trait for BoxStatefulMutatingFunction<T, R>
100impl<T, R> StatefulMutatingFunction<T, R> for BoxStatefulMutatingFunction<T, R> {
101    fn apply(&mut self, t: &mut T) -> R {
102        (self.function)(t)
103    }
104
105    // Generates: into_box(), into_rc(), into_fn(), into_once()
106    impl_box_conversions!(
107        BoxStatefulMutatingFunction<T, R>,
108        RcStatefulMutatingFunction,
109        FnMut(&mut T) -> R,
110        BoxMutatingFunctionOnce
111    );
112}