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