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