Skip to main content

qubit_function/transformers/bi_transformer/
arc_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 `ArcBiTransformer` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// ============================================================================
16// ArcBiTransformer - Arc<dyn Fn(T, U) -> R + Send + Sync>
17// ============================================================================
18
19/// ArcBiTransformer - thread-safe bi-transformer wrapper
20///
21/// A thread-safe, clonable bi-transformer wrapper suitable for multi-threaded
22/// scenarios. Can be called multiple times and shared across threads.
23///
24/// # Features
25///
26/// - **Based on**: `Arc<dyn Fn(T, U) -> R + Send + Sync>`
27/// - **Ownership**: Shared ownership via reference counting
28/// - **Reusability**: Can be called multiple times (each call consumes its
29///   inputs)
30/// - **Thread Safety**: Thread-safe (`Send + Sync` required)
31/// - **Clonable**: Cheap cloning via `Arc::clone`
32///
33/// # Author
34///
35/// Haixing Hu
36pub struct ArcBiTransformer<T, U, R> {
37    pub(super) function: Arc<dyn Fn(T, U) -> R + Send + Sync>,
38    pub(super) name: Option<String>,
39}
40
41impl<T, U, R> ArcBiTransformer<T, U, R> {
42    impl_transformer_common_methods!(
43        ArcBiTransformer<T, U, R>,
44        (Fn(T, U) -> R + Send + Sync + 'static),
45        |f| Arc::new(f)
46    );
47
48    impl_shared_transformer_methods!(
49        ArcBiTransformer<T, U, R>,
50        ArcConditionalBiTransformer,
51        into_arc,
52        Transformer,
53        Send + Sync + 'static
54    );
55}
56
57// Implement constant method for ArcBiTransformer
58impl_transformer_constant_method!(thread_safe ArcBiTransformer<T, U, R>);
59
60// Implement Debug and Display for ArcBiTransformer
61impl_transformer_debug_display!(ArcBiTransformer<T, U, R>);
62
63// Implement Clone for ArcBiTransformer
64impl_transformer_clone!(ArcBiTransformer<T, U, R>);
65
66// Implement BiTransformer trait for ArcBiTransformer
67impl<T, U, R> BiTransformer<T, U, R> for ArcBiTransformer<T, U, R> {
68    fn apply(&self, first: T, second: U) -> R {
69        (self.function)(first, second)
70    }
71
72    // Use macro to implement conversion methods
73    impl_arc_conversions!(
74        ArcBiTransformer<T, U, R>,
75        BoxBiTransformer,
76        RcBiTransformer,
77        BoxBiTransformerOnce,
78        Fn(t: T, u: U) -> R
79    );
80}
81
82// ============================================================================
83// Blanket implementation for standard Fn trait
84// ============================================================================
85
86/// Implement BiTransformer<T, U, R> for any type that implements Fn(T, U) -> R
87///
88/// This allows closures and function pointers to be used directly with our
89/// BiTransformer trait without wrapping.
90///
91/// # Examples
92///
93/// ```rust
94/// use qubit_function::BiTransformer;
95///
96/// fn add(x: i32, y: i32) -> i32 { x + y }
97///
98/// assert_eq!(add.apply(20, 22), 42);
99///
100/// let multiply = |x: i32, y: i32| x * y;
101/// assert_eq!(multiply.apply(6, 7), 42);
102/// ```
103///
104/// # Author
105///
106/// Haixing Hu
107impl<F, T, U, R> BiTransformer<T, U, R> for F
108where
109    F: Fn(T, U) -> R,
110{
111    fn apply(&self, first: T, second: U) -> R {
112        self(first, second)
113    }
114
115    fn into_box(self) -> BoxBiTransformer<T, U, R>
116    where
117        Self: Sized + 'static,
118    {
119        BoxBiTransformer::new(self)
120    }
121
122    fn into_rc(self) -> RcBiTransformer<T, U, R>
123    where
124        Self: Sized + 'static,
125    {
126        RcBiTransformer::new(self)
127    }
128
129    fn into_arc(self) -> ArcBiTransformer<T, U, R>
130    where
131        Self: Sized + Send + Sync + 'static,
132    {
133        ArcBiTransformer::new(self)
134    }
135
136    fn into_fn(self) -> impl Fn(T, U) -> R
137    where
138        Self: Sized + 'static,
139    {
140        move |t: T, u: U| self(t, u)
141    }
142
143    // use the default implementation of to_box(), to_rc(), to_arc() from
144    // BiTransformer trait
145
146    fn to_fn(&self) -> impl Fn(T, U) -> R
147    where
148        Self: Sized + Clone + 'static,
149    {
150        self.clone()
151    }
152}