qubit_function/transformers/bi_transformer/rc_bi_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 `RcBiTransformer` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// ============================================================================
16// RcBiTransformer - Rc<dyn Fn(T, U) -> R>
17// ============================================================================
18
19/// RcBiTransformer - single-threaded bi-transformer wrapper
20///
21/// A single-threaded, clonable bi-transformer wrapper optimized for scenarios
22/// that require sharing without thread-safety overhead.
23///
24/// # Features
25///
26/// - **Based on**: `Rc<dyn Fn(T, U) -> R>`
27/// - **Ownership**: Shared ownership via reference counting (non-atomic)
28/// - **Reusability**: Can be called multiple times (each call consumes its
29/// inputs)
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 RcBiTransformer<T, U, R> {
37 pub(super) function: Rc<dyn Fn(T, U) -> R>,
38 pub(super) name: Option<String>,
39}
40
41impl<T, U, R> RcBiTransformer<T, U, R> {
42 impl_transformer_common_methods!(
43 RcBiTransformer<T, U, R>,
44 (Fn(T, U) -> R + 'static),
45 |f| Rc::new(f)
46 );
47
48 impl_shared_transformer_methods!(
49 RcBiTransformer<T, U, R>,
50 RcConditionalBiTransformer,
51 into_rc,
52 Transformer,
53 'static
54 );
55}
56
57// Implement constant method for RcBiTransformer
58impl_transformer_constant_method!(RcBiTransformer<T, U, R>);
59
60// Implement Debug and Display for RcBiTransformer
61impl_transformer_debug_display!(RcBiTransformer<T, U, R>);
62
63// Implement Clone for RcBiTransformer
64impl_transformer_clone!(RcBiTransformer<T, U, R>);
65
66// Implement BiTransformer trait for RcBiTransformer
67impl<T, U, R> BiTransformer<T, U, R> for RcBiTransformer<T, U, R> {
68 fn apply(&self, first: T, second: U) -> R {
69 (self.function)(first, second)
70 }
71
72 // Generate all conversion methods using the unified macro
73 impl_rc_conversions!(
74 RcBiTransformer<T, U, R>,
75 BoxBiTransformer,
76 BoxBiTransformerOnce,
77 Fn(first: T, second: U) -> R
78 );
79
80 // do NOT override RcBiTransformer::into_arc() because RcBiTransformer is not Send + Sync
81 // and calling RcBiTransformer::into_arc() will cause a compile error
82
83 // do NOT override RcBiTransformer::to_arc() because RcBiTransformer is not Send + Sync
84 // and calling RcBiTransformer::to_arc() will cause a compile error
85}