@@ -17,6 +17,7 @@ pub fn derive_asn1_read(input: proc_macro::TokenStream) -> proc_macro::TokenStre
1717 all_field_types ( & input. data ) ,
1818 syn:: parse_quote!( asn1:: Asn1Readable <#lifetime_name>) ,
1919 syn:: parse_quote!( asn1:: Asn1DefinedByReadable <#lifetime_name, asn1:: ObjectIdentifier >) ,
20+ false ,
2021 ) ;
2122 let ( impl_generics, _, where_clause) = generics. split_for_impl ( ) ;
2223
@@ -65,6 +66,7 @@ pub fn derive_asn1_write(input: proc_macro::TokenStream) -> proc_macro::TokenStr
6566 all_field_types ( & input. data ) ,
6667 syn:: parse_quote!( asn1:: Asn1Writable ) ,
6768 syn:: parse_quote!( asn1:: Asn1DefinedByWritable <asn1:: ObjectIdentifier >) ,
69+ true ,
6870 ) ;
6971 let ( impl_generics, ty_generics, where_clause) = input. generics . split_for_impl ( ) ;
7072
@@ -93,7 +95,7 @@ pub fn derive_asn1_write(input: proc_macro::TokenStream) -> proc_macro::TokenStr
9395
9496 impl #impl_generics asn1:: Asn1Writable for & #name #ty_generics #where_clause {
9597 fn write( & self , w: & mut asn1:: Writer ) -> asn1:: WriteResult {
96- #name :: write ( self , w)
98+ ( * self ) . write ( w)
9799 }
98100 }
99101 }
@@ -273,48 +275,69 @@ fn add_lifetime_if_none(generics: &mut syn::Generics) -> syn::Lifetime {
273275 generics. lifetimes ( ) . next ( ) . unwrap ( ) . lifetime . clone ( )
274276}
275277
276- fn all_field_types ( data : & syn:: Data ) -> Vec < ( syn:: Type , bool ) > {
278+ fn all_field_types ( data : & syn:: Data ) -> Vec < ( syn:: Type , OpType , bool ) > {
277279 let mut field_types = vec ! [ ] ;
278280 match data {
279281 syn:: Data :: Struct ( v) => {
280- add_field_types ( & mut field_types, & v. fields ) ;
282+ add_field_types ( & mut field_types, & v. fields , None ) ;
281283 }
282284 syn:: Data :: Enum ( v) => {
283285 for variant in & v. variants {
284- add_field_types ( & mut field_types, & variant. fields ) ;
286+ let ( op_type, _) = extract_field_properties ( & variant. attrs ) ;
287+ add_field_types ( & mut field_types, & variant. fields , Some ( op_type) ) ;
285288 }
286289 }
287290 syn:: Data :: Union ( _) => panic ! ( "Unions not supported" ) ,
288291 }
289292 field_types
290293}
291294
292- fn add_field_types ( field_types : & mut Vec < ( syn:: Type , bool ) > , fields : & syn:: Fields ) {
295+ fn add_field_types (
296+ field_types : & mut Vec < ( syn:: Type , OpType , bool ) > ,
297+ fields : & syn:: Fields ,
298+ op_type : Option < OpType > ,
299+ ) {
293300 match fields {
294301 syn:: Fields :: Named ( v) => {
295302 for f in & v. named {
296- add_field_type ( field_types, f) ;
303+ add_field_type ( field_types, f, op_type . clone ( ) ) ;
297304 }
298305 }
299306 syn:: Fields :: Unnamed ( v) => {
300307 for f in & v. unnamed {
301- add_field_type ( field_types, f) ;
308+ add_field_type ( field_types, f, op_type . clone ( ) ) ;
302309 }
303310 }
304311 syn:: Fields :: Unit => { }
305312 }
306313}
307314
308- fn add_field_type ( field_types : & mut Vec < ( syn:: Type , bool ) > , f : & syn:: Field ) {
309- let ( op_type, _) = extract_field_properties ( & f. attrs ) ;
310- field_types. push ( ( f. ty . clone ( ) , matches ! ( op_type, OpType :: DefinedBy ( _) ) ) ) ;
315+ fn add_field_type (
316+ field_types : & mut Vec < ( syn:: Type , OpType , bool ) > ,
317+ f : & syn:: Field ,
318+ op_type : Option < OpType > ,
319+ ) {
320+ // If we have an op_type here, it means it came from an enum variant. In
321+ // that case, even though it wasn't marked "required", it is for the
322+ // purposes of how we're using it.
323+ let ( op_type, default) = if let Some ( OpType :: Explicit ( mut args) ) = op_type {
324+ args. required = true ;
325+ ( OpType :: Explicit ( args) , None )
326+ } else if let Some ( OpType :: Implicit ( mut args) ) = op_type {
327+ args. required = true ;
328+ ( OpType :: Implicit ( args) , None )
329+ } else {
330+ extract_field_properties ( & f. attrs )
331+ } ;
332+ field_types. push ( ( f. ty . clone ( ) , op_type, default. is_some ( ) ) ) ;
311333}
312334
313335fn add_bounds (
314336 generics : & mut syn:: Generics ,
315- field_types : Vec < ( syn:: Type , bool ) > ,
337+ field_types : Vec < ( syn:: Type , OpType , bool ) > ,
316338 bound : syn:: TypeParamBound ,
317339 defined_by_bound : syn:: TypeParamBound ,
340+ add_ref : bool ,
318341) {
319342 let where_clause = if field_types. is_empty ( ) {
320343 return ;
@@ -327,33 +350,74 @@ fn add_bounds(
327350 } )
328351 } ;
329352
330- for ( f, is_defined_by) in field_types {
353+ for ( f, op_type, has_default) in field_types {
354+ let ( bounded_ty, required_bound) = match ( op_type, add_ref) {
355+ ( OpType :: Regular , _) => ( f, bound. clone ( ) ) ,
356+ ( OpType :: DefinedBy ( _) , _) => ( f, defined_by_bound. clone ( ) ) ,
357+
358+ ( OpType :: Implicit ( OpTypeArgs { value, required } ) , false ) => {
359+ let ty = if required || has_default {
360+ syn:: parse_quote!( asn1:: Implicit :: <#f, #value>)
361+ } else {
362+ syn:: parse_quote!( asn1:: Implicit :: <<#f as asn1:: OptionExt >:: T , #value>)
363+ } ;
364+
365+ ( ty, bound. clone ( ) )
366+ }
367+ ( OpType :: Implicit ( OpTypeArgs { value, required } ) , true ) => {
368+ let ty = if required || has_default {
369+ syn:: parse_quote!( for <' asn1_internal> asn1:: Implicit :: <& ' asn1_internal #f, #value>)
370+ } else {
371+ syn:: parse_quote!( for <' asn1_internal> asn1:: Implicit :: <& ' asn1_internal <#f as asn1:: OptionExt >:: T , #value>)
372+ } ;
373+
374+ ( ty, bound. clone ( ) )
375+ }
376+
377+ ( OpType :: Explicit ( OpTypeArgs { value, required } ) , false ) => {
378+ let ty = if required || has_default {
379+ syn:: parse_quote!( asn1:: Explicit :: <#f, #value>)
380+ } else {
381+ syn:: parse_quote!( asn1:: Explicit :: <<#f as asn1:: OptionExt >:: T , #value>)
382+ } ;
383+
384+ ( ty, bound. clone ( ) )
385+ }
386+ ( OpType :: Explicit ( OpTypeArgs { value, required } ) , true ) => {
387+ let ty = if required || has_default {
388+ syn:: parse_quote!( for <' asn1_internal> asn1:: Explicit :: <& ' asn1_internal #f, #value>)
389+ } else {
390+ syn:: parse_quote!( for <' asn1_internal> asn1:: Explicit :: <& ' asn1_internal <#f as asn1:: OptionExt >:: T , #value>)
391+ } ;
392+
393+ ( ty, bound. clone ( ) )
394+ }
395+ } ;
396+
331397 where_clause
332398 . predicates
333399 . push ( syn:: WherePredicate :: Type ( syn:: PredicateType {
334400 lifetimes : None ,
335- bounded_ty : f ,
401+ bounded_ty,
336402 colon_token : Default :: default ( ) ,
337403 bounds : {
338404 let mut p = syn:: punctuated:: Punctuated :: new ( ) ;
339- if is_defined_by {
340- p. push ( defined_by_bound. clone ( ) ) ;
341- } else {
342- p. push ( bound. clone ( ) ) ;
343- }
405+ p. push ( required_bound) ;
344406 p
345407 } ,
346408 } ) )
347409 }
348410}
349411
412+ #[ derive( Clone ) ]
350413enum OpType {
351414 Regular ,
352415 Explicit ( OpTypeArgs ) ,
353416 Implicit ( OpTypeArgs ) ,
354417 DefinedBy ( syn:: Ident ) ,
355418}
356419
420+ #[ derive( Clone ) ]
357421struct OpTypeArgs {
358422 value : proc_macro2:: Literal ,
359423 required : bool ,
0 commit comments