Skip to main content

qubit_function/transformers/
stateful_bi_transformer.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026.
4 *    Haixing Hu, Qubit Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9//! # StatefulBiTransformer Types
10//!
11//! Provides Rust implementations of stateful bi-transformer traits for type
12//! conversion and value transformation with two inputs. StatefulBiTransformers
13//! consume two input values (taking ownership) and produce an output value.
14//!
15//! This module provides the `StatefulBiTransformer<T, U, R>` trait and three
16//! implementations:
17//!
18//! - [`BoxStatefulBiTransformer`]: Single ownership, not cloneable
19//! - [`ArcStatefulBiTransformer`]: Thread-safe shared ownership, cloneable
20//! - [`RcStatefulBiTransformer`]: Single-threaded shared ownership, cloneable
21//!
22//! # Author
23//!
24//! Haixing Hu
25use std::cell::RefCell;
26use std::rc::Rc;
27use std::sync::Arc;
28
29use parking_lot::Mutex;
30
31use crate::macros::{
32    impl_arc_conversions,
33    impl_closure_trait,
34    impl_rc_conversions,
35};
36use crate::predicates::bi_predicate::{
37    ArcBiPredicate,
38    BiPredicate,
39    BoxBiPredicate,
40    RcBiPredicate,
41};
42use crate::transformers::{
43    bi_transformer_once::BoxBiTransformerOnce,
44    macros::{
45        impl_box_conditional_transformer,
46        impl_box_transformer_methods,
47        impl_conditional_transformer_clone,
48        impl_conditional_transformer_debug_display,
49        impl_shared_conditional_transformer,
50        impl_shared_transformer_methods,
51        impl_transformer_clone,
52        impl_transformer_common_methods,
53        impl_transformer_constant_method,
54        impl_transformer_debug_display,
55    },
56    stateful_transformer::StatefulTransformer,
57};
58
59mod box_stateful_bi_transformer;
60pub use box_stateful_bi_transformer::BoxStatefulBiTransformer;
61mod rc_stateful_bi_transformer;
62pub use rc_stateful_bi_transformer::RcStatefulBiTransformer;
63mod arc_stateful_bi_transformer;
64pub use arc_stateful_bi_transformer::ArcStatefulBiTransformer;
65mod fn_stateful_bi_transformer_ops;
66pub use fn_stateful_bi_transformer_ops::FnStatefulBiTransformerOps;
67mod stateful_binary_operator;
68pub use stateful_binary_operator::StatefulBinaryOperator;
69mod box_stateful_binary_operator;
70pub use box_stateful_binary_operator::BoxStatefulBinaryOperator;
71mod arc_stateful_binary_operator;
72pub use arc_stateful_binary_operator::ArcStatefulBinaryOperator;
73mod rc_stateful_binary_operator;
74pub use rc_stateful_binary_operator::RcStatefulBinaryOperator;
75mod box_conditional_stateful_bi_transformer;
76pub use box_conditional_stateful_bi_transformer::BoxConditionalStatefulBiTransformer;
77mod rc_conditional_stateful_bi_transformer;
78pub use rc_conditional_stateful_bi_transformer::RcConditionalStatefulBiTransformer;
79mod arc_conditional_stateful_bi_transformer;
80pub use arc_conditional_stateful_bi_transformer::ArcConditionalStatefulBiTransformer;
81
82// ============================================================================
83// Core Trait
84// ============================================================================
85
86/// StatefulBiTransformer trait - transforms two values to produce a result
87///
88/// Defines the behavior of a bi-transformation: converting two values of types
89/// `T` and `U` to a value of type `R` by consuming the inputs. This is
90/// analogous to `Fn(T, U) -> R` in Rust's standard library.
91///
92/// # Type Parameters
93///
94/// * `T` - The type of the first input value (consumed)
95/// * `U` - The type of the second input value (consumed)
96/// * `R` - The type of the output value
97///
98/// # Author
99///
100/// Haixing Hu
101pub trait StatefulBiTransformer<T, U, R> {
102    /// Transforms two input values to produce an output value
103    ///
104    /// # Parameters
105    ///
106    /// * `first` - The first input value to transform (consumed)
107    /// * `second` - The second input value to transform (consumed)
108    ///
109    /// # Returns
110    ///
111    /// The transformed output value
112    fn apply(&mut self, first: T, second: U) -> R;
113
114    /// Converts to BoxStatefulBiTransformer
115    ///
116    /// **⚠️ Consumes `self`**: The original bi-transformer becomes unavailable
117    /// after calling this method.
118    ///
119    /// # Default Implementation
120    ///
121    /// The default implementation wraps `self` in a `Box` and creates a
122    /// `BoxStatefulBiTransformer`. Types can override this method to provide more
123    /// efficient conversions.
124    ///
125    /// # Returns
126    ///
127    /// Returns `BoxStatefulBiTransformer<T, U, R>`
128    fn into_box(self) -> BoxStatefulBiTransformer<T, U, R>
129    where
130        Self: Sized + 'static,
131    {
132        let mut trans = self;
133        BoxStatefulBiTransformer::new(move |x, y| trans.apply(x, y))
134    }
135
136    /// Converts to RcStatefulBiTransformer
137    ///
138    /// **⚠️ Consumes `self`**: The original bi-transformer becomes unavailable
139    /// after calling this method.
140    ///
141    /// # Default Implementation
142    ///
143    /// The default implementation wraps `self` in an `Rc` and creates an
144    /// `RcStatefulBiTransformer`. Types can override this method to provide more
145    /// efficient conversions.
146    ///
147    /// # Returns
148    ///
149    /// Returns `RcStatefulBiTransformer<T, U, R>`
150    fn into_rc(self) -> RcStatefulBiTransformer<T, U, R>
151    where
152        Self: Sized + 'static,
153    {
154        let mut trans = self;
155        RcStatefulBiTransformer::new(move |x, y| trans.apply(x, y))
156    }
157
158    /// Converts to ArcStatefulBiTransformer
159    ///
160    /// **⚠️ Consumes `self`**: The original bi-transformer becomes unavailable
161    /// after calling this method.
162    ///
163    /// # Default Implementation
164    ///
165    /// The default implementation wraps `self` in an `Arc` and creates an
166    /// `ArcStatefulBiTransformer`. Types can override this method to provide more
167    /// efficient conversions.
168    ///
169    /// # Returns
170    ///
171    /// Returns `ArcStatefulBiTransformer<T, U, R>`
172    fn into_arc(self) -> ArcStatefulBiTransformer<T, U, R>
173    where
174        Self: Sized + Send + 'static,
175    {
176        let mut trans = self;
177        ArcStatefulBiTransformer::new(move |x, y| trans.apply(x, y))
178    }
179
180    /// Converts bi-transformer to a closure
181    ///
182    /// **⚠️ Consumes `self`**: The original bi-transformer becomes unavailable
183    /// after calling this method.
184    ///
185    /// # Default Implementation
186    ///
187    /// The default implementation creates a closure that captures `self`
188    /// and calls its `apply` method. Types can override this method
189    /// to provide more efficient conversions.
190    ///
191    /// # Returns
192    ///
193    /// Returns a closure that implements `FnMut(T, U) -> R`
194    fn into_fn(self) -> impl FnMut(T, U) -> R
195    where
196        Self: Sized + 'static,
197    {
198        let mut trans = self;
199        move |t, u| trans.apply(t, u)
200    }
201
202    /// Converts bi-transformer to a mutable closure (`FnMut`) with an explicit
203    /// method name.
204    ///
205    /// This is a naming alias of [`StatefulBiTransformer::into_fn`] to avoid
206    /// confusion with non-stateful `into_fn` methods that typically return `Fn`.
207    fn into_mut_fn(self) -> impl FnMut(T, U) -> R
208    where
209        Self: Sized + 'static,
210    {
211        self.into_fn()
212    }
213
214    /// Converts to BoxBiTransformerOnce
215    ///
216    /// **⚠️ Consumes `self`**: The original bi-transformer becomes unavailable
217    /// after calling this method.
218    ///
219    /// # Default Implementation
220    ///
221    /// The default implementation wraps `self` in a `Box` and creates a
222    /// `BoxBiTransformerOnce`. Types can override this method to provide more
223    /// efficient conversions.
224    ///
225    /// # Returns
226    ///
227    /// Returns `BoxBiTransformerOnce<T, U, R>`
228    fn into_once(self) -> BoxBiTransformerOnce<T, U, R>
229    where
230        Self: Sized + 'static,
231    {
232        let mut trans = self;
233        BoxBiTransformerOnce::new(move |t, u| trans.apply(t, u))
234    }
235
236    /// Non-consuming conversion to `BoxStatefulBiTransformer` using `&self`.
237    ///
238    /// Default implementation clones `self` and delegates to `into_box`.
239    fn to_box(&self) -> BoxStatefulBiTransformer<T, U, R>
240    where
241        Self: Sized + Clone + 'static,
242    {
243        self.clone().into_box()
244    }
245
246    /// Non-consuming conversion to `RcStatefulBiTransformer` using `&self`.
247    ///
248    /// Default implementation clones `self` and delegates to `into_rc`.
249    fn to_rc(&self) -> RcStatefulBiTransformer<T, U, R>
250    where
251        Self: Sized + Clone + 'static,
252    {
253        self.clone().into_rc()
254    }
255
256    /// Non-consuming conversion to `ArcStatefulBiTransformer` using `&self`.
257    ///
258    /// Default implementation clones `self` and delegates to `into_arc`.
259    fn to_arc(&self) -> ArcStatefulBiTransformer<T, U, R>
260    where
261        Self: Sized + Clone + Send + 'static,
262    {
263        self.clone().into_arc()
264    }
265
266    /// Non-consuming conversion to a boxed function using `&self`.
267    ///
268    /// Returns a `Box<dyn FnMut(T, U) -> R>` that clones `self` and calls
269    /// `apply` inside the boxed closure.
270    fn to_fn(&self) -> impl FnMut(T, U) -> R
271    where
272        Self: Sized + Clone + 'static,
273    {
274        self.clone().into_fn()
275    }
276
277    /// Non-consuming conversion to a mutable closure (`FnMut`) with an explicit
278    /// method name.
279    ///
280    /// This is a naming alias of [`StatefulBiTransformer::to_fn`] and keeps the
281    /// same clone-based behavior.
282    fn to_mut_fn(&self) -> impl FnMut(T, U) -> R
283    where
284        Self: Sized + Clone + 'static,
285    {
286        self.to_fn()
287    }
288
289    /// Non-consuming conversion to `BoxBiTransformerOnce` using `&self`.
290    ///
291    /// Default implementation clones `self` and delegates to `into_once`.
292    fn to_once(&self) -> BoxBiTransformerOnce<T, U, R>
293    where
294        Self: Sized + Clone + 'static,
295    {
296        self.clone().into_once()
297    }
298}