prism3_function/macros/
closure_once_trait.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025.
4 *    3-Prism Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9//! # Closure Once Trait Implementation Macro
10//!
11//! This module provides the `impl_closure_once_trait!` macro for implementing
12//! common conversion methods for closure-based once traits.
13//!
14//! ## Overview
15//!
16//! The macro generates standard conversion methods (`into_box`, `into_fn`) for
17//! traits that are implemented by closures with once semantics. It
18//! automatically infers all necessary information from the function signature
19//! and trait name.
20//!
21//! ## Generated Methods
22//!
23//! - `into_box()`: Converts the closure into a boxed wrapper type
24//! - `into_fn()`: Returns the closure as a generic `FnOnce` implementation
25//! - Core method: Direct delegation to the underlying closure
26//!
27//! ## Usage
28//!
29//! The macro is typically used in trait definitions to provide consistent
30//! conversion methods across different once trait implementations.
31//!
32//! ## Author
33//!
34//! Haixing Hu
35
36/// Implement common conversion methods for closure once traits
37///
38/// This macro generates standard conversion methods for all once traits
39/// that are implemented by closures. It automatically infers everything from
40/// the function signature and trait name.
41///
42/// # Parameters
43///
44/// * `$trait_name<$(generics),*>` - Full trait name with generics (e.g., `ConsumerOnce<T>`, `BiFunctionOnce<T, U, R>`)
45/// * `$method_name` - Core method name (e.g., `accept`, `apply`)
46/// * `$box_type` - Box wrapper type (e.g., `BoxConsumerOnce`, `BoxBiFunctionOnce`)
47/// * `$fn_trait` - Function signature (e.g., `FnOnce(value: &T)`, `FnOnce(first: &T, second: &U) -> R`)
48///
49/// # Generated implementation
50///
51/// ```ignore
52/// impl<F, Generics...> TraitName<Generics...> for F
53/// where
54///     F: FnOnce(...),
55/// {
56///     fn method_name(self, ...) {
57///         self(...)
58///     }
59///     fn into_box(self) -> BoxType<...> { ... }
60///     fn into_fn(self) -> impl FnOnce(...) { ... }
61/// }
62/// ```
63///
64/// # Examples
65///
66/// ```ignore
67/// // ConsumerOnce<T>
68/// impl_closure_once_trait!(
69///     ConsumerOnce<T>,
70///     accept,
71///     BoxConsumerOnce,
72///     FnOnce(value: &T)
73/// );
74///
75/// // BiConsumerOnce<T, U>
76/// impl_closure_once_trait!(
77///     BiConsumerOnce<T, U>,
78///     accept,
79///     BoxBiConsumerOnce,
80///     FnOnce(first: &T, second: &U)
81/// );
82///
83/// // FunctionOnce<T, R>
84/// impl_closure_once_trait!(
85///     FunctionOnce<T, R>,
86///     apply,
87///     BoxFunctionOnce,
88///     FnOnce(input: &T) -> R
89/// );
90/// ```
91///
92/// # Author
93///
94/// Haixing Hu
95macro_rules! impl_closure_once_trait {
96  // ==================== Internal Implementation ====================
97
98  // Core implementation: Generate complete trait implementation
99  (
100      @impl
101      $trait_name:ident < $($generics:ident),* >,
102      $method_name:ident,
103      $box_type:ident,
104      ($($arg:ident : $arg_ty:ty),*) $(-> $ret:ty)?
105  ) => {
106      impl<F, $($generics),*> $trait_name<$($generics),*> for F
107      where
108          F: FnOnce($($arg_ty),*) $(-> $ret)?,
109      {
110          // Core method: Direct closure call
111          fn $method_name(self, $($arg : $arg_ty),*) $(-> $ret)? {
112              self($($arg),*)
113          }
114
115          // into_box: Convert to Box type
116          fn into_box(self) -> $box_type<$($generics),*>
117          where
118              Self: Sized + 'static,
119              $($generics: 'static),*
120          {
121              $box_type::new(self)
122          }
123
124          // into_fn: Convert to closure (always return self directly since F is already FnOnce)
125          fn into_fn(self) -> impl FnOnce($($arg_ty),*) $(-> $ret)?
126          where
127              Self: Sized + 'static,
128          {
129              // F is already FnOnce with the correct signature, return directly (zero cost)
130              self
131          }
132      }
133  };
134
135  // ==================== Public Interface ====================
136
137  // No return value version
138  (
139      $trait_name:ident < $($generics:ident),* >,
140      $method_name:ident,
141      $box_type:ident,
142      FnOnce($($arg:ident : $arg_ty:ty),*)
143  ) => {
144      impl_closure_once_trait!(
145          @impl
146          $trait_name<$($generics),*>,
147          $method_name,
148          $box_type,
149          ($($arg : $arg_ty),*)
150      );
151  };
152
153  // With return value version
154  (
155      $trait_name:ident < $($generics:ident),* >,
156      $method_name:ident,
157      $box_type:ident,
158      FnOnce($($arg:ident : $arg_ty:ty),*) -> $ret:ty
159  ) => {
160      impl_closure_once_trait!(
161          @impl
162          $trait_name<$($generics),*>,
163          $method_name,
164          $box_type,
165          ($($arg : $arg_ty),*) -> $ret
166      );
167  };
168}
169
170pub(crate) use impl_closure_once_trait;