@@ -8,15 +8,15 @@ use field::SequenceField;
8
8
use proc_macro2:: TokenStream ;
9
9
use proc_macro_error:: abort;
10
10
use quote:: quote;
11
- use syn:: { DeriveInput , Ident , Lifetime } ;
11
+ use syn:: { DeriveInput , GenericParam , Generics , Ident , Lifetime , LifetimeParam } ;
12
12
13
13
/// Derive the `Sequence` trait for a struct
14
14
pub ( crate ) struct DeriveSequence {
15
15
/// Name of the sequence struct.
16
16
ident : Ident ,
17
17
18
- /// Lifetime of the struct.
19
- lifetime : Option < Lifetime > ,
18
+ /// Generics of the struct.
19
+ generics : Generics ,
20
20
21
21
/// Fields of the struct.
22
22
fields : Vec < SequenceField > ,
@@ -33,13 +33,6 @@ impl DeriveSequence {
33
33
) ,
34
34
} ;
35
35
36
- // TODO(tarcieri): properly handle multiple lifetimes
37
- let lifetime = input
38
- . generics
39
- . lifetimes ( )
40
- . next ( )
41
- . map ( |lt| lt. lifetime . clone ( ) ) ;
42
-
43
36
let type_attrs = TypeAttrs :: parse ( & input. attrs ) ;
44
37
45
38
let fields = data
@@ -50,27 +43,32 @@ impl DeriveSequence {
50
43
51
44
Self {
52
45
ident : input. ident ,
53
- lifetime ,
46
+ generics : input . generics . clone ( ) ,
54
47
fields,
55
48
}
56
49
}
57
50
58
51
/// Lower the derived output into a [`TokenStream`].
59
52
pub fn to_tokens ( & self ) -> TokenStream {
60
53
let ident = & self . ident ;
61
-
62
- let lifetime = match self . lifetime {
63
- Some ( ref lifetime) => quote ! ( #lifetime) ,
64
- None => default_lifetime ( ) ,
54
+ let mut generics = self . generics . clone ( ) ;
55
+
56
+ // Use the first lifetime parameter as lifetime for Decode/Encode lifetime
57
+ // if none found, add one.
58
+ let lifetime: Lifetime = if let Some ( lt) = generics. lifetimes ( ) . next ( ) {
59
+ lt. lifetime . clone ( )
60
+ } else {
61
+ let lifetime = default_lifetime ( ) ;
62
+ generics. params . insert (
63
+ 0 ,
64
+ GenericParam :: Lifetime ( LifetimeParam :: new ( lifetime. clone ( ) ) ) ,
65
+ ) ;
66
+ lifetime
65
67
} ;
66
68
67
- // Lifetime parameters
68
- // TODO(tarcieri): support multiple lifetimes
69
- let lt_params = self
70
- . lifetime
71
- . as_ref ( )
72
- . map ( |_| lifetime. clone ( ) )
73
- . unwrap_or_default ( ) ;
69
+ // We may or may not have inserted a lifetime.
70
+ let ( _impl_generics, ty_generics, where_clause) = self . generics . split_for_impl ( ) ;
71
+ let ( impl_generics, _ty_generics, _where_clause) = generics. split_for_impl ( ) ;
74
72
75
73
let mut decode_body = Vec :: new ( ) ;
76
74
let mut decode_result = Vec :: new ( ) ;
@@ -87,7 +85,7 @@ impl DeriveSequence {
87
85
}
88
86
89
87
quote ! {
90
- impl <#lifetime> :: der:: DecodeValue <#lifetime> for #ident<#lt_params> {
88
+ impl #impl_generics :: der:: DecodeValue <#lifetime> for #ident #ty_generics #where_clause {
91
89
fn decode_value<R : :: der:: Reader <#lifetime>>(
92
90
reader: & mut R ,
93
91
header: :: der:: Header ,
@@ -104,7 +102,7 @@ impl DeriveSequence {
104
102
}
105
103
}
106
104
107
- impl <#lifetime> :: der:: EncodeValue for #ident<#lt_params> {
105
+ impl #impl_generics :: der:: EncodeValue for #ident #ty_generics #where_clause {
108
106
fn value_len( & self ) -> :: der:: Result <:: der:: Length > {
109
107
use :: der:: Encode as _;
110
108
@@ -122,7 +120,7 @@ impl DeriveSequence {
122
120
}
123
121
}
124
122
125
- impl <#lifetime> :: der:: Sequence <#lifetime> for #ident<#lt_params> { }
123
+ impl #impl_generics :: der:: Sequence <#lifetime> for #ident #ty_generics #where_clause { }
126
124
}
127
125
}
128
126
}
0 commit comments