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