qubit_function/functions/stateful_mutating_function/rc_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 `RcStatefulMutatingFunction` public type.
12
13#![allow(unused_imports)]
14
15use super::*;
16
17// =======================================================================
18// 4. RcStatefulMutatingFunction - Single-Threaded Shared Ownership
19// =======================================================================
20
21/// RcStatefulMutatingFunction struct
22///
23/// A stateful mutating function implementation based on
24/// `Rc<RefCell<dyn FnMut(&mut T) -> R>>` for single-threaded shared
25/// ownership scenarios. This type allows multiple references to the same
26/// function without the overhead of thread safety.
27///
28/// # Features
29///
30/// - **Shared Ownership**: Cloneable via `Rc`, multiple owners allowed
31/// - **Single-Threaded**: Not thread-safe, cannot be sent across threads
32/// - **Stateful**: Can modify captured environment (uses `FnMut`)
33/// - **Chainable**: Method chaining via `&self` (non-consuming)
34/// - **Performance**: More efficient than `ArcStatefulMutatingFunction` (no
35/// locking)
36///
37/// # Use Cases
38///
39/// Choose `RcStatefulMutatingFunction` when:
40/// - The function needs to be shared within a single thread for stateful
41/// operations
42/// - Thread safety is not required
43/// - Performance is important (avoiding lock overhead)
44///
45/// # Examples
46///
47/// ```rust
48/// use qubit_function::{StatefulMutatingFunction,
49/// RcStatefulMutatingFunction};
50///
51/// let counter = {
52/// let mut count = 0;
53/// RcStatefulMutatingFunction::new(move |x: &mut i32| {
54/// count += 1;
55/// *x *= 2;
56/// count
57/// })
58/// };
59/// let mut clone = counter.clone();
60///
61/// let mut value = 5;
62/// assert_eq!(clone.apply(&mut value), 1);
63/// ```
64///
65pub struct RcStatefulMutatingFunction<T, R> {
66 pub(super) function: RcStatefulMutatingFunctionFn<T, R>,
67 pub(super) name: Option<String>,
68}
69
70impl<T, R> RcStatefulMutatingFunction<T, R> {
71 // Generates: new(), new_with_name(), new_with_optional_name(), name(), set_name()
72 impl_function_common_methods!(
73 RcStatefulMutatingFunction<T, R>,
74 (FnMut(&mut T) -> R + 'static),
75 |f| Rc::new(RefCell::new(f))
76 );
77
78 // Generates: when(), and_then(), compose()
79 impl_shared_function_methods!(
80 RcStatefulMutatingFunction<T, R>,
81 RcConditionalStatefulMutatingFunction,
82 into_rc,
83 Function, // chains a non-mutating function after this mutating function
84 'static
85 );
86}
87
88// Generates: Clone implementation for RcStatefulMutatingFunction<T, R>
89impl_function_clone!(RcStatefulMutatingFunction<T, R>);
90
91// Generates: Debug and Display implementations for RcStatefulMutatingFunction<T, R>
92impl_function_debug_display!(RcStatefulMutatingFunction<T, R>);
93
94// Generates: identity() method for RcStatefulMutatingFunction<T, T>
95impl_function_identity_method!(RcStatefulMutatingFunction<T, T>, mutating);
96
97// Implement StatefulMutatingFunction trait for RcStatefulMutatingFunction<T, R>
98impl<T, R> StatefulMutatingFunction<T, R> for RcStatefulMutatingFunction<T, R> {
99 fn apply(&mut self, t: &mut T) -> R {
100 (self.function.borrow_mut())(t)
101 }
102
103 // Use macro to implement conversion methods
104 impl_rc_conversions!(
105 RcStatefulMutatingFunction<T, R>,
106 BoxStatefulMutatingFunction,
107 BoxMutatingFunctionOnce,
108 FnMut(input: &mut T) -> R
109 );
110}