@@ -9,7 +9,7 @@ use syn::{
9
9
punctuated:: Punctuated ,
10
10
spanned:: Spanned ,
11
11
token:: { Comma , Paren } ,
12
- DeriveInput , ExprPath , Ident , LitStr , Path , Result ,
12
+ DeriveInput , ExprClosure , ExprPath , Ident , LitStr , Path , Result ,
13
13
} ;
14
14
15
15
pub fn derive_event ( input : TokenStream ) -> TokenStream {
@@ -84,14 +84,22 @@ pub fn derive_component(input: TokenStream) -> TokenStream {
84
84
register_recursive_requires. push ( quote ! {
85
85
<#ident as Component >:: register_required_components( components, storages, required_components) ;
86
86
} ) ;
87
- if let Some ( func) = & require. func {
88
- register_required. push ( quote ! {
89
- required_components. register( components, storages, || { let x: #ident = #func( ) . into( ) ; x } ) ;
90
- } ) ;
91
- } else {
92
- register_required. push ( quote ! {
93
- required_components. register( components, storages, <#ident as Default >:: default ) ;
94
- } ) ;
87
+ match & require. func {
88
+ Some ( RequireFunc :: Path ( func) ) => {
89
+ register_required. push ( quote ! {
90
+ required_components. register( components, storages, || { let x: #ident = #func( ) . into( ) ; x } ) ;
91
+ } ) ;
92
+ }
93
+ Some ( RequireFunc :: Closure ( func) ) => {
94
+ register_required. push ( quote ! {
95
+ required_components. register( components, storages, || { let x: #ident = ( #func) ( ) . into( ) ; x } ) ;
96
+ } ) ;
97
+ }
98
+ None => {
99
+ register_required. push ( quote ! {
100
+ required_components. register( components, storages, <#ident as Default >:: default ) ;
101
+ } ) ;
102
+ }
95
103
}
96
104
}
97
105
}
@@ -162,7 +170,12 @@ enum StorageTy {
162
170
163
171
struct Require {
164
172
path : Path ,
165
- func : Option < Path > ,
173
+ func : Option < RequireFunc > ,
174
+ }
175
+
176
+ enum RequireFunc {
177
+ Path ( Path ) ,
178
+ Closure ( ExprClosure ) ,
166
179
}
167
180
168
181
// values for `storage` attribute
@@ -238,8 +251,12 @@ impl Parse for Require {
238
251
let func = if input. peek ( Paren ) {
239
252
let content;
240
253
parenthesized ! ( content in input) ;
241
- let func = content. parse :: < Path > ( ) ?;
242
- Some ( func)
254
+ if let Ok ( func) = content. parse :: < ExprClosure > ( ) {
255
+ Some ( RequireFunc :: Closure ( func) )
256
+ } else {
257
+ let func = content. parse :: < Path > ( ) ?;
258
+ Some ( RequireFunc :: Path ( func) )
259
+ }
243
260
} else {
244
261
None
245
262
} ;
0 commit comments