@@ -17,7 +17,6 @@ pub fn derive_asn1_read(input: proc_macro::TokenStream) -> proc_macro::TokenStre
17
17
all_field_types ( & input. data , & input. generics ) ,
18
18
syn:: parse_quote!( asn1:: Asn1Readable <#lifetime_name>) ,
19
19
syn:: parse_quote!( asn1:: Asn1DefinedByReadable <#lifetime_name, asn1:: ObjectIdentifier >) ,
20
- false ,
21
20
) ;
22
21
let ( impl_generics, _, where_clause) = generics. split_for_impl ( ) ;
23
22
@@ -67,7 +66,6 @@ pub fn derive_asn1_write(input: proc_macro::TokenStream) -> proc_macro::TokenStr
67
66
fields,
68
67
syn:: parse_quote!( asn1:: Asn1Writable ) ,
69
68
syn:: parse_quote!( asn1:: Asn1DefinedByWritable <asn1:: ObjectIdentifier >) ,
70
- true ,
71
69
) ;
72
70
let ( impl_generics, ty_generics, where_clause) = input. generics . split_for_impl ( ) ;
73
71
@@ -402,7 +400,6 @@ fn add_bounds(
402
400
field_types : Vec < ( syn:: Type , OpType , bool ) > ,
403
401
bound : syn:: TypeParamBound ,
404
402
defined_by_bound : syn:: TypeParamBound ,
405
- add_ref : bool ,
406
403
) {
407
404
let where_clause = if field_types. is_empty ( ) {
408
405
return ;
@@ -416,11 +413,11 @@ fn add_bounds(
416
413
} ;
417
414
418
415
for ( f, op_type, has_default) in field_types {
419
- let ( bounded_ty, required_bound) = match ( op_type, add_ref ) {
420
- ( OpType :: Regular , _ ) => ( f, bound. clone ( ) ) ,
421
- ( OpType :: DefinedBy ( _ ) , _) => ( f, defined_by_bound. clone ( ) ) ,
416
+ let ( bounded_ty, required_bound) = match op_type {
417
+ OpType :: Regular => ( f, bound. clone ( ) ) ,
418
+ OpType :: DefinedBy ( _) => ( f, defined_by_bound. clone ( ) ) ,
422
419
423
- ( OpType :: Implicit ( OpTypeArgs { value, required } ) , false ) => {
420
+ OpType :: Implicit ( OpTypeArgs { value, required } ) => {
424
421
let ty = if required || has_default {
425
422
syn:: parse_quote!( asn1:: Implicit :: <#f, #value>)
426
423
} else {
@@ -429,32 +426,14 @@ fn add_bounds(
429
426
430
427
( ty, bound. clone ( ) )
431
428
}
432
- ( OpType :: Implicit ( OpTypeArgs { value, required } ) , true ) => {
433
- let ty = if required || has_default {
434
- syn:: parse_quote!( for <' asn1_internal> asn1:: Implicit :: <& ' asn1_internal #f, #value>)
435
- } else {
436
- syn:: parse_quote!( for <' asn1_internal> asn1:: Implicit :: <& ' asn1_internal <#f as asn1:: OptionExt >:: T , #value>)
437
- } ;
438
-
439
- ( ty, bound. clone ( ) )
440
- }
441
429
442
- ( OpType :: Explicit ( OpTypeArgs { value, required } ) , false ) => {
430
+ OpType :: Explicit ( OpTypeArgs { value, required } ) => {
443
431
let ty = if required || has_default {
444
432
syn:: parse_quote!( asn1:: Explicit :: <#f, #value>)
445
433
} else {
446
434
syn:: parse_quote!( asn1:: Explicit :: <<#f as asn1:: OptionExt >:: T , #value>)
447
435
} ;
448
436
449
- ( ty, bound. clone ( ) )
450
- }
451
- ( OpType :: Explicit ( OpTypeArgs { value, required } ) , true ) => {
452
- let ty = if required || has_default {
453
- syn:: parse_quote!( for <' asn1_internal> asn1:: Explicit :: <& ' asn1_internal #f, #value>)
454
- } else {
455
- syn:: parse_quote!( for <' asn1_internal> asn1:: Explicit :: <& ' asn1_internal <#f as asn1:: OptionExt >:: T , #value>)
456
- } ;
457
-
458
437
( ty, bound. clone ( ) )
459
438
}
460
439
} ;
@@ -750,8 +729,9 @@ fn generate_write_element(
750
729
) -> proc_macro2:: TokenStream {
751
730
let ( write_type, default) = extract_field_properties ( & f. attrs ) ;
752
731
732
+ let has_default = default. is_some ( ) ;
753
733
if let Some ( default) = default {
754
- field_read = quote:: quote! { & {
734
+ field_read = quote:: quote! { {
755
735
asn1:: to_optional_default( #field_read, & ( #default ) . into( ) )
756
736
} }
757
737
}
@@ -761,12 +741,12 @@ fn generate_write_element(
761
741
let value = arg. value ;
762
742
if arg. required {
763
743
quote:: quote_spanned! { f. span( ) =>
764
- w. write_element( & asn1:: Explicit :: <_, #value>:: new ( #field_read) ) ?;
744
+ w. write_element( asn1:: Explicit :: <_, #value>:: from_ref ( #field_read) ) ?;
765
745
}
766
746
} else {
767
747
quote:: quote_spanned! { f. span( ) =>
768
748
if let Some ( v) = #field_read {
769
- w. write_element( & asn1:: Explicit :: <_, #value>:: new ( v) ) ?;
749
+ w. write_element( asn1:: Explicit :: <_, #value>:: from_ref ( v) ) ?;
770
750
}
771
751
}
772
752
}
@@ -775,12 +755,12 @@ fn generate_write_element(
775
755
let value = arg. value ;
776
756
if arg. required {
777
757
quote:: quote_spanned! { f. span( ) =>
778
- w. write_element( & asn1:: Implicit :: <_, #value>:: new ( #field_read) ) ?;
758
+ w. write_element( asn1:: Implicit :: <_, #value>:: from_ref ( #field_read) ) ?;
779
759
}
780
760
} else {
781
761
quote:: quote_spanned! { f. span( ) =>
782
762
if let Some ( v) = #field_read {
783
- w. write_element( & asn1:: Implicit :: <_, #value>:: new ( v) ) ?;
763
+ w. write_element( asn1:: Implicit :: <_, #value>:: from_ref ( v) ) ?;
784
764
}
785
765
}
786
766
}
@@ -790,6 +770,12 @@ fn generate_write_element(
790
770
quote:: quote! {
791
771
w. write_element( asn1:: writable_defined_by_item( #defined_by_marker_read) ) ?;
792
772
}
773
+ } else if has_default {
774
+ quote:: quote! {
775
+ if let Some ( v) = #field_read {
776
+ w. write_element( v) ?;
777
+ }
778
+ }
793
779
} else {
794
780
quote:: quote! {
795
781
w. write_element( #field_read) ?;
@@ -870,13 +856,13 @@ fn generate_enum_write_block(name: &syn::Ident, data: &syn::DataEnum) -> proc_ma
870
856
OpType :: Explicit ( arg) => {
871
857
let tag = arg. value ;
872
858
quote:: quote! {
873
- #name:: #ident( value) => w. write_element( & asn1:: Explicit :: <_, #tag>:: new ( value) ) ,
859
+ #name:: #ident( value) => w. write_element( asn1:: Explicit :: <_, #tag>:: from_ref ( value) ) ,
874
860
}
875
861
}
876
862
OpType :: Implicit ( arg) => {
877
863
let tag = arg. value ;
878
864
quote:: quote! {
879
- #name:: #ident( value) => w. write_element( & asn1:: Implicit :: <_, #tag>:: new ( value) ) ,
865
+ #name:: #ident( value) => w. write_element( asn1:: Implicit :: <_, #tag>:: from_ref ( value) ) ,
880
866
}
881
867
}
882
868
OpType :: DefinedBy ( _) => panic ! ( "Can't use #[defined_by] in an Asn1Write on an enum" ) ,
0 commit comments