qubit_function/functions/bi_function/arc_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 `ArcConditionalBiFunction` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// ============================================================================
16// ArcConditionalBiFunction - Arc-based Conditional BiFunction
17// ============================================================================
18
19/// ArcConditionalBiFunction struct
20///
21/// A thread-safe conditional bi-function that only executes when a
22/// bi-predicate is satisfied. Uses `ArcBiFunction` and `ArcBiPredicate` for
23/// shared ownership across threads.
24///
25/// This type is typically created by calling `ArcBiFunction::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 `Arc`, multiple owners allowed
31/// - **Thread-Safe**: Implements `Send + Sync`, safe for concurrent use
32/// - **Conditional Execution**: Only computes when bi-predicate returns `true`
33/// - **Chainable**: Can add `or_else` branch to create if-then-else logic
34///
35/// # Examples
36///
37/// ```rust
38/// use qubit_function::{BiFunction, ArcBiFunction};
39///
40/// let add = ArcBiFunction::new(|x: &i32, y: &i32| *x + *y);
41/// let multiply = ArcBiFunction::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 ArcConditionalBiFunction<T, U, R> {
54 pub(super) function: ArcBiFunction<T, U, R>,
55 pub(super) predicate: ArcBiPredicate<T, U>,
56}
57
58// Implement ArcConditionalBiFunction
59impl_shared_conditional_function!(
60 ArcConditionalBiFunction<T, U, R>,
61 ArcBiFunction,
62 BiFunction,
63 into_arc,
64 Send + Sync + 'static
65);
66
67// Implement Debug and Display for ArcConditionalBiFunction
68impl_conditional_function_debug_display!(ArcConditionalBiFunction<T, U, R>);
69
70// Implement Clone for ArcConditionalBiFunction
71impl_conditional_function_clone!(ArcConditionalBiFunction<T, U, R>);