@@ -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
@@ -154,7 +152,6 @@ pub fn derive_asn1_defined_by_read(input: proc_macro::TokenStream) -> proc_macro
154152 all_field_types ( & input. data , true , & input. generics ) ,
155153 syn:: parse_quote!( asn1:: Asn1Readable <#lifetime_name>) ,
156154 syn:: parse_quote!( asn1:: Asn1DefinedByReadable <#lifetime_name, asn1:: ObjectIdentifier >) ,
157- false ,
158155 ) ;
159156 let ( impl_generics, _, where_clause) = generics. split_for_impl ( ) ;
160157
@@ -220,7 +217,6 @@ pub fn derive_asn1_defined_by_write(input: proc_macro::TokenStream) -> proc_macr
220217 fields,
221218 syn:: parse_quote!( asn1:: Asn1Writable ) ,
222219 syn:: parse_quote!( asn1:: Asn1DefinedByWritable <asn1:: ObjectIdentifier >) ,
223- true ,
224220 ) ;
225221 let ( impl_generics, ty_generics, where_clause) = input. generics . split_for_impl ( ) ;
226222
@@ -454,7 +450,6 @@ fn add_bounds(
454450 field_types : Vec < ( syn:: Type , OpType , bool ) > ,
455451 bound : syn:: TypeParamBound ,
456452 defined_by_bound : syn:: TypeParamBound ,
457- add_ref : bool ,
458453) {
459454 let where_clause = if field_types. is_empty ( ) {
460455 return ;
@@ -468,11 +463,11 @@ fn add_bounds(
468463 } ;
469464
470465 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 ( ) ) ,
466+ let ( bounded_ty, required_bound) = match op_type {
467+ OpType :: Regular => ( f, bound. clone ( ) ) ,
468+ OpType :: DefinedBy ( _) => ( f, defined_by_bound. clone ( ) ) ,
474469
475- ( OpType :: Implicit ( OpTypeArgs { value, required } ) , false ) => {
470+ OpType :: Implicit ( OpTypeArgs { value, required } ) => {
476471 let ty = if required || has_default {
477472 syn:: parse_quote!( asn1:: Implicit :: <#f, #value>)
478473 } else {
@@ -481,32 +476,14 @@ fn add_bounds(
481476
482477 ( ty, bound. clone ( ) )
483478 }
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- } ;
490-
491- ( ty, bound. clone ( ) )
492- }
493479
494- ( OpType :: Explicit ( OpTypeArgs { value, required } ) , false ) => {
480+ OpType :: Explicit ( OpTypeArgs { value, required } ) => {
495481 let ty = if required || has_default {
496482 syn:: parse_quote!( asn1:: Explicit :: <#f, #value>)
497483 } else {
498484 syn:: parse_quote!( asn1:: Explicit :: <<#f as asn1:: OptionExt >:: T , #value>)
499485 } ;
500486
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-
510487 ( ty, bound. clone ( ) )
511488 }
512489 } ;
@@ -814,8 +791,9 @@ fn generate_write_element(
814791) -> proc_macro2:: TokenStream {
815792 let ( write_type, default) = extract_field_properties ( & f. attrs ) ;
816793
794+ let has_default = default. is_some ( ) ;
817795 if let Some ( default) = default {
818- field_read = quote:: quote! { & {
796+ field_read = quote:: quote! { {
819797 asn1:: to_optional_default( #field_read, & ( #default ) . into( ) )
820798 } }
821799 }
@@ -825,12 +803,12 @@ fn generate_write_element(
825803 let value = arg. value ;
826804 if arg. required {
827805 quote:: quote_spanned! { f. span( ) =>
828- w. write_element( & asn1:: Explicit :: <_, #value>:: new ( #field_read) ) ?;
806+ w. write_element( asn1:: Explicit :: <_, #value>:: from_ref ( #field_read) ) ?;
829807 }
830808 } else {
831809 quote:: quote_spanned! { f. span( ) =>
832810 if let Some ( v) = #field_read {
833- w. write_element( & asn1:: Explicit :: <_, #value>:: new ( v) ) ?;
811+ w. write_element( asn1:: Explicit :: <_, #value>:: from_ref ( v) ) ?;
834812 }
835813 }
836814 }
@@ -839,12 +817,12 @@ fn generate_write_element(
839817 let value = arg. value ;
840818 if arg. required {
841819 quote:: quote_spanned! { f. span( ) =>
842- w. write_element( & asn1:: Implicit :: <_, #value>:: new ( #field_read) ) ?;
820+ w. write_element( asn1:: Implicit :: <_, #value>:: from_ref ( #field_read) ) ?;
843821 }
844822 } else {
845823 quote:: quote_spanned! { f. span( ) =>
846824 if let Some ( v) = #field_read {
847- w. write_element( & asn1:: Implicit :: <_, #value>:: new ( v) ) ?;
825+ w. write_element( asn1:: Implicit :: <_, #value>:: from_ref ( v) ) ?;
848826 }
849827 }
850828 }
@@ -854,6 +832,12 @@ fn generate_write_element(
854832 quote:: quote! {
855833 w. write_element( asn1:: writable_defined_by_item( #defined_by_marker_read) ) ?;
856834 }
835+ } else if has_default {
836+ quote:: quote! {
837+ if let Some ( v) = #field_read {
838+ w. write_element( v) ?;
839+ }
840+ }
857841 } else {
858842 quote:: quote! {
859843 w. write_element( #field_read) ?;
@@ -934,13 +918,13 @@ fn generate_enum_write_block(name: &syn::Ident, data: &syn::DataEnum) -> proc_ma
934918 OpType :: Explicit ( arg) => {
935919 let tag = arg. value ;
936920 quote:: quote! {
937- #name:: #ident( value) => w. write_element( & asn1:: Explicit :: <_, #tag>:: new ( value) ) ,
921+ #name:: #ident( value) => w. write_element( asn1:: Explicit :: <_, #tag>:: from_ref ( value) ) ,
938922 }
939923 }
940924 OpType :: Implicit ( arg) => {
941925 let tag = arg. value ;
942926 quote:: quote! {
943- #name:: #ident( value) => w. write_element( & asn1:: Implicit :: <_, #tag>:: new ( value) ) ,
927+ #name:: #ident( value) => w. write_element( asn1:: Implicit :: <_, #tag>:: from_ref ( value) ) ,
944928 }
945929 }
946930 OpType :: DefinedBy ( _) => panic ! ( "Can't use #[defined_by] in an Asn1Write on an enum" ) ,
0 commit comments