@@ -124,9 +124,22 @@ pub enum CallStyle {
124
124
125
125
// Note: we could use currying to make this a little nicer
126
126
127
- /// Trait object that implements the extension function call.
128
- pub type ExtensionFunctionObject =
129
- Box < dyn Fn ( & [ Value ] ) -> evaluator:: Result < ExtensionOutputValue > + Sync + Send + ' static > ;
127
+ macro_rules! extension_function_object {
128
+ ( $( $tys: ty ) , * ) => {
129
+ Box <dyn Fn ( $( $tys, ) * ) -> evaluator:: Result <ExtensionOutputValue > + Sync + Send + ' static >
130
+ }
131
+ }
132
+
133
+ /// Trait object that implements the extension function call accepting any number of arguments.
134
+ pub type ExtensionFunctionObject = extension_function_object ! ( & [ Value ] ) ;
135
+ /// Trait object that implements the extension function call accepting exactly 0 arguments
136
+ pub type NullaryExtensionFunctionObject = extension_function_object ! ( ) ;
137
+ /// Trait object that implements the extension function call accepting exactly 1 arguments
138
+ pub type UnaryExtensionFunctionObject = extension_function_object ! ( & Value ) ;
139
+ /// Trait object that implements the extension function call accepting exactly 2 arguments
140
+ pub type BinaryExtensionFunctionObject = extension_function_object ! ( & Value , & Value ) ;
141
+ /// Trait object that implements the extension function call accepting exactly 3 arguments
142
+ pub type TernaryExtensionFunctionObject = extension_function_object ! ( & Value , & Value , & Value ) ;
130
143
131
144
/// Extension function. These can be called by the given `name` in Ceder
132
145
/// expressions.
@@ -172,7 +185,7 @@ impl ExtensionFunction {
172
185
pub fn nullary (
173
186
name : Name ,
174
187
style : CallStyle ,
175
- func : Box < dyn Fn ( ) -> evaluator :: Result < ExtensionOutputValue > + Sync + Send + ' static > ,
188
+ func : NullaryExtensionFunctionObject ,
176
189
return_type : SchemaType ,
177
190
) -> Self {
178
191
Self :: new (
@@ -200,14 +213,14 @@ impl ExtensionFunction {
200
213
pub fn partial_eval_unknown (
201
214
name : Name ,
202
215
style : CallStyle ,
203
- func : Box < dyn Fn ( Value ) -> evaluator :: Result < ExtensionOutputValue > + Sync + Send + ' static > ,
216
+ func : UnaryExtensionFunctionObject ,
204
217
arg_type : SchemaType ,
205
218
) -> Self {
206
219
Self :: new (
207
220
name. clone ( ) ,
208
221
style,
209
222
Box :: new ( move |args : & [ Value ] | match args. first ( ) {
210
- Some ( arg) => func ( arg. clone ( ) ) ,
223
+ Some ( arg) => func ( arg) ,
211
224
None => Err ( evaluator:: EvaluationError :: wrong_num_arguments (
212
225
name. clone ( ) ,
213
226
1 ,
@@ -221,18 +234,19 @@ impl ExtensionFunction {
221
234
}
222
235
223
236
/// Create a new `ExtensionFunction` taking one argument
237
+ #[ allow( clippy:: type_complexity) ]
224
238
pub fn unary (
225
239
name : Name ,
226
240
style : CallStyle ,
227
- func : Box < dyn Fn ( Value ) -> evaluator :: Result < ExtensionOutputValue > + Sync + Send + ' static > ,
241
+ func : UnaryExtensionFunctionObject ,
228
242
return_type : SchemaType ,
229
243
arg_type : SchemaType ,
230
244
) -> Self {
231
245
Self :: new (
232
246
name. clone ( ) ,
233
247
style,
234
248
Box :: new ( move |args : & [ Value ] | match & args {
235
- & [ arg] => func ( arg. clone ( ) ) ,
249
+ & [ arg] => func ( arg) ,
236
250
_ => Err ( evaluator:: EvaluationError :: wrong_num_arguments (
237
251
name. clone ( ) ,
238
252
1 ,
@@ -246,20 +260,19 @@ impl ExtensionFunction {
246
260
}
247
261
248
262
/// Create a new `ExtensionFunction` taking two arguments
263
+ #[ allow( clippy:: type_complexity) ]
249
264
pub fn binary (
250
265
name : Name ,
251
266
style : CallStyle ,
252
- func : Box <
253
- dyn Fn ( Value , Value ) -> evaluator:: Result < ExtensionOutputValue > + Sync + Send + ' static ,
254
- > ,
267
+ func : BinaryExtensionFunctionObject ,
255
268
return_type : SchemaType ,
256
269
arg_types : ( SchemaType , SchemaType ) ,
257
270
) -> Self {
258
271
Self :: new (
259
272
name. clone ( ) ,
260
273
style,
261
274
Box :: new ( move |args : & [ Value ] | match & args {
262
- & [ first, second] => func ( first. clone ( ) , second. clone ( ) ) ,
275
+ & [ first, second] => func ( first, second) ,
263
276
_ => Err ( evaluator:: EvaluationError :: wrong_num_arguments (
264
277
name. clone ( ) ,
265
278
2 ,
@@ -273,23 +286,19 @@ impl ExtensionFunction {
273
286
}
274
287
275
288
/// Create a new `ExtensionFunction` taking three arguments
289
+ #[ allow( clippy:: type_complexity) ]
276
290
pub fn ternary (
277
291
name : Name ,
278
292
style : CallStyle ,
279
- func : Box <
280
- dyn Fn ( Value , Value , Value ) -> evaluator:: Result < ExtensionOutputValue >
281
- + Sync
282
- + Send
283
- + ' static ,
284
- > ,
293
+ func : TernaryExtensionFunctionObject ,
285
294
return_type : SchemaType ,
286
295
arg_types : ( SchemaType , SchemaType , SchemaType ) ,
287
296
) -> Self {
288
297
Self :: new (
289
298
name. clone ( ) ,
290
299
style,
291
300
Box :: new ( move |args : & [ Value ] | match & args {
292
- & [ first, second, third] => func ( first. clone ( ) , second. clone ( ) , third. clone ( ) ) ,
301
+ & [ first, second, third] => func ( first, second, third) ,
293
302
_ => Err ( evaluator:: EvaluationError :: wrong_num_arguments (
294
303
name. clone ( ) ,
295
304
3 ,
0 commit comments