qubit_function/functions/bi_function/box_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 `BoxConditionalBiFunction` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// ============================================================================
16// BoxConditionalBiFunction - Box-based Conditional BiFunction
17// ============================================================================
18
19/// BoxConditionalBiFunction struct
20///
21/// A conditional bi-function that only executes when a bi-predicate is
22/// satisfied. Uses `BoxBiFunction` and `BoxBiPredicate` for single
23/// ownership semantics.
24///
25/// This type is typically created by calling `BoxBiFunction::when()` and is
26/// designed to work with the `or_else()` method to create if-then-else logic.
27///
28/// # Features
29///
30/// - **Single Ownership**: Not cloneable, consumes `self` on use
31/// - **Conditional Execution**: Only computes when bi-predicate returns `true`
32/// - **Chainable**: Can add `or_else` branch to create if-then-else logic
33/// - **Implements BiFunction**: Can be used anywhere a `BiFunction` is expected
34///
35/// # Examples
36///
37/// ## With or_else Branch
38///
39/// ```rust
40/// use qubit_function::{BiFunction, BoxBiFunction};
41///
42/// let add = BoxBiFunction::new(|x: &i32, y: &i32| *x + *y);
43/// let multiply = BoxBiFunction::new(|x: &i32, y: &i32| *x * *y);
44/// let conditional = add.when(|x: &i32, y: &i32| *x > 0).or_else(multiply);
45///
46/// assert_eq!(conditional.apply(&5, &3), 8); // when branch executed
47/// assert_eq!(conditional.apply(&-5, &3), -15); // or_else branch executed
48/// ```
49///
50/// # Author
51///
52/// Haixing Hu
53pub struct BoxConditionalBiFunction<T, U, R> {
54 pub(super) function: BoxBiFunction<T, U, R>,
55 pub(super) predicate: BoxBiPredicate<T, U>,
56}
57
58// Implement BoxConditionalBiFunction
59impl_box_conditional_function!(
60 BoxConditionalBiFunction<T, U, R>,
61 BoxBiFunction,
62 BiFunction
63);
64
65// Use macro to generate Debug and Display implementations
66impl_conditional_function_debug_display!(BoxConditionalBiFunction<T, U, R>);