Skip to main content

qubit_function/functions/stateful_mutating_function/
rc_conditional_stateful_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 `RcConditionalStatefulMutatingFunction` public type.
12
13use super::{
14    Predicate,
15    RcPredicate,
16    RcStatefulMutatingFunction,
17    StatefulMutatingFunction,
18    impl_conditional_function_clone,
19    impl_conditional_function_debug_display,
20    impl_shared_conditional_function,
21};
22
23// ============================================================================
24// RcConditionalStatefulMutatingFunction - Rc-based Conditional Stateful Mutating Function
25// ============================================================================
26
27/// RcConditionalStatefulMutatingFunction struct
28///
29/// A single-threaded conditional function that only executes when a
30/// predicate is satisfied. Uses `RcStatefulMutatingFunction` and `RcPredicate` for shared
31/// ownership within a single thread.
32///
33/// This type is typically created by calling `RcStatefulMutatingFunction::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::{StatefulMutatingFunction, RcStatefulMutatingFunction};
47///
48/// let double = RcStatefulMutatingFunction::new(|x: &mut i32| {
49///     *x *= 2;
50///     *x
51/// });
52/// let identity = RcStatefulMutatingFunction::<i32, i32>::identity();
53/// let mut conditional = double.when(|x: &i32| *x > 0).or_else(identity);
54///
55/// let mut conditional_clone = conditional.clone();
56///
57/// let mut positive = 5;
58/// let mut negative = -5;
59/// assert_eq!(conditional.apply(&mut positive), 10);
60/// assert_eq!(conditional_clone.apply(&mut negative), -5);
61/// ```
62///
63pub struct RcConditionalStatefulMutatingFunction<T, R> {
64    pub(super) function: RcStatefulMutatingFunction<T, R>,
65    pub(super) predicate: RcPredicate<T>,
66}
67
68// Use macro to generate conditional function implementations
69impl_shared_conditional_function!(
70    RcConditionalStatefulMutatingFunction<T, R>,
71    RcStatefulMutatingFunction,
72    StatefulMutatingFunction,
73    'static
74);
75
76// Use macro to generate conditional function clone implementations
77impl_conditional_function_clone!(RcConditionalStatefulMutatingFunction<T, R>);
78
79// Use macro to generate conditional function debug and display implementations
80impl_conditional_function_debug_display!(RcConditionalStatefulMutatingFunction<T, R>);