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