@@ -14,8 +14,8 @@ use proc_macro::TokenStream;
1414use proc_macro2:: { Span , TokenStream as TokenStream2 } ;
1515use quote:: quote;
1616use syn:: {
17- parse_macro_input, parse_quote, punctuated:: Punctuated , DeriveInput , Ident , PredicateType ,
18- Token , TraitBound , Type , TypeParam , TypeParamBound , WhereClause , WherePredicate ,
17+ parse_macro_input, parse_quote, punctuated:: Punctuated , DeriveInput , Ident , Lifetime ,
18+ PredicateType , Token , TraitBound , Type , TypeParam , TypeParamBound , WhereClause , WherePredicate ,
1919} ;
2020
2121/// Derive the [`Signer`] trait for a type which impls [`DigestSigner`].
@@ -51,12 +51,13 @@ fn emit_signer_impl(input: DeriveInput) -> TokenStream2 {
5151 ) ;
5252
5353 let name = params. name ;
54+ let lifetimes = params. lifetimes ;
5455 let impl_generics = params. impl_generics ;
5556 let ty_generics = params. ty_generics ;
5657 let where_clause = params. where_clause ;
5758
5859 quote ! {
59- impl <#( #impl_generics) , * > :: signature:: Signer <#s_ident> for #name<#( #ty_generics) , * >
60+ impl <#( #lifetimes ) , * , # ( # impl_generics) , * > :: signature:: Signer <#s_ident> for #name<# ( #lifetimes ) , * , #( #ty_generics) , * >
6061 #where_clause
6162 {
6263 fn try_sign( & self , msg: & [ u8 ] ) -> :: signature:: Result <#s_ident> {
@@ -99,12 +100,13 @@ fn emit_verifier_impl(input: DeriveInput) -> TokenStream2 {
99100 ) ;
100101
101102 let name = params. name ;
103+ let lifetimes = params. lifetimes ;
102104 let impl_generics = params. impl_generics ;
103105 let ty_generics = params. ty_generics ;
104106 let where_clause = params. where_clause ;
105107
106108 quote ! {
107- impl <#( #impl_generics) , * > :: signature:: Verifier <#s_ident> for #name<#( #ty_generics) , * >
109+ impl <#( #lifetimes ) , * , # ( # impl_generics) , * > :: signature:: Verifier <#s_ident> for #name<# ( #lifetimes ) , * , #( #ty_generics) , * >
108110 #where_clause
109111 {
110112 fn verify( & self , msg: & [ u8 ] , signature: & #s_ident) -> :: signature:: Result <( ) > {
@@ -137,12 +139,13 @@ fn emit_digest_signer_impl(input: DeriveInput) -> TokenStream2 {
137139 ) ;
138140
139141 let name = params. name ;
142+ let lifetimes = params. lifetimes ;
140143 let impl_generics = params. impl_generics ;
141144 let ty_generics = params. ty_generics ;
142145 let where_clause = params. where_clause ;
143146
144147 quote ! {
145- impl <#( #impl_generics) , * > :: signature:: DigestSigner <#d_ident, #s_ident> for #name<#( #ty_generics) , * >
148+ impl <#( #lifetimes ) , * , # ( # impl_generics) , * > :: signature:: DigestSigner <#d_ident, #s_ident> for #name<# ( #lifetimes ) , * , #( #ty_generics) , * >
146149 #where_clause
147150 {
148151 fn try_sign_digest( & self , digest: #d_ident) -> :: signature:: Result <#s_ident> {
@@ -175,12 +178,13 @@ fn emit_digest_verifier_impl(input: DeriveInput) -> TokenStream2 {
175178 ) ;
176179
177180 let name = params. name ;
181+ let lifetimes = params. lifetimes ;
178182 let impl_generics = params. impl_generics ;
179183 let ty_generics = params. ty_generics ;
180184 let where_clause = params. where_clause ;
181185
182186 quote ! {
183- impl <#( #impl_generics) , * > :: signature:: DigestVerifier <#d_ident, #s_ident> for #name<#( #ty_generics) , * >
187+ impl <#( #lifetimes ) , * , # ( # impl_generics) , * > :: signature:: DigestVerifier <#d_ident, #s_ident> for #name<# ( #lifetimes ) , * , #( #ty_generics) , * >
184188 #where_clause
185189 {
186190 fn verify_digest( & self , digest: #d_ident, signature: & #s_ident) -> :: signature:: Result <( ) > {
@@ -195,6 +199,8 @@ struct DeriveParams {
195199 /// Name of the struct the trait impls are being added to.
196200 name : Ident ,
197201
202+ lifetimes : Vec < Lifetime > ,
203+
198204 /// Generic parameters of `impl`.
199205 impl_generics : Vec < TypeParam > ,
200206
@@ -210,6 +216,12 @@ impl DeriveParams {
210216 fn new ( input : DeriveInput ) -> Self {
211217 let impl_generics = input. generics . type_params ( ) . cloned ( ) . collect ( ) ;
212218
219+ let lifetimes = input
220+ . generics
221+ . lifetimes ( )
222+ . map ( |lt| lt. lifetime . clone ( ) )
223+ . collect ( ) ;
224+
213225 let ty_generics = input
214226 . generics
215227 . type_params ( )
@@ -227,6 +239,7 @@ impl DeriveParams {
227239
228240 Self {
229241 name : input. ident ,
242+ lifetimes,
230243 impl_generics,
231244 ty_generics,
232245 where_clause,
@@ -291,7 +304,7 @@ mod tests {
291304 assert_eq ! (
292305 output. to_string( ) ,
293306 quote! {
294- impl <C , __S> :: signature:: Signer <__S> for MySigner <C >
307+ impl <, C , __S> :: signature:: Signer <__S> for MySigner <, C >
295308 where
296309 C : EllipticCurve ,
297310 __S: :: signature:: PrehashSignature ,
@@ -320,7 +333,7 @@ mod tests {
320333 assert_eq ! (
321334 output. to_string( ) ,
322335 quote! {
323- impl <C : EllipticCurve , __S> :: signature:: Verifier <__S> for MyVerifier <C >
336+ impl <, C : EllipticCurve , __S> :: signature:: Verifier <__S> for MyVerifier <, C >
324337 where
325338 __S: :: signature:: PrehashSignature ,
326339 Self : :: signature:: DigestVerifier <__S:: Digest , __S>
@@ -348,7 +361,7 @@ mod tests {
348361 assert_eq ! (
349362 output. to_string( ) ,
350363 quote! {
351- impl <C : EllipticCurve , __D, __S> :: signature:: DigestSigner <__D, __S> for MySigner <C >
364+ impl <, C : EllipticCurve , __D, __S> :: signature:: DigestSigner <__D, __S> for MySigner <, C >
352365 where
353366 __D: :: signature:: digest:: Digest ,
354367 Self : :: signature:: hazmat:: PrehashSigner <__S>
@@ -376,7 +389,7 @@ mod tests {
376389 assert_eq ! (
377390 output. to_string( ) ,
378391 quote! {
379- impl <C : EllipticCurve , __D, __S> :: signature:: DigestVerifier <__D, __S> for MyVerifier <C >
392+ impl <, C : EllipticCurve , __D, __S> :: signature:: DigestVerifier <__D, __S> for MyVerifier <, C >
380393 where
381394 __D: :: signature:: digest:: Digest ,
382395 Self : :: signature:: hazmat:: PrehashVerifier <__S>
@@ -389,4 +402,37 @@ mod tests {
389402 . to_string( )
390403 ) ;
391404 }
405+
406+ #[ test]
407+ fn signer_lifetimes ( ) {
408+ let input = parse_quote ! {
409+ #[ derive( Signer ) ]
410+ struct MySigner <' a, C >
411+ where
412+ C : EllipticCurve
413+ {
414+ scalar: Scalar <C :: ScalarSize >,
415+ _lifetime: & ' a ( ) ,
416+ }
417+ } ;
418+
419+ let output = emit_signer_impl ( input) ;
420+
421+ assert_eq ! (
422+ output. to_string( ) ,
423+ quote! {
424+ impl <' a, C , __S> :: signature:: Signer <__S> for MySigner <' a, C >
425+ where
426+ C : EllipticCurve ,
427+ __S: :: signature:: PrehashSignature ,
428+ Self : :: signature:: DigestSigner <__S:: Digest , __S>
429+ {
430+ fn try_sign( & self , msg: & [ u8 ] ) -> :: signature:: Result <__S> {
431+ self . try_sign_digest( __S:: Digest :: new_with_prefix( msg) )
432+ }
433+ }
434+ }
435+ . to_string( )
436+ ) ;
437+ }
392438}
0 commit comments