File tree Expand file tree Collapse file tree 2 files changed +36
-1
lines changed Expand file tree Collapse file tree 2 files changed +36
-1
lines changed Original file line number Diff line number Diff line change @@ -493,7 +493,21 @@ pub fn derive_from_variant(input: TokenStream) -> TokenStream {
493
493
/// #[opt] baz: Option<Ref<Node>>,
494
494
/// }
495
495
/// ```
496
- #[ proc_macro_derive( FromVarargs , attributes( opt) ) ]
496
+ ///
497
+ /// ## Field attributes
498
+ ///
499
+ /// Attributes can be used to customize behavior of certain fields. All attributes are optional.
500
+ ///
501
+ /// ### `#[opt]`
502
+ ///
503
+ /// Marks an argument as optional. Required arguments must precede all optional arguments.
504
+ /// Default values are obtained through `Default::default`.
505
+ ///
506
+ /// ### `#[skip]`
507
+ ///
508
+ /// Instructs the macro to skip a field. Skipped fields do not affect the signature of the
509
+ /// argument list. They may be located anywhere. Values are obtained through `Default::default`.
510
+ #[ proc_macro_derive( FromVarargs , attributes( opt, skip) ) ]
497
511
pub fn derive_from_varargs ( input : TokenStream ) -> TokenStream {
498
512
let derive_input = syn:: parse_macro_input!( input as syn:: DeriveInput ) ;
499
513
match varargs:: derive_from_varargs ( derive_input) {
Original file line number Diff line number Diff line change @@ -48,7 +48,13 @@ pub(crate) fn derive_from_varargs(input: DeriveInput) -> Result<TokenStream2, sy
48
48
49
49
let mut required = Vec :: new ( ) ;
50
50
let mut optional = Vec :: new ( ) ;
51
+ let mut skipped = Vec :: new ( ) ;
51
52
for field in fields {
53
+ if field. attrs . iter ( ) . any ( |attr| attr. path . is_ident ( "skip" ) ) {
54
+ skipped. push ( field) ;
55
+ continue ;
56
+ }
57
+
52
58
let is_optional = field. attrs . iter ( ) . any ( |attr| attr. path . is_ident ( "opt" ) ) ;
53
59
if !is_optional && !optional. is_empty ( ) {
54
60
return Err ( syn:: Error :: new (
@@ -111,6 +117,16 @@ pub(crate) fn derive_from_varargs(input: DeriveInput) -> Result<TokenStream2, sy
111
117
. map ( |field| format ! ( "{}" , field. ty. to_token_stream( ) ) )
112
118
. collect :: < Vec < _ > > ( ) ;
113
119
120
+ let skipped_var_idents = skipped
121
+ . iter ( )
122
+ . enumerate ( )
123
+ . map ( |( n, field) | {
124
+ field. ident . clone ( ) . unwrap_or_else ( || {
125
+ Ident :: new ( & format ! ( "__skipped_arg_{}" , n) , Span :: call_site ( ) )
126
+ } )
127
+ } )
128
+ . collect :: < Vec < _ > > ( ) ;
129
+
114
130
Ok ( quote ! {
115
131
#derived
116
132
impl #generics :: gdnative:: export:: FromVarargs for #ident #generics #where_clause {
@@ -147,9 +163,14 @@ pub(crate) fn derive_from_varargs(input: DeriveInput) -> Result<TokenStream2, sy
147
163
let #req_var_idents = #req_var_idents. unwrap( ) ;
148
164
) *
149
165
166
+ #(
167
+ let #skipped_var_idents = core:: default :: Default :: default ( ) ;
168
+ ) *
169
+
150
170
std:: result:: Result :: Ok ( #ident {
151
171
#( #req_var_idents, ) *
152
172
#( #opt_var_idents, ) *
173
+ #( #skipped_var_idents, ) *
153
174
} )
154
175
}
155
176
}
You can’t perform that action at this time.
0 commit comments