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