@@ -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 {
@@ -90,24 +90,37 @@ pub fn derive_component(input: TokenStream) -> TokenStream {
90
90
inheritance_depth + 1
91
91
) ;
92
92
} ) ;
93
- if let Some ( func) = & require. func {
94
- register_required. push ( quote ! {
95
- components. register_required_components_manual:: <Self , #ident>(
96
- storages,
97
- required_components,
98
- || { let x: #ident = #func( ) . into( ) ; x } ,
99
- inheritance_depth
100
- ) ;
101
- } ) ;
102
- } else {
103
- register_required. push ( quote ! {
104
- components. register_required_components_manual:: <Self , #ident>(
105
- storages,
106
- required_components,
107
- <#ident as Default >:: default ,
108
- inheritance_depth
109
- ) ;
110
- } ) ;
93
+ match & require. func {
94
+ Some ( RequireFunc :: Path ( func) ) => {
95
+ register_required. push ( quote ! {
96
+ components. register_required_components_manual:: <Self , #ident>(
97
+ storages,
98
+ required_components,
99
+ || { let x: #ident = #func( ) . into( ) ; x } ,
100
+ inheritance_depth
101
+ ) ;
102
+ } ) ;
103
+ }
104
+ Some ( RequireFunc :: Closure ( func) ) => {
105
+ register_required. push ( quote ! {
106
+ components. register_required_components_manual:: <Self , #ident>(
107
+ storages,
108
+ required_components,
109
+ || { let x: #ident = ( #func) ( ) . into( ) ; x } ,
110
+ inheritance_depth
111
+ ) ;
112
+ } ) ;
113
+ }
114
+ None => {
115
+ register_required. push ( quote ! {
116
+ components. register_required_components_manual:: <Self , #ident>(
117
+ storages,
118
+ required_components,
119
+ <#ident as Default >:: default ,
120
+ inheritance_depth
121
+ ) ;
122
+ } ) ;
123
+ }
111
124
}
112
125
}
113
126
}
@@ -180,7 +193,12 @@ enum StorageTy {
180
193
181
194
struct Require {
182
195
path : Path ,
183
- func : Option < Path > ,
196
+ func : Option < RequireFunc > ,
197
+ }
198
+
199
+ enum RequireFunc {
200
+ Path ( Path ) ,
201
+ Closure ( ExprClosure ) ,
184
202
}
185
203
186
204
// values for `storage` attribute
@@ -256,8 +274,12 @@ impl Parse for Require {
256
274
let func = if input. peek ( Paren ) {
257
275
let content;
258
276
parenthesized ! ( content in input) ;
259
- let func = content. parse :: < Path > ( ) ?;
260
- Some ( func)
277
+ if let Ok ( func) = content. parse :: < ExprClosure > ( ) {
278
+ Some ( RequireFunc :: Closure ( func) )
279
+ } else {
280
+ let func = content. parse :: < Path > ( ) ?;
281
+ Some ( RequireFunc :: Path ( func) )
282
+ }
261
283
} else {
262
284
None
263
285
} ;
0 commit comments