Skip to main content

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