qubit_function/functions/stateful_mutating_function/box_conditional_stateful_mutating_function.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2025 - 2026.
4 * Haixing Hu, Qubit Co. Ltd.
5 *
6 * All rights reserved.
7 *
8 ******************************************************************************/
9//! Defines the `BoxConditionalStatefulMutatingFunction` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// ============================================================================
16// BoxConditionalStatefulMutatingFunction - Box-based Conditional Stateful Mutating Function
17// ============================================================================
18
19/// BoxConditionalStatefulMutatingFunction struct
20///
21/// A conditional function that only executes when a predicate is satisfied.
22/// Uses `BoxStatefulMutatingFunction` and `BoxPredicate` for single ownership semantics.
23///
24/// This type is typically created by calling `BoxStatefulMutatingFunction::when()` and is
25/// designed to work with the `or_else()` method to create if-then-else logic.
26///
27/// # Features
28///
29/// - **Single Ownership**: Not cloneable, consumes `self` on use
30/// - **Conditional Execution**: Only transforms when predicate returns `true`
31/// - **Chainable**: Can add `or_else` branch to create if-then-else logic
32/// - **Implements Function**: Can be used anywhere a `Function` is expected
33///
34/// # Examples
35///
36/// ## With or_else Branch
37///
38/// ```rust
39/// use qubit_function::{StatefulMutatingFunction, BoxStatefulMutatingFunction};
40///
41/// let double = BoxStatefulMutatingFunction::new(|x: &mut i32| {
42/// *x *= 2;
43/// *x
44/// });
45/// let negate = BoxStatefulMutatingFunction::new(|x: &mut i32| {
46/// *x = -*x;
47/// *x
48/// });
49/// let mut conditional = double.when(|x: &i32| *x > 0).or_else(negate);
50///
51/// let mut positive = 5;
52/// let mut negative = -5;
53/// assert_eq!(conditional.apply(&mut positive), 10); // when branch executed
54/// assert_eq!(conditional.apply(&mut negative), 5); // or_else branch executed
55/// ```
56///
57/// # Author
58///
59/// Haixing Hu
60pub struct BoxConditionalStatefulMutatingFunction<T, R> {
61 pub(super) function: BoxStatefulMutatingFunction<T, R>,
62 pub(super) predicate: BoxPredicate<T>,
63}
64
65// Use macro to generate conditional function implementations
66impl_box_conditional_function!(
67 BoxConditionalStatefulMutatingFunction<T, R>,
68 BoxStatefulMutatingFunction,
69 StatefulMutatingFunction
70);
71
72// Use macro to generate conditional function debug and display implementations
73impl_conditional_function_debug_display!(BoxConditionalStatefulMutatingFunction<T, R>);