qubit_function/functions/mutating_function/rc_conditional_mutating_function.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 `RcConditionalMutatingFunction` public type.
12
13#![allow(unused_imports)]
14
15use super::*;
16
17// ============================================================================
18// RcConditionalMutatingFunction - Rc-based Conditional Mutating Function
19// ============================================================================
20
21/// RcConditionalMutatingFunction struct
22///
23/// A single-threaded conditional function that only executes when a
24/// predicate is satisfied. Uses `RcMutatingFunction` and `RcPredicate` for shared
25/// ownership within a single thread.
26///
27/// This type is typically created by calling `RcMutatingFunction::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 `ArcConditionalFunction`
36///
37/// # Examples
38///
39/// ```rust
40/// use qubit_function::{MutatingFunction, RcMutatingFunction};
41///
42/// let double = RcMutatingFunction::new(|x: &mut i32| *x * 2);
43/// let identity = RcMutatingFunction::<i32, i32>::identity();
44/// let conditional = double.when(|x: &i32| *x > 0).or_else(identity);
45///
46/// let conditional_clone = conditional.clone();
47///
48/// let mut positive = 5;
49/// assert_eq!(conditional.apply(&mut positive), 10);
50/// let mut negative = -5;
51/// assert_eq!(conditional_clone.apply(&mut negative), -5);
52/// ```
53///
54pub struct RcConditionalMutatingFunction<T, R> {
55 pub(super) function: RcMutatingFunction<T, R>,
56 pub(super) predicate: RcPredicate<T>,
57}
58
59// Use macro to generate conditional function implementations
60impl_shared_conditional_function!(
61 RcConditionalMutatingFunction<T, R>,
62 RcMutatingFunction,
63 MutatingFunction,
64 'static
65);
66
67// Use macro to generate conditional function clone implementations
68impl_conditional_function_clone!(RcConditionalMutatingFunction<T, R>);
69
70// Use macro to generate conditional function debug and display implementations
71impl_conditional_function_debug_display!(RcConditionalMutatingFunction<T, R>);