@@ -17,7 +17,6 @@ pub fn derive_asn1_read(input: proc_macro::TokenStream) -> proc_macro::TokenStre
1717 all_field_types ( & input. data , & 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
@@ -402,7 +400,6 @@ fn add_bounds(
402400 field_types : Vec < ( syn:: Type , OpType , bool ) > ,
403401 bound : syn:: TypeParamBound ,
404402 defined_by_bound : syn:: TypeParamBound ,
405- add_ref : bool ,
406403) {
407404 let where_clause = if field_types. is_empty ( ) {
408405 return ;
@@ -416,11 +413,11 @@ fn add_bounds(
416413 } ;
417414
418415 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 ( ) ) ,
422419
423- ( OpType :: Implicit ( OpTypeArgs { value, required } ) , false ) => {
420+ OpType :: Implicit ( OpTypeArgs { value, required } ) => {
424421 let ty = if required || has_default {
425422 syn:: parse_quote!( asn1:: Implicit :: <#f, #value>)
426423 } else {
@@ -429,32 +426,14 @@ fn add_bounds(
429426
430427 ( ty, bound. clone ( ) )
431428 }
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- }
441429
442- ( OpType :: Explicit ( OpTypeArgs { value, required } ) , false ) => {
430+ OpType :: Explicit ( OpTypeArgs { value, required } ) => {
443431 let ty = if required || has_default {
444432 syn:: parse_quote!( asn1:: Explicit :: <#f, #value>)
445433 } else {
446434 syn:: parse_quote!( asn1:: Explicit :: <<#f as asn1:: OptionExt >:: T , #value>)
447435 } ;
448436
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-
458437 ( ty, bound. clone ( ) )
459438 }
460439 } ;
@@ -750,8 +729,9 @@ fn generate_write_element(
750729) -> proc_macro2:: TokenStream {
751730 let ( write_type, default) = extract_field_properties ( & f. attrs ) ;
752731
732+ let has_default = default. is_some ( ) ;
753733 if let Some ( default) = default {
754- field_read = quote:: quote! { & {
734+ field_read = quote:: quote! { {
755735 asn1:: to_optional_default( #field_read, & ( #default ) . into( ) )
756736 } }
757737 }
@@ -761,12 +741,12 @@ fn generate_write_element(
761741 let value = arg. value ;
762742 if arg. required {
763743 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) ) ?;
765745 }
766746 } else {
767747 quote:: quote_spanned! { f. span( ) =>
768748 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) ) ?;
770750 }
771751 }
772752 }
@@ -775,12 +755,12 @@ fn generate_write_element(
775755 let value = arg. value ;
776756 if arg. required {
777757 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) ) ?;
779759 }
780760 } else {
781761 quote:: quote_spanned! { f. span( ) =>
782762 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) ) ?;
784764 }
785765 }
786766 }
@@ -790,6 +770,12 @@ fn generate_write_element(
790770 quote:: quote! {
791771 w. write_element( asn1:: writable_defined_by_item( #defined_by_marker_read) ) ?;
792772 }
773+ } else if has_default {
774+ quote:: quote! {
775+ if let Some ( v) = #field_read {
776+ w. write_element( v) ?;
777+ }
778+ }
793779 } else {
794780 quote:: quote! {
795781 w. write_element( #field_read) ?;
@@ -870,13 +856,13 @@ fn generate_enum_write_block(name: &syn::Ident, data: &syn::DataEnum) -> proc_ma
870856 OpType :: Explicit ( arg) => {
871857 let tag = arg. value ;
872858 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) ) ,
874860 }
875861 }
876862 OpType :: Implicit ( arg) => {
877863 let tag = arg. value ;
878864 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) ) ,
880866 }
881867 }
882868 OpType :: DefinedBy ( _) => panic ! ( "Can't use #[defined_by] in an Asn1Write on an enum" ) ,
0 commit comments