1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*******************************************************************************
*
* Copyright (c) 2025 - 2026.
* Haixing Hu, Qubit Co. Ltd.
*
* All rights reserved.
*
******************************************************************************/
//! # Fn Ops Trait Macro
//!
//! Generate extension traits and implementations for closure types
//!
//! This macro generates extension traits for closure types that implement
//! `Fn` or `FnMut`, providing `and_then` and `when` methods without requiring
//! explicit wrapping as `BoxFunction`, `RcFunction`, or `ArcFunction`.
//!
//! # Parameters
//!
//! * `$fn_signature` - Closure signature (in parentheses, without constraints)
//! Examples: `(Fn(&T) -> R)`, `(FnMut(&T) -> R)`, `(FnMut(&mut T) -> R)`
//! * `$trait_name` - Name of the extension trait (e.g., `FnFunctionOps`,
//! `FnStatefulFunctionOps`)
//! * `$box_type` - Box wrapper type (e.g., `BoxFunction`, `BoxStatefulFunction`)
//! * `$chained_function_trait` - The name of the function trait that chained
//! after the execution of this function (e.g., Function, BiFunction)
//! * `$conditional_type` - Conditional function type (e.g., `BoxConditionalFunction`)
//!
//! # Implementation Notes
//!
//! The macro uses mutable references (`&mut`) uniformly because in Rust,
//! `&mut T` can be automatically dereferenced to `&T`. This allows both `Fn`
//! and `FnMut` closures to use the same implementation logic, simplifying
//! the code and improving performance (avoiding additional boxing operations).
//!
//! # Usage Examples
//!
//! ```ignore
//! // Generate extension trait for Fn(&T) -> R
//! impl_fn_ops_trait!(
//! (Fn(&T) -> R),
//! FnFunctionOps,
//! BoxFunction,
//! Function,
//! BoxConditionalFunction
//! );
//!
//! // Generate extension trait for FnMut(&T) -> R
//! impl_fn_ops_trait!(
//! (FnMut(&T) -> R),
//! FnStatefulFunctionOps,
//! BoxStatefulFunction,
//! StatefulFunction,
//! BoxConditionalStatefulFunction
//! );
//!
//! // Generate extension trait for FnMut(&mut T) -> R (consuming functions)
//! impl_fn_ops_trait!(
//! (FnMut(&mut T) -> R),
//! FnMutatingFunctionOps,
//! BoxMutatingFunction,
//! MutatingFunction,
//! BoxConditionalMutatingFunction
//! );
//! ```
//!
//! # Author
//!
//! Haixing Hu
/// Generate extension traits and implementations for closure types
///
/// This macro generates an extension trait that provides composition methods
/// (`and_then`, `when`) for closures implementing the specified
/// closure trait, without requiring explicit wrapping.
///
/// # Unified Implementation Strategy
///
/// The macro uses a unified implementation approach, passing intermediate
/// results using mutable references (`&mut`). This is because:
/// 1. In Rust, `&mut T` can be automatically dereferenced to `&T`
/// 2. Avoids code duplication and simplifies the macro implementation
/// 3. Better performance by avoiding additional boxing operations
/// 4. Uses `#[allow(unused_mut)]` to suppress unnecessary mutability warnings
///
/// # Parameters
///
/// * `$fn_signature` - Closure signature (in parentheses, without constraints)
/// * `$trait_name` - Name of the extension trait
/// * `$box_type` - Box wrapper type
/// * `$chained_function_trait` - The name of the function trait that chained
/// after the execution of this function (e.g., Function, BiFunction)
/// * `$conditional_type` - Conditional function type
///
/// # Generated Code
///
/// Generates a trait definition and a blanket implementation, containing:
/// - `and_then<S, F>` - Chain composition method
/// - `when<P>` - Conditional execution method
///
/// # Examples
///
/// ```ignore
/// // Fn(&T) -> R version
/// impl_fn_ops_trait!(
/// (Fn(&T) -> R),
/// FnFunctionOps,
/// BoxFunction,
/// Function,
/// BoxConditionalFunction
/// );
///
/// // FnMut(&T) -> R version
/// impl_fn_ops_trait!(
/// (FnMut(&T) -> R),
/// FnStatefulFunctionOps,
/// BoxStatefulFunction,
/// StatefulFunction,
/// BoxConditionalStatefulFunction
/// );
///
/// // FnMut(&mut T) -> R version (consuming functions)
/// impl_fn_ops_trait!(
/// (FnMut(&mut T) -> R),
/// FnMutatingFunctionOps,
/// BoxMutatingFunction,
/// MutatingFunction,
/// BoxConditionalMutatingFunction
/// );
/// ```
///
/// # Author
///
/// Haixing Hu
)
}
/// Creates a conditional function
///
/// Returns a function that only executes when a predicate is satisfied.
/// You must call `or_else()` to provide an alternative function for when
/// the condition is not satisfied.
///
/// # Parameters
///
/// * `predicate` - The condition to check. **Note: This parameter is passed
/// by value and will transfer ownership.** If you need to preserve the
/// original predicate, clone it first (if it implements `Clone`). Can be:
/// - A closure: `|x: &T| -> bool`
/// - A function pointer: `fn(&T) -> bool`
/// - A `BoxPredicate<T>`
/// - An `RcPredicate<T>`
/// - An `ArcPredicate<T>`
/// - Any type implementing `Predicate<T>`
///
/// # Returns
///
/// Returns the appropriate conditional function type
///
/// # Examples
///
/// ## Basic usage with or_else
///
/// ```rust
/// use qubit_function::{Function, FnFunctionOps};
///
/// let double = |x: i32| x * 2;
/// let conditional = double.when(|x: &i32| *x > 0).or_else(|x: i32| -x);
///
/// assert_eq!(conditional.apply(5), 10);
/// assert_eq!(conditional.apply(-5), 5);
/// ```
///
/// ## Preserving predicate with clone
///
/// ```rust
/// use qubit_function::{Function, FnFunctionOps, BoxPredicate};
///
/// let double = |x: i32| x * 2;
/// let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
///
/// // Clone to preserve original predicate
/// let conditional = double.when(is_positive.clone())
/// .or_else(|x: i32| -x);
///
/// assert_eq!(conditional.apply(5), 10);
///
/// // Original predicate still usable
/// assert!(is_positive.test(&3));
/// ```
}
/// Blanket implementation for all closures
///
/// Automatically implements the extension trait for any type that
/// implements the base function trait.
///
/// # Author
///
/// Haixing Hu
};
}
pub use impl_fn_ops_trait;