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