Skip to main content

qubit_function/functions/mutating_function/
box_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 `BoxMutatingFunction` public type.
12
13#![allow(unused_imports)]
14
15use super::*;
16
17// =======================================================================
18// 3. BoxMutatingFunction - Single Ownership Implementation
19// =======================================================================
20
21/// BoxMutatingFunction struct
22///
23/// A mutating function implementation based on `Box<dyn Fn(&mut T) -> R>`
24/// for single ownership scenarios. This is the simplest and most efficient
25/// mutating function type when sharing is not required.
26///
27/// # Features
28///
29/// - **Single Ownership**: Not cloneable, ownership moves on use
30/// - **Zero Overhead**: No reference counting or locking
31/// - **Stateless**: Cannot modify captured environment (uses `Fn` not
32///   `FnMut`)
33/// - **Builder Pattern**: Method chaining consumes `self` naturally
34/// - **Factory Methods**: Convenient constructors for common patterns
35///
36/// # Use Cases
37///
38/// Choose `BoxMutatingFunction` when:
39/// - The function is used for stateless operations
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/// `BoxMutatingFunction` has the best performance among the three function
47/// 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::{MutatingFunction, BoxMutatingFunction};
57///
58/// let func = BoxMutatingFunction::new(|x: &mut i32| {
59///     *x *= 2;
60///     *x
61/// });
62/// let mut value = 5;
63/// assert_eq!(func.apply(&mut value), 10);
64/// assert_eq!(value, 10);
65/// ```
66///
67pub struct BoxMutatingFunction<T, R> {
68    pub(super) function: Box<dyn Fn(&mut T) -> R>,
69    pub(super) name: Option<String>,
70}
71
72impl<T, R> BoxMutatingFunction<T, R> {
73    // Generates: new(), new_with_name(), new_with_optional_name(), name(), set_name()
74    impl_function_common_methods!(
75        BoxMutatingFunction<T, R>,
76        (Fn(&mut T) -> R + 'static),
77        |f| Box::new(f)
78    );
79
80    // Generates: when(), and_then(), compose()
81    impl_box_function_methods!(
82        BoxMutatingFunction<T, R>,
83        BoxConditionalMutatingFunction,
84        Function  // chains a non-mutating function after this mutating function
85    );
86}
87
88// Generates: Debug and Display implementations for BoxMutatingFunction<T, R>
89impl_function_debug_display!(BoxMutatingFunction<T, R>);
90
91// Generates: identity() method for BoxMutatingFunction<T, T>
92impl_function_identity_method!(BoxMutatingFunction<T, T>, mutating);
93
94// Implement MutatingFunction trait for BoxMutatingFunction<T, R>
95impl<T, R> MutatingFunction<T, R> for BoxMutatingFunction<T, R> {
96    fn apply(&self, t: &mut T) -> R {
97        (self.function)(t)
98    }
99
100    // Generates: into_box(), into_rc(), into_fn(), into_once()
101    impl_box_conversions!(
102        BoxMutatingFunction<T, R>,
103        RcMutatingFunction,
104        Fn(&mut T) -> R,
105        BoxMutatingFunctionOnce
106    );
107}