@@ -14,8 +14,8 @@ use proc_macro::TokenStream;
14
14
use proc_macro2:: { Span , TokenStream as TokenStream2 } ;
15
15
use quote:: quote;
16
16
use 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 ,
19
19
} ;
20
20
21
21
/// Derive the [`Signer`] trait for a type which impls [`DigestSigner`].
@@ -51,12 +51,13 @@ fn emit_signer_impl(input: DeriveInput) -> TokenStream2 {
51
51
) ;
52
52
53
53
let name = params. name ;
54
+ let lifetimes = params. lifetimes ;
54
55
let impl_generics = params. impl_generics ;
55
56
let ty_generics = params. ty_generics ;
56
57
let where_clause = params. where_clause ;
57
58
58
59
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) , * >
60
61
#where_clause
61
62
{
62
63
fn try_sign( & self , msg: & [ u8 ] ) -> :: signature:: Result <#s_ident> {
@@ -99,12 +100,13 @@ fn emit_verifier_impl(input: DeriveInput) -> TokenStream2 {
99
100
) ;
100
101
101
102
let name = params. name ;
103
+ let lifetimes = params. lifetimes ;
102
104
let impl_generics = params. impl_generics ;
103
105
let ty_generics = params. ty_generics ;
104
106
let where_clause = params. where_clause ;
105
107
106
108
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) , * >
108
110
#where_clause
109
111
{
110
112
fn verify( & self , msg: & [ u8 ] , signature: & #s_ident) -> :: signature:: Result <( ) > {
@@ -137,12 +139,13 @@ fn emit_digest_signer_impl(input: DeriveInput) -> TokenStream2 {
137
139
) ;
138
140
139
141
let name = params. name ;
142
+ let lifetimes = params. lifetimes ;
140
143
let impl_generics = params. impl_generics ;
141
144
let ty_generics = params. ty_generics ;
142
145
let where_clause = params. where_clause ;
143
146
144
147
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) , * >
146
149
#where_clause
147
150
{
148
151
fn try_sign_digest( & self , digest: #d_ident) -> :: signature:: Result <#s_ident> {
@@ -175,12 +178,13 @@ fn emit_digest_verifier_impl(input: DeriveInput) -> TokenStream2 {
175
178
) ;
176
179
177
180
let name = params. name ;
181
+ let lifetimes = params. lifetimes ;
178
182
let impl_generics = params. impl_generics ;
179
183
let ty_generics = params. ty_generics ;
180
184
let where_clause = params. where_clause ;
181
185
182
186
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) , * >
184
188
#where_clause
185
189
{
186
190
fn verify_digest( & self , digest: #d_ident, signature: & #s_ident) -> :: signature:: Result <( ) > {
@@ -195,6 +199,8 @@ struct DeriveParams {
195
199
/// Name of the struct the trait impls are being added to.
196
200
name : Ident ,
197
201
202
+ lifetimes : Vec < Lifetime > ,
203
+
198
204
/// Generic parameters of `impl`.
199
205
impl_generics : Vec < TypeParam > ,
200
206
@@ -210,6 +216,12 @@ impl DeriveParams {
210
216
fn new ( input : DeriveInput ) -> Self {
211
217
let impl_generics = input. generics . type_params ( ) . cloned ( ) . collect ( ) ;
212
218
219
+ let lifetimes = input
220
+ . generics
221
+ . lifetimes ( )
222
+ . map ( |lt| lt. lifetime . clone ( ) )
223
+ . collect ( ) ;
224
+
213
225
let ty_generics = input
214
226
. generics
215
227
. type_params ( )
@@ -227,6 +239,7 @@ impl DeriveParams {
227
239
228
240
Self {
229
241
name : input. ident ,
242
+ lifetimes,
230
243
impl_generics,
231
244
ty_generics,
232
245
where_clause,
@@ -291,7 +304,7 @@ mod tests {
291
304
assert_eq ! (
292
305
output. to_string( ) ,
293
306
quote! {
294
- impl <C , __S> :: signature:: Signer <__S> for MySigner <C >
307
+ impl <, C , __S> :: signature:: Signer <__S> for MySigner <, C >
295
308
where
296
309
C : EllipticCurve ,
297
310
__S: :: signature:: PrehashSignature ,
@@ -320,7 +333,7 @@ mod tests {
320
333
assert_eq ! (
321
334
output. to_string( ) ,
322
335
quote! {
323
- impl <C : EllipticCurve , __S> :: signature:: Verifier <__S> for MyVerifier <C >
336
+ impl <, C : EllipticCurve , __S> :: signature:: Verifier <__S> for MyVerifier <, C >
324
337
where
325
338
__S: :: signature:: PrehashSignature ,
326
339
Self : :: signature:: DigestVerifier <__S:: Digest , __S>
@@ -348,7 +361,7 @@ mod tests {
348
361
assert_eq ! (
349
362
output. to_string( ) ,
350
363
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 >
352
365
where
353
366
__D: :: signature:: digest:: Digest ,
354
367
Self : :: signature:: hazmat:: PrehashSigner <__S>
@@ -376,7 +389,7 @@ mod tests {
376
389
assert_eq ! (
377
390
output. to_string( ) ,
378
391
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 >
380
393
where
381
394
__D: :: signature:: digest:: Digest ,
382
395
Self : :: signature:: hazmat:: PrehashVerifier <__S>
@@ -389,4 +402,37 @@ mod tests {
389
402
. to_string( )
390
403
) ;
391
404
}
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
+ }
392
438
}
0 commit comments