qubit_function/transformers/transformer/rc_conditional_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 `RcConditionalTransformer` public type.
12
13#![allow(unused_imports)]
14
15use super::*;
16
17// ============================================================================
18// RcConditionalTransformer - Rc-based Conditional Transformer
19// ============================================================================
20
21/// RcConditionalTransformer struct
22///
23/// A single-threaded conditional transformer that only executes when a
24/// predicate is satisfied. Uses `RcTransformer` and `RcPredicate` for shared
25/// ownership within a single thread.
26///
27/// This type is typically created by calling `RcTransformer::when()` and is
28/// designed to work with the `or_else()` method to create if-then-else logic.
29///
30/// # Features
31///
32/// - **Shared Ownership**: Cloneable via `Rc`, multiple owners allowed
33/// - **Single-Threaded**: Not thread-safe, cannot be sent across threads
34/// - **Conditional Execution**: Only transforms when predicate returns `true`
35/// - **No Lock Overhead**: More efficient than `ArcConditionalTransformer`
36///
37/// # Examples
38///
39/// ```rust
40/// use qubit_function::{Transformer, RcTransformer};
41///
42/// let double = RcTransformer::new(|x: i32| x * 2);
43/// let identity = RcTransformer::<i32, i32>::identity();
44/// let conditional = double.when(|x: &i32| *x > 0).or_else(identity);
45///
46/// let conditional_clone = conditional.clone();
47///
48/// assert_eq!(conditional.apply(5), 10);
49/// assert_eq!(conditional_clone.apply(-5), -5);
50/// ```
51///
52pub struct RcConditionalTransformer<T, R> {
53 pub(super) transformer: RcTransformer<T, R>,
54 pub(super) predicate: RcPredicate<T>,
55}
56
57// Implement RcConditionalTransformer
58impl_shared_conditional_transformer!(
59 RcConditionalTransformer<T, R>,
60 RcTransformer,
61 Transformer,
62 into_rc,
63 'static
64);
65
66// Use macro to generate Debug and Display implementations
67impl_conditional_transformer_debug_display!(RcConditionalTransformer<T, R>);
68
69// Implement Clone for RcConditionalTransformer
70impl_conditional_transformer_clone!(RcConditionalTransformer<T, R>);