qubit_function/functions/function_once/box_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 `BoxFunctionOnce` public type.
12
13use super::{
14 BoxConditionalFunctionOnce,
15 FunctionOnce,
16 Predicate,
17 impl_box_function_methods,
18 impl_box_once_conversions,
19 impl_closure_once_trait,
20 impl_function_common_methods,
21 impl_function_constant_method,
22 impl_function_debug_display,
23 impl_function_identity_method,
24};
25
26// ============================================================================
27// BoxFunctionOnce - Box<dyn FnOnce(&T) -> R>
28// ============================================================================
29
30/// BoxFunctionOnce - consuming transformer wrapper based on
31/// `Box<dyn FnOnce>`
32///
33/// A transformer wrapper that provides single ownership with one-time use
34/// semantics. Consumes both self and the input value.
35///
36/// # Features
37///
38/// - **Based on**: `Box<dyn FnOnce(&T) -> R>`
39/// - **Ownership**: Single ownership, cannot be cloned
40/// - **Reusability**: Can only be called once (consumes self and input)
41/// - **Thread Safety**: Not thread-safe (no `Send + Sync` requirement)
42///
43pub struct BoxFunctionOnce<T, R> {
44 pub(super) function: Box<dyn FnOnce(&T) -> R>,
45 pub(super) name: Option<String>,
46}
47
48impl<T, R> BoxFunctionOnce<T, R> {
49 // Generates: new(), new_with_name(), new_with_optional_name(), name(), set_name()
50 impl_function_common_methods!(
51 BoxFunctionOnce<T, R>,
52 (FnOnce(&T) -> R + 'static),
53 |f| Box::new(f)
54 );
55
56 // Generates: when(), and_then(), compose()
57 impl_box_function_methods!(
58 BoxFunctionOnce<T, R>,
59 BoxConditionalFunctionOnce,
60 FunctionOnce
61 );
62}
63
64impl<T, R> FunctionOnce<T, R> for BoxFunctionOnce<T, R> {
65 fn apply(self, input: &T) -> R {
66 (self.function)(input)
67 }
68
69 impl_box_once_conversions!(
70 BoxFunctionOnce<T, R>,
71 FunctionOnce,
72 FnOnce(&T) -> R
73 );
74}
75
76// Generates: constant() method for BoxFunctionOnce<T, R>
77impl_function_constant_method!(BoxFunctionOnce<T, R>, 'static);
78
79// Generates: identity() method for BoxFunctionOnce<T, T>
80impl_function_identity_method!(BoxFunctionOnce<T, T>);
81
82// Generates: Debug and Display implementations for BoxFunctionOnce<T, R>
83impl_function_debug_display!(BoxFunctionOnce<T, R>);
84
85// ============================================================================
86// Blanket implementation for standard FnOnce trait
87// ============================================================================
88
89// Implement FunctionOnce for all FnOnce(&T) -> R using macro
90impl_closure_once_trait!(
91 FunctionOnce<T, R>,
92 apply,
93 BoxFunctionOnce,
94 FnOnce(input: &T) -> R
95);