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