Skip to main content

qubit_function/functions/stateful_function/
rc_stateful_function.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026.
4 *    Haixing Hu, Qubit Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9//! Defines the `RcStatefulFunction` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// ============================================================================
16// RcStatefulFunction - Rc<RefCell<dyn FnMut(&T) -> R>>
17// ============================================================================
18
19/// RcStatefulFunction - single-threaded function wrapper
20///
21/// A single-threaded, clonable function wrapper optimized for scenarios
22/// that require sharing without thread-safety overhead.
23///
24/// # Features
25///
26/// - **Based on**: `Rc<RefCell<dyn FnMut(&T) -> R>>`
27/// - **Ownership**: Shared ownership via reference counting (non-atomic)
28/// - **Reusability**: Can be called multiple times (each call consumes
29///   its input)
30/// - **Thread Safety**: Not thread-safe (no `Send + Sync`)
31/// - **Clonable**: Cheap cloning via `Rc::clone`
32/// - **Statefulness**: Can modify internal state between calls
33///
34/// # Author
35///
36/// Haixing Hu
37pub struct RcStatefulFunction<T, R> {
38    pub(super) function: RcStatefulFn<T, R>,
39    pub(super) name: Option<String>,
40}
41
42type RcStatefulFn<T, R> = Rc<RefCell<dyn FnMut(&T) -> R>>;
43
44impl<T, R> RcStatefulFunction<T, R> {
45    // Generates: new(), new_with_name(), new_with_optional_name(), name(), set_name()
46    impl_function_common_methods!(
47        RcStatefulFunction<T, R>,
48        (FnMut(&T) -> R + 'static),
49        |f| Rc::new(RefCell::new(f))
50    );
51
52    // Generates: when(), and_then(), compose()
53    impl_shared_function_methods!(
54        RcStatefulFunction<T, R>,
55        RcConditionalStatefulFunction,
56        into_rc,
57        StatefulFunction,
58        'static
59    );
60}
61
62// Generates: constant() method for RcStatefulFunction<T, R>
63impl_function_constant_method!(RcStatefulFunction<T, R>, 'static);
64
65// Generates: identity() method for RcStatefulFunction<T, T>
66impl_function_identity_method!(RcStatefulFunction<T, T>);
67
68// Generates: Clone implementation for RcStatefulFunction<T, R>
69impl_function_clone!(RcStatefulFunction<T, R>);
70
71// Generates: Debug and Display implementations for RcStatefulFunction<T, R>
72impl_function_debug_display!(RcStatefulFunction<T, R>);
73
74// Implement StatefulFunction trait for RcStatefulFunction<T, R>
75impl<T, R> StatefulFunction<T, R> for RcStatefulFunction<T, R> {
76    fn apply(&mut self, t: &T) -> R {
77        (self.function.borrow_mut())(t)
78    }
79
80    // Use macro to implement conversion methods
81    impl_rc_conversions!(
82        RcStatefulFunction<T, R>,
83        BoxStatefulFunction,
84        BoxFunctionOnce,
85        FnMut(t: &T) -> R
86    );
87}