Skip to main content

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