qubit_function/functions/function_once/box_conditional_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 `BoxConditionalFunctionOnce` public type.
12
13#![allow(unused_imports)]
14
15use super::*;
16
17// ============================================================================
18// BoxConditionalFunctionOnce - Box-based Conditional Function
19// ============================================================================
20
21/// BoxConditionalFunctionOnce struct
22///
23/// A conditional consuming transformer that only executes when a predicate is
24/// satisfied. Uses `BoxFunctionOnce` and `BoxPredicate` for single
25/// ownership semantics.
26///
27/// This type is typically created by calling `BoxFunctionOnce::when()` and
28/// is designed to work with the `or_else()` method to create if-then-else
29/// logic.
30///
31/// # Features
32///
33/// - **Single Ownership**: Not cloneable, consumes `self` on use
34/// - **One-time Use**: Can only be called once
35/// - **Conditional Execution**: Only transforms when predicate returns `true`
36/// - **Chainable**: Can add `or_else` branch to create if-then-else logic
37///
38/// # Examples
39///
40/// ## With or_else Branch
41///
42/// ```rust
43/// use qubit_function::{FunctionOnce, BoxFunctionOnce};
44///
45/// let double = BoxFunctionOnce::new(|x: &i32| x * 2);
46/// let negate = BoxFunctionOnce::new(|x: &i32| -x);
47/// let conditional = double.when(|x: &i32| *x > 0).or_else(negate);
48/// assert_eq!(conditional.apply(&5), 10); // when branch executed
49///
50/// let double2 = BoxFunctionOnce::new(|x: &i32| x * 2);
51/// let negate2 = BoxFunctionOnce::new(|x: &i32| -x);
52/// let conditional2 = double2.when(|x: &i32| *x > 0).or_else(negate2);
53/// assert_eq!(conditional2.apply(&-5), 5); // or_else branch executed
54/// ```
55///
56pub struct BoxConditionalFunctionOnce<T, R> {
57 pub(super) function: BoxFunctionOnce<T, R>,
58 pub(super) predicate: BoxPredicate<T>,
59}
60
61// Use macro to generate conditional function implementations
62impl_box_conditional_function!(
63 BoxConditionalFunctionOnce<T, R>,
64 BoxFunctionOnce,
65 FunctionOnce
66);
67
68// Use macro to generate conditional function debug and display implementations
69impl_conditional_function_debug_display!(BoxConditionalFunctionOnce<T, R>);