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