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