qubit_function/transformers/transformer/box_transformer.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2025 - 2026.
4 * Haixing Hu, Qubit Co. Ltd.
5 *
6 * All rights reserved.
7 *
8 ******************************************************************************/
9//! Defines the `BoxTransformer` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15// ============================================================================
16// BoxTransformer - Box<dyn Fn(T) -> R>
17// ============================================================================
18
19/// BoxTransformer - transformer wrapper based on `Box<dyn Fn>`
20///
21/// A transformer wrapper that provides single ownership with reusable
22/// transformation. The transformer consumes the input and can be called
23/// multiple times.
24///
25/// # Features
26///
27/// - **Based on**: `Box<dyn Fn(T) -> R>`
28/// - **Ownership**: Single ownership, cannot be cloned
29/// - **Reusability**: Can be called multiple times (each call consumes its
30/// input)
31/// - **Thread Safety**: Not thread-safe (no `Send + Sync` requirement)
32///
33/// # Author
34///
35/// Haixing Hu
36pub struct BoxTransformer<T, R> {
37 pub(super) function: Box<dyn Fn(T) -> R>,
38 pub(super) name: Option<String>,
39}
40
41// Implement BoxTransformer
42impl<T, R> BoxTransformer<T, R> {
43 impl_transformer_common_methods!(
44 BoxTransformer<T, R>,
45 (Fn(T) -> R + 'static),
46 |f| Box::new(f)
47 );
48
49 impl_box_transformer_methods!(
50 BoxTransformer<T, R>,
51 BoxConditionalTransformer,
52 Transformer
53 );
54}
55
56// Implement constant method for BoxTransformer
57impl_transformer_constant_method!(BoxTransformer<T, R>);
58
59// Implement Debug and Display for BoxTransformer
60impl_transformer_debug_display!(BoxTransformer<T, R>);
61
62// Implement Transformer for BoxTransformer
63impl<T, R> Transformer<T, R> for BoxTransformer<T, R> {
64 fn apply(&self, input: T) -> R {
65 (self.function)(input)
66 }
67
68 // Generates: into_box(), into_rc(), into_fn(), into_once()
69 impl_box_conversions!(
70 BoxTransformer<T, R>,
71 RcTransformer,
72 Fn(T) -> R,
73 BoxTransformerOnce
74 );
75}