qubit_function/functions/stateful_mutating_function/box_conditional_stateful_mutating_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 `BoxConditionalStatefulMutatingFunction` public type.
12
13#![allow(unused_imports)]
14
15use super::*;
16
17// ============================================================================
18// BoxConditionalStatefulMutatingFunction - Box-based Conditional Stateful Mutating Function
19// ============================================================================
20
21/// BoxConditionalStatefulMutatingFunction struct
22///
23/// A conditional function that only executes when a predicate is satisfied.
24/// Uses `BoxStatefulMutatingFunction` and `BoxPredicate` for single ownership semantics.
25///
26/// This type is typically created by calling `BoxStatefulMutatingFunction::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::{StatefulMutatingFunction, BoxStatefulMutatingFunction};
42///
43/// let double = BoxStatefulMutatingFunction::new(|x: &mut i32| {
44/// *x *= 2;
45/// *x
46/// });
47/// let negate = BoxStatefulMutatingFunction::new(|x: &mut i32| {
48/// *x = -*x;
49/// *x
50/// });
51/// let mut conditional = double.when(|x: &i32| *x > 0).or_else(negate);
52///
53/// let mut positive = 5;
54/// let mut negative = -5;
55/// assert_eq!(conditional.apply(&mut positive), 10); // when branch executed
56/// assert_eq!(conditional.apply(&mut negative), 5); // or_else branch executed
57/// ```
58///
59pub struct BoxConditionalStatefulMutatingFunction<T, R> {
60 pub(super) function: BoxStatefulMutatingFunction<T, R>,
61 pub(super) predicate: BoxPredicate<T>,
62}
63
64// Use macro to generate conditional function implementations
65impl_box_conditional_function!(
66 BoxConditionalStatefulMutatingFunction<T, R>,
67 BoxStatefulMutatingFunction,
68 StatefulMutatingFunction
69);
70
71// Use macro to generate conditional function debug and display implementations
72impl_conditional_function_debug_display!(BoxConditionalStatefulMutatingFunction<T, R>);