qubit_function/functions/bi_function_once/box_conditional_bi_function_once.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2025 - 2026.
4 * Haixing Hu, Qubit Co. Ltd.
5 *
6 * All rights reserved.
7 *
8 ******************************************************************************/
9//! Defines the `BoxConditionalBiFunctionOnce` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// ============================================================================
16// BoxConditionalBiFunctionOnce - Box-based Conditional BiFunction
17// ============================================================================
18
19/// BoxConditionalBiFunctionOnce struct
20///
21/// A conditional consuming bi-function that only executes when a bi-predicate
22/// is satisfied. Uses `BoxBiFunctionOnce` and `BoxBiPredicate` for single
23/// ownership semantics.
24///
25/// This type is typically created by calling `BoxBiFunctionOnce::when()` and
26/// is 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/// - **One-time Use**: Can only be called once
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/// ## With or_else Branch
38///
39/// ```rust
40/// use qubit_function::{BiFunctionOnce, BoxBiFunctionOnce};
41///
42/// let add = BoxBiFunctionOnce::new(|x: &i32, y: &i32| *x + *y);
43/// let multiply = BoxBiFunctionOnce::new(|x: &i32, y: &i32| *x * *y);
44/// let conditional = add.when(|x: &i32, y: &i32| *x > 0 && *y > 0).or_else(multiply);
45/// assert_eq!(conditional.apply(&5, &3), 8); // when branch executed
46///
47/// let add2 = BoxBiFunctionOnce::new(|x: &i32, y: &i32| *x + *y);
48/// let multiply2 = BoxBiFunctionOnce::new(|x: &i32, y: &i32| *x * *y);
49/// let conditional2 = add2.when(|x: &i32, y: &i32| *x > 0 && *y > 0).or_else(multiply2);
50/// assert_eq!(conditional2.apply(&-5, &3), -15); // or_else branch executed
51/// ```
52///
53/// # Author
54///
55/// Haixing Hu
56pub struct BoxConditionalBiFunctionOnce<T, U, R> {
57 pub(super) function: BoxBiFunctionOnce<T, U, R>,
58 pub(super) predicate: BoxBiPredicate<T, U>,
59}
60
61// Implement BoxConditionalBiFunctionOnce
62impl_box_conditional_function!(
63 BoxConditionalBiFunctionOnce<T, U, R>,
64 BoxBiFunctionOnce,
65 BiFunctionOnce
66);
67
68// Use macro to generate Debug and Display implementations
69impl_conditional_function_debug_display!(BoxConditionalBiFunctionOnce<T, U, R>);