Skip to main content

qubit_function/functions/stateful_function/
box_stateful_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 `BoxStatefulFunction` public type.
12
13use super::{
14    BoxConditionalStatefulFunction,
15    BoxFunctionOnce,
16    Predicate,
17    RcStatefulFunction,
18    StatefulFunction,
19    impl_box_conversions,
20    impl_box_function_methods,
21    impl_function_common_methods,
22    impl_function_constant_method,
23    impl_function_debug_display,
24    impl_function_identity_method,
25};
26
27// ============================================================================
28// BoxStatefulFunction - Box<dyn FnMut(&T) -> R>
29// ============================================================================
30
31/// BoxStatefulFunction - stateful function wrapper based on `Box<dyn FnMut>`
32///
33/// A stateful function wrapper that provides single ownership with reusable stateful
34/// transformation. The stateful function consumes the input and can be called
35/// multiple times while maintaining internal state.
36///
37/// # Features
38///
39/// - **Based on**: `Box<dyn FnMut(&T) -> R>`
40/// - **Ownership**: Single ownership, cannot be cloned
41/// - **Reusability**: Can be called multiple times (each call consumes
42///   its input)
43/// - **Thread Safety**: Not thread-safe (no `Send + Sync` requirement)
44/// - **Statefulness**: Can modify internal state between calls
45///
46pub struct BoxStatefulFunction<T, R> {
47    pub(super) function: Box<dyn FnMut(&T) -> R>,
48    pub(super) name: Option<String>,
49}
50
51impl<T, R> BoxStatefulFunction<T, R> {
52    // Generates: new(), new_with_name(), new_with_optional_name(), name(), set_name()
53    impl_function_common_methods!(
54        BoxStatefulFunction<T, R>,
55        (FnMut(&T) -> R + 'static),
56        |f| Box::new(f)
57    );
58
59    // Generates: when(), and_then(), compose()
60    impl_box_function_methods!(
61        BoxStatefulFunction<T, R>,
62        BoxConditionalStatefulFunction,
63        StatefulFunction
64    );
65}
66
67// Generates: constant() method for BoxStatefulFunction<T, R>
68impl_function_constant_method!(BoxStatefulFunction<T, R>, 'static);
69
70// Generates: identity() method for BoxStatefulFunction<T, T>
71impl_function_identity_method!(BoxStatefulFunction<T, T>);
72
73// Generates: Debug and Display implementations for BoxStatefulFunction<T, R>
74impl_function_debug_display!(BoxStatefulFunction<T, R>);
75
76// Implement StatefulFunction trait for BoxStatefulFunction<T, R>
77impl<T, R> StatefulFunction<T, R> for BoxStatefulFunction<T, R> {
78    fn apply(&mut self, t: &T) -> R {
79        (self.function)(t)
80    }
81
82    // Generates: into_box(), into_rc(), into_fn(), into_once()
83    impl_box_conversions!(
84        BoxStatefulFunction<T, R>,
85        RcStatefulFunction,
86        FnMut(&T) -> R,
87        BoxFunctionOnce
88    );
89}