qubit_function/transformers/transformer.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//! # Transformer Types
11//!
12//! Provides Rust implementations of transformer traits for type conversion
13//! and value transformation. Transformers consume input values (taking
14//! ownership) and produce output values. This is analogous to
15//! `Fn(T) -> R` in Rust's standard library.
16//!
17//! This module provides the `Transformer<T, R>` trait and three
18//! implementations:
19//!
20//! - [`BoxTransformer`]: Single ownership, not cloneable
21//! - [`ArcTransformer`]: Thread-safe shared ownership, cloneable
22//! - [`RcTransformer`]: Single-threaded shared ownership, cloneable
23//!
24use std::rc::Rc;
25use std::sync::Arc;
26
27use crate::macros::{
28 impl_arc_conversions,
29 impl_box_conversions,
30 impl_closure_trait,
31 impl_rc_conversions,
32};
33use crate::predicates::predicate::{
34 ArcPredicate,
35 BoxPredicate,
36 Predicate,
37 RcPredicate,
38};
39use crate::transformers::{
40 macros::{
41 impl_box_conditional_transformer,
42 impl_box_transformer_methods,
43 impl_conditional_transformer_clone,
44 impl_conditional_transformer_debug_display,
45 impl_shared_conditional_transformer,
46 impl_shared_transformer_methods,
47 impl_transformer_clone,
48 impl_transformer_common_methods,
49 impl_transformer_constant_method,
50 impl_transformer_debug_display,
51 },
52 transformer_once::BoxTransformerOnce,
53};
54
55mod box_transformer;
56pub use box_transformer::BoxTransformer;
57mod rc_transformer;
58pub use rc_transformer::RcTransformer;
59mod arc_transformer;
60pub use arc_transformer::ArcTransformer;
61mod fn_transformer_ops;
62pub use fn_transformer_ops::FnTransformerOps;
63mod unary_operator;
64pub use unary_operator::UnaryOperator;
65mod box_unary_operator;
66pub use box_unary_operator::BoxUnaryOperator;
67mod arc_unary_operator;
68pub use arc_unary_operator::ArcUnaryOperator;
69mod rc_unary_operator;
70pub use rc_unary_operator::RcUnaryOperator;
71mod box_conditional_transformer;
72pub use box_conditional_transformer::BoxConditionalTransformer;
73mod rc_conditional_transformer;
74pub use rc_conditional_transformer::RcConditionalTransformer;
75mod arc_conditional_transformer;
76pub use arc_conditional_transformer::ArcConditionalTransformer;
77
78// ============================================================================
79// Core Trait
80// ============================================================================
81
82/// Transformer trait - transforms values from type T to type R
83///
84/// Defines the behavior of a transformation: converting a value of type `T`
85/// to a value of type `R` by consuming the input. This is analogous to
86/// `Fn(T) -> R` in Rust's standard library.
87///
88/// # Type Parameters
89///
90/// * `T` - The type of the input value (consumed)
91/// * `R` - The type of the output value
92///
93pub trait Transformer<T, R> {
94 /// Applies the transformation to the input value to produce an output value
95 ///
96 /// # Parameters
97 ///
98 /// * `input` - The input value to transform (consumed)
99 ///
100 /// # Returns
101 ///
102 /// The transformed output value
103 fn apply(&self, input: T) -> R;
104
105 /// Converts to BoxTransformer
106 ///
107 /// **⚠️ Consumes `self`**: The original transformer becomes
108 /// unavailable after calling this method.
109 ///
110 /// # Default Implementation
111 ///
112 /// The default implementation wraps `self` in a `Box` and creates a
113 /// `BoxTransformer`. Types can override this method to provide more
114 /// efficient conversions.
115 ///
116 /// # Returns
117 ///
118 /// Returns `BoxTransformer<T, R>`
119 fn into_box(self) -> BoxTransformer<T, R>
120 where
121 Self: Sized + 'static,
122 {
123 BoxTransformer::new(move |x| self.apply(x))
124 }
125
126 /// Converts to RcTransformer
127 ///
128 /// **⚠️ Consumes `self`**: The original transformer becomes
129 /// unavailable after calling this method.
130 ///
131 /// # Default Implementation
132 ///
133 /// The default implementation wraps `self` in an `Rc` and creates an
134 /// `RcTransformer`. Types can override this method to provide more
135 /// efficient conversions.
136 ///
137 /// # Returns
138 ///
139 /// Returns `RcTransformer<T, R>`
140 fn into_rc(self) -> RcTransformer<T, R>
141 where
142 Self: Sized + 'static,
143 {
144 RcTransformer::new(move |x| self.apply(x))
145 }
146
147 /// Converts to ArcTransformer
148 ///
149 /// **⚠️ Consumes `self`**: The original transformer becomes
150 /// unavailable after calling this method.
151 ///
152 /// # Default Implementation
153 ///
154 /// The default implementation wraps `self` in an `Arc` and creates
155 /// an `ArcTransformer`. Types can override this method to provide
156 /// more efficient conversions.
157 ///
158 /// # Returns
159 ///
160 /// Returns `ArcTransformer<T, R>`
161 fn into_arc(self) -> ArcTransformer<T, R>
162 where
163 Self: Sized + Send + Sync + 'static,
164 {
165 ArcTransformer::new(move |x| self.apply(x))
166 }
167
168 /// Converts transformer to a closure
169 ///
170 /// **⚠️ Consumes `self`**: The original transformer becomes
171 /// unavailable after calling this method.
172 ///
173 /// # Default Implementation
174 ///
175 /// The default implementation creates a closure that captures `self`
176 /// and calls its `transform` method. Types can override this method
177 /// to provide more efficient conversions.
178 ///
179 /// # Returns
180 ///
181 /// Returns a closure that implements `Fn(T) -> R`
182 fn into_fn(self) -> impl Fn(T) -> R
183 where
184 Self: Sized + 'static,
185 {
186 move |t: T| self.apply(t)
187 }
188
189 /// Converts to `BoxTransformerOnce`.
190 ///
191 /// This method has a default implementation that wraps the
192 /// transformer in a `BoxTransformerOnce`. Custom implementations
193 /// can override this method for optimization purposes.
194 ///
195 /// # Returns
196 ///
197 /// A new `BoxTransformerOnce<T, R>` instance
198 ///
199 /// # Examples
200 ///
201 /// ```rust
202 /// use qubit_function::{Transformer, TransformerOnce};
203 ///
204 /// let closure = |x: i32| x * 2;
205 /// let once = closure.into_once();
206 /// assert_eq!(once.apply(5), 10);
207 /// ```
208 fn into_once(self) -> BoxTransformerOnce<T, R>
209 where
210 Self: Sized + 'static,
211 {
212 BoxTransformerOnce::new(move |t| self.apply(t))
213 }
214
215 /// Converts to BoxTransformer without consuming self
216 ///
217 /// **📌 Borrows `&self`**: The original transformer remains usable
218 /// after calling this method.
219 ///
220 /// # Default Implementation
221 ///
222 /// The default implementation creates a new `BoxTransformer` that
223 /// captures a reference-counted clone. Types implementing `Clone`
224 /// can override this method to provide more efficient conversions.
225 ///
226 /// # Returns
227 ///
228 /// Returns `BoxTransformer<T, R>`
229 ///
230 /// # Examples
231 ///
232 /// ```rust
233 /// use qubit_function::{ArcTransformer, Transformer};
234 ///
235 /// let double = ArcTransformer::new(|x: i32| x * 2);
236 /// let boxed = double.to_box();
237 ///
238 /// // Original transformer still usable
239 /// assert_eq!(double.apply(21), 42);
240 /// assert_eq!(boxed.apply(21), 42);
241 /// ```
242 fn to_box(&self) -> BoxTransformer<T, R>
243 where
244 Self: Clone + 'static,
245 {
246 self.clone().into_box()
247 }
248
249 /// Converts to RcTransformer without consuming self
250 ///
251 /// **📌 Borrows `&self`**: The original transformer remains usable
252 /// after calling this method.
253 ///
254 /// # Default Implementation
255 ///
256 /// The default implementation creates a new `RcTransformer` that
257 /// captures a reference-counted clone. Types implementing `Clone`
258 /// can override this method to provide more efficient conversions.
259 ///
260 /// # Returns
261 ///
262 /// Returns `RcTransformer<T, R>`
263 ///
264 /// # Examples
265 ///
266 /// ```rust
267 /// use qubit_function::{ArcTransformer, Transformer};
268 ///
269 /// let double = ArcTransformer::new(|x: i32| x * 2);
270 /// let rc = double.to_rc();
271 ///
272 /// // Original transformer still usable
273 /// assert_eq!(double.apply(21), 42);
274 /// assert_eq!(rc.apply(21), 42);
275 /// ```
276 fn to_rc(&self) -> RcTransformer<T, R>
277 where
278 Self: Clone + 'static,
279 {
280 self.clone().into_rc()
281 }
282
283 /// Converts to ArcTransformer without consuming self
284 ///
285 /// **📌 Borrows `&self`**: The original transformer remains usable
286 /// after calling this method.
287 ///
288 /// # Default Implementation
289 ///
290 /// The default implementation creates a new `ArcTransformer` that
291 /// captures a reference-counted clone. Types implementing `Clone`
292 /// can override this method to provide more efficient conversions.
293 ///
294 /// # Returns
295 ///
296 /// Returns `ArcTransformer<T, R>`
297 ///
298 /// # Examples
299 ///
300 /// ```rust
301 /// use qubit_function::{ArcTransformer, Transformer};
302 ///
303 /// let double = ArcTransformer::new(|x: i32| x * 2);
304 /// let arc = double.to_arc();
305 ///
306 /// // Original transformer still usable
307 /// assert_eq!(double.apply(21), 42);
308 /// assert_eq!(arc.apply(21), 42);
309 /// ```
310 fn to_arc(&self) -> ArcTransformer<T, R>
311 where
312 Self: Clone + Send + Sync + 'static,
313 {
314 self.clone().into_arc()
315 }
316
317 /// Converts transformer to a closure without consuming self
318 ///
319 /// **📌 Borrows `&self`**: The original transformer remains usable
320 /// after calling this method.
321 ///
322 /// # Default Implementation
323 ///
324 /// The default implementation creates a closure that captures a
325 /// clone of `self` and calls its `transform` method. Types can
326 /// override this method to provide more efficient conversions.
327 ///
328 /// # Returns
329 ///
330 /// Returns a closure that implements `Fn(T) -> R`
331 ///
332 /// # Examples
333 ///
334 /// ```rust
335 /// use qubit_function::{ArcTransformer, Transformer};
336 ///
337 /// let double = ArcTransformer::new(|x: i32| x * 2);
338 /// let closure = double.to_fn();
339 ///
340 /// // Original transformer still usable
341 /// assert_eq!(double.apply(21), 42);
342 /// assert_eq!(closure(21), 42);
343 /// ```
344 fn to_fn(&self) -> impl Fn(T) -> R
345 where
346 Self: Clone + 'static,
347 {
348 self.clone().into_fn()
349 }
350
351 /// Converts to `BoxTransformerOnce` without consuming self
352 ///
353 /// **⚠️ Requires Clone**: This method requires `Self` to implement `Clone`.
354 /// Clones the current transformer and converts the clone to a one-time transformer.
355 ///
356 /// # Returns
357 ///
358 /// Returns a `BoxTransformerOnce<T, R>`
359 fn to_once(&self) -> BoxTransformerOnce<T, R>
360 where
361 Self: Clone + 'static,
362 {
363 self.clone().into_once()
364 }
365}