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