@@ -17,7 +17,6 @@ pub fn derive_asn1_read(input: proc_macro::TokenStream) -> proc_macro::TokenStre
1717 all_field_types ( & input. data , false , & input. generics ) ,
1818 syn:: parse_quote!( asn1:: Asn1Readable <#lifetime_name>) ,
1919 syn:: parse_quote!( asn1:: Asn1DefinedByReadable <#lifetime_name, asn1:: ObjectIdentifier >) ,
20- false ,
2120 ) ;
2221 let ( impl_generics, _, where_clause) = generics. split_for_impl ( ) ;
2322
@@ -67,7 +66,6 @@ pub fn derive_asn1_write(input: proc_macro::TokenStream) -> proc_macro::TokenStr
6766 fields,
6867 syn:: parse_quote!( asn1:: Asn1Writable ) ,
6968 syn:: parse_quote!( asn1:: Asn1DefinedByWritable <asn1:: ObjectIdentifier >) ,
70- true ,
7169 ) ;
7270 let ( impl_generics, ty_generics, where_clause) = input. generics . split_for_impl ( ) ;
7371
@@ -454,7 +452,6 @@ fn add_bounds(
454452 field_types : Vec < ( syn:: Type , OpType , bool ) > ,
455453 bound : syn:: TypeParamBound ,
456454 defined_by_bound : syn:: TypeParamBound ,
457- add_ref : bool ,
458455) {
459456 let where_clause = if field_types. is_empty ( ) {
460457 return ;
@@ -468,11 +465,11 @@ fn add_bounds(
468465 } ;
469466
470467 for ( f, op_type, has_default) in field_types {
471- let ( bounded_ty, required_bound) = match ( op_type, add_ref ) {
472- ( OpType :: Regular , _ ) => ( f, bound. clone ( ) ) ,
473- ( OpType :: DefinedBy ( _ ) , _) => ( f, defined_by_bound. clone ( ) ) ,
468+ let ( bounded_ty, required_bound) = match op_type {
469+ OpType :: Regular => ( f, bound. clone ( ) ) ,
470+ OpType :: DefinedBy ( _) => ( f, defined_by_bound. clone ( ) ) ,
474471
475- ( OpType :: Implicit ( OpTypeArgs { value, required } ) , false ) => {
472+ OpType :: Implicit ( OpTypeArgs { value, required } ) => {
476473 let ty = if required || has_default {
477474 syn:: parse_quote!( asn1:: Implicit :: <#f, #value>)
478475 } else {
@@ -481,32 +478,14 @@ fn add_bounds(
481478
482479 ( ty, bound. clone ( ) )
483480 }
484- ( OpType :: Implicit ( OpTypeArgs { value, required } ) , true ) => {
485- let ty = if required || has_default {
486- syn:: parse_quote!( for <' asn1_internal> asn1:: Implicit :: <& ' asn1_internal #f, #value>)
487- } else {
488- syn:: parse_quote!( for <' asn1_internal> asn1:: Implicit :: <& ' asn1_internal <#f as asn1:: OptionExt >:: T , #value>)
489- } ;
490481
491- ( ty, bound. clone ( ) )
492- }
493-
494- ( OpType :: Explicit ( OpTypeArgs { value, required } ) , false ) => {
482+ OpType :: Explicit ( OpTypeArgs { value, required } ) => {
495483 let ty = if required || has_default {
496484 syn:: parse_quote!( asn1:: Explicit :: <#f, #value>)
497485 } else {
498486 syn:: parse_quote!( asn1:: Explicit :: <<#f as asn1:: OptionExt >:: T , #value>)
499487 } ;
500488
501- ( ty, bound. clone ( ) )
502- }
503- ( OpType :: Explicit ( OpTypeArgs { value, required } ) , true ) => {
504- let ty = if required || has_default {
505- syn:: parse_quote!( for <' asn1_internal> asn1:: Explicit :: <& ' asn1_internal #f, #value>)
506- } else {
507- syn:: parse_quote!( for <' asn1_internal> asn1:: Explicit :: <& ' asn1_internal <#f as asn1:: OptionExt >:: T , #value>)
508- } ;
509-
510489 ( ty, bound. clone ( ) )
511490 }
512491 } ;
@@ -814,8 +793,9 @@ fn generate_write_element(
814793) -> proc_macro2:: TokenStream {
815794 let ( write_type, default) = extract_field_properties ( & f. attrs ) ;
816795
796+ let has_default = default. is_some ( ) ;
817797 if let Some ( default) = default {
818- field_read = quote:: quote! { & {
798+ field_read = quote:: quote! { {
819799 asn1:: to_optional_default( #field_read, & ( #default ) . into( ) )
820800 } }
821801 }
@@ -825,12 +805,12 @@ fn generate_write_element(
825805 let value = arg. value ;
826806 if arg. required {
827807 quote:: quote_spanned! { f. span( ) =>
828- w. write_element( & asn1:: Explicit :: <_, #value>:: new ( #field_read) ) ?;
808+ w. write_element( asn1:: Explicit :: <_, #value>:: from_ref ( #field_read) ) ?;
829809 }
830810 } else {
831811 quote:: quote_spanned! { f. span( ) =>
832812 if let Some ( v) = #field_read {
833- w. write_element( & asn1:: Explicit :: <_, #value>:: new ( v) ) ?;
813+ w. write_element( asn1:: Explicit :: <_, #value>:: from_ref ( v) ) ?;
834814 }
835815 }
836816 }
@@ -839,12 +819,12 @@ fn generate_write_element(
839819 let value = arg. value ;
840820 if arg. required {
841821 quote:: quote_spanned! { f. span( ) =>
842- w. write_element( & asn1:: Implicit :: <_, #value>:: new ( #field_read) ) ?;
822+ w. write_element( asn1:: Implicit :: <_, #value>:: from_ref ( #field_read) ) ?;
843823 }
844824 } else {
845825 quote:: quote_spanned! { f. span( ) =>
846826 if let Some ( v) = #field_read {
847- w. write_element( & asn1:: Implicit :: <_, #value>:: new ( v) ) ?;
827+ w. write_element( asn1:: Implicit :: <_, #value>:: from_ref ( v) ) ?;
848828 }
849829 }
850830 }
@@ -854,6 +834,12 @@ fn generate_write_element(
854834 quote:: quote! {
855835 w. write_element( asn1:: writable_defined_by_item( #defined_by_marker_read) ) ?;
856836 }
837+ } else if has_default {
838+ quote:: quote! {
839+ if let Some ( v) = #field_read {
840+ w. write_element( v) ?;
841+ }
842+ }
857843 } else {
858844 quote:: quote! {
859845 w. write_element( #field_read) ?;
@@ -934,13 +920,13 @@ fn generate_enum_write_block(name: &syn::Ident, data: &syn::DataEnum) -> proc_ma
934920 OpType :: Explicit ( arg) => {
935921 let tag = arg. value ;
936922 quote:: quote! {
937- #name:: #ident( value) => w. write_element( & asn1:: Explicit :: <_, #tag>:: new ( value) ) ,
923+ #name:: #ident( value) => w. write_element( asn1:: Explicit :: <_, #tag>:: from_ref ( value) ) ,
938924 }
939925 }
940926 OpType :: Implicit ( arg) => {
941927 let tag = arg. value ;
942928 quote:: quote! {
943- #name:: #ident( value) => w. write_element( & asn1:: Implicit :: <_, #tag>:: new ( value) ) ,
929+ #name:: #ident( value) => w. write_element( asn1:: Implicit :: <_, #tag>:: from_ref ( value) ) ,
944930 }
945931 }
946932 OpType :: DefinedBy ( _) => panic ! ( "Can't use #[defined_by] in an Asn1Write on an enum" ) ,
0 commit comments