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