Skip to main content

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