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