qubit_function/functions/stateful_function/arc_conditional_stateful_function.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2025 - 2026.
4 * Haixing Hu, Qubit Co. Ltd.
5 *
6 * All rights reserved.
7 *
8 ******************************************************************************/
9//! Defines the `ArcConditionalStatefulFunction` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// ============================================================================
16// ArcConditionalStatefulFunction - Arc-based Conditional StatefulFunction
17// ============================================================================
18
19/// ArcConditionalStatefulFunction struct
20///
21/// A thread-safe conditional function that only executes when a predicate
22/// is satisfied. Uses `ArcStatefulFunction` and `ArcPredicate` for shared
23/// ownership across threads.
24///
25/// This type is typically created by calling `ArcStatefulFunction::when()` and is
26/// designed to work with the `or_else()` method to create if-then-else
27/// logic.
28///
29/// # Features
30///
31/// - **Shared Ownership**: Cloneable via `Arc`, multiple owners allowed
32/// - **Thread-Safe**: Implements `Send`, safe for concurrent use
33/// - **Conditional Execution**: Only maps when predicate returns `true`
34/// - **Chainable**: Can add `or_else` branch to create if-then-else
35/// logic
36///
37/// # Examples
38///
39/// ```rust
40/// use qubit_function::{StatefulFunction, ArcStatefulFunction};
41///
42/// let mut function = ArcStatefulFunction::new(|x: &i32| x * 2)
43/// .when(|x: &i32| *x > 0)
44/// .or_else(|x: &i32| -x);
45///
46/// let mut function_clone = function.clone();
47///
48/// assert_eq!(function.apply(&5), 10);
49/// assert_eq!(function_clone.apply(&-5), 5);
50/// ```
51///
52/// # Author
53///
54/// Haixing Hu
55pub struct ArcConditionalStatefulFunction<T, R> {
56 pub(super) function: ArcStatefulFunction<T, R>,
57 pub(super) predicate: ArcPredicate<T>,
58}
59
60// Use macro to generate conditional function implementations
61impl_shared_conditional_function!(
62 ArcConditionalStatefulFunction<T, R>,
63 ArcStatefulFunction,
64 StatefulFunction,
65 Send + Sync + 'static
66);
67
68// Use macro to generate conditional function clone implementations
69impl_conditional_function_clone!(ArcConditionalStatefulFunction<T, R>);
70
71// Use macro to generate conditional function debug and display implementations
72impl_conditional_function_debug_display!(ArcConditionalStatefulFunction<T, R>);