qubit_function/transformers/transformer/rc_transformer.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2025 - 2026.
4 * Haixing Hu, Qubit Co. Ltd.
5 *
6 * All rights reserved.
7 *
8 ******************************************************************************/
9//! Defines the `RcTransformer` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// ============================================================================
16// RcTransformer - Rc<dyn Fn(T) -> R>
17// ============================================================================
18
19/// RcTransformer - single-threaded transformer wrapper
20///
21/// A single-threaded, clonable transformer wrapper optimized for scenarios
22/// that require sharing without thread-safety overhead.
23///
24/// # Features
25///
26/// - **Based on**: `Rc<dyn Fn(T) -> R>`
27/// - **Ownership**: Shared ownership via reference counting (non-atomic)
28/// - **Reusability**: Can be called multiple times (each call consumes its
29/// input)
30/// - **Thread Safety**: Not thread-safe (no `Send + Sync`)
31/// - **Clonable**: Cheap cloning via `Rc::clone`
32///
33/// # Author
34///
35/// Haixing Hu
36pub struct RcTransformer<T, R> {
37 pub(super) function: Rc<dyn Fn(T) -> R>,
38 pub(super) name: Option<String>,
39}
40
41// Implement RcTransformer
42impl<T, R> RcTransformer<T, R> {
43 impl_transformer_common_methods!(
44 RcTransformer<T, R>,
45 (Fn(T) -> R + 'static),
46 |f| Rc::new(f)
47 );
48
49 impl_shared_transformer_methods!(
50 RcTransformer<T, R>,
51 RcConditionalTransformer,
52 into_rc,
53 Transformer,
54 'static
55 );
56}
57
58impl_transformer_constant_method!(RcTransformer<T, R>);
59
60// Implement Debug and Display for RcTransformer
61impl_transformer_debug_display!(RcTransformer<T, R>);
62
63// Implement Clone for RcTransformer
64impl_transformer_clone!(RcTransformer<T, R>);
65
66// Implement Transformer for RcTransformer
67impl<T, R> Transformer<T, R> for RcTransformer<T, R> {
68 fn apply(&self, input: T) -> R {
69 (self.function)(input)
70 }
71
72 // Generate all conversion methods using the unified macro
73 impl_rc_conversions!(
74 RcTransformer<T, R>,
75 BoxTransformer,
76 BoxTransformerOnce,
77 Fn(input: T) -> R
78 );
79}