@@ -138,6 +138,9 @@ enum PropAttr {
138
138
139
139
// ident = "literal"
140
140
Name ( syn:: LitStr ) ,
141
+
142
+ // ident
143
+ Default ,
141
144
}
142
145
143
146
impl Parse for PropAttr {
@@ -194,6 +197,7 @@ impl Parse for PropAttr {
194
197
) ,
195
198
) )
196
199
}
200
+ "default" => PropAttr :: Default ,
197
201
_ => PropAttr :: BuilderField ( ( name, None ) ) ,
198
202
}
199
203
} ;
@@ -213,6 +217,7 @@ struct ReceivedAttrs {
213
217
name : Option < syn:: LitStr > ,
214
218
builder : Option < ( Punctuated < syn:: Expr , Token ! [ , ] > , TokenStream2 ) > ,
215
219
builder_fields : HashMap < syn:: Ident , Option < syn:: Expr > > ,
220
+ use_default : bool ,
216
221
}
217
222
218
223
impl Parse for ReceivedAttrs {
@@ -244,6 +249,9 @@ impl ReceivedAttrs {
244
249
PropAttr :: BuilderField ( ( ident, expr) ) => {
245
250
self . builder_fields . insert ( ident, expr) ;
246
251
}
252
+ PropAttr :: Default => {
253
+ self . use_default = true ;
254
+ }
247
255
}
248
256
}
249
257
}
@@ -265,6 +273,7 @@ struct PropDesc {
265
273
builder : Option < ( Punctuated < syn:: Expr , Token ! [ , ] > , TokenStream2 ) > ,
266
274
builder_fields : HashMap < syn:: Ident , Option < syn:: Expr > > ,
267
275
is_construct_only : bool ,
276
+ use_default : bool ,
268
277
}
269
278
270
279
impl PropDesc {
@@ -286,6 +295,7 @@ impl PropDesc {
286
295
name,
287
296
builder,
288
297
builder_fields,
298
+ use_default,
289
299
} = attrs;
290
300
291
301
let is_construct_only = builder_fields. iter ( ) . any ( |( k, _) | * k == "construct_only" ) ;
@@ -333,6 +343,7 @@ impl PropDesc {
333
343
builder,
334
344
builder_fields,
335
345
is_construct_only,
346
+ use_default,
336
347
} )
337
348
}
338
349
fn is_overriding ( & self ) -> bool {
@@ -343,7 +354,11 @@ impl PropDesc {
343
354
fn expand_param_spec ( prop : & PropDesc ) -> TokenStream2 {
344
355
let crate_ident = crate_ident_new ( ) ;
345
356
let PropDesc {
346
- ty, name, builder, ..
357
+ ty,
358
+ name,
359
+ builder,
360
+ use_default,
361
+ ..
347
362
} = prop;
348
363
let stripped_name = strip_raw_prefix_from_name ( name) ;
349
364
@@ -385,9 +400,20 @@ fn expand_param_spec(prop: &PropDesc) -> TokenStream2 {
385
400
let builder_fields = prop. builder_fields . iter ( ) . map ( |( k, v) | quote ! ( . #k( #v) ) ) ;
386
401
387
402
let span = prop. attrs_span ;
403
+
404
+ // Figure out if we should use the default version or the one that explicitly sets the `Default` value.
405
+ let ( trait_name, fn_name) = if * use_default {
406
+ (
407
+ quote ! ( HasParamSpecDefaulted ) ,
408
+ quote ! ( param_spec_builder_defaulted) ,
409
+ )
410
+ } else {
411
+ ( quote ! ( HasParamSpec ) , quote ! ( param_spec_builder) )
412
+ } ;
413
+
388
414
quote_spanned ! { span=>
389
- <<#ty as #crate_ident:: property:: Property >:: Value as #crate_ident:: prelude :: HasParamSpec >
390
- :: param_spec_builder ( ) #builder_call
415
+ <<#ty as #crate_ident:: property:: Property >:: Value as #crate_ident:: #trait_name >
416
+ :: #fn_name ( ) #builder_call
391
417
#rw_flags
392
418
#( #builder_fields) *
393
419
. build( )
0 commit comments