@@ -8,7 +8,7 @@ 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 , Ident , Lifetime , TypeParam } ;
12
12
13
13
/// Derive the `Sequence` trait for a struct
14
14
pub ( crate ) struct DeriveSequence {
@@ -18,6 +18,9 @@ pub(crate) struct DeriveSequence {
18
18
/// Lifetime of the struct.
19
19
lifetime : Option < Lifetime > ,
20
20
21
+ /// Type param of the struct.
22
+ type_param : Option < TypeParam > ,
23
+
21
24
/// Fields of the struct.
22
25
fields : Vec < SequenceField > ,
23
26
}
@@ -40,6 +43,8 @@ impl DeriveSequence {
40
43
. next ( )
41
44
. map ( |lt| lt. lifetime . clone ( ) ) ;
42
45
46
+ let type_param = input. generics . type_params ( ) . next ( ) . cloned ( ) ;
47
+
43
48
let type_attrs = TypeAttrs :: parse ( & input. attrs ) ;
44
49
45
50
let fields = data
@@ -51,6 +56,7 @@ impl DeriveSequence {
51
56
Self {
52
57
ident : input. ident ,
53
58
lifetime,
59
+ type_param,
54
60
fields,
55
61
}
56
62
}
@@ -72,6 +78,13 @@ impl DeriveSequence {
72
78
. map ( |_| lifetime. clone ( ) )
73
79
. unwrap_or_default ( ) ;
74
80
81
+ let type_params = self . type_param . as_ref ( ) . map ( |t| {
82
+ let mut t = t. clone ( ) ;
83
+ t. default = None ;
84
+ t
85
+ } ) ;
86
+ let type_params_names = self . type_param . as_ref ( ) . map ( |t| t. ident . clone ( ) ) ;
87
+
75
88
let mut decode_body = Vec :: new ( ) ;
76
89
let mut decode_result = Vec :: new ( ) ;
77
90
let mut encoded_lengths = Vec :: new ( ) ;
@@ -87,7 +100,7 @@ impl DeriveSequence {
87
100
}
88
101
89
102
quote ! {
90
- impl <#lifetime> :: der:: DecodeValue <#lifetime> for #ident<#lt_params> {
103
+ impl <#lifetime, #type_params > :: der:: DecodeValue <#lifetime> for #ident<#lt_params #type_params_names > {
91
104
fn decode_value<R : :: der:: Reader <#lifetime>>(
92
105
reader: & mut R ,
93
106
header: :: der:: Header ,
@@ -104,7 +117,7 @@ impl DeriveSequence {
104
117
}
105
118
}
106
119
107
- impl <#lifetime> :: der:: EncodeValue for #ident<#lt_params> {
120
+ impl <#lifetime, #type_params > :: der:: EncodeValue for #ident<#lt_params #type_params_names > {
108
121
fn value_len( & self ) -> :: der:: Result <:: der:: Length > {
109
122
use :: der:: Encode as _;
110
123
@@ -122,7 +135,7 @@ impl DeriveSequence {
122
135
}
123
136
}
124
137
125
- impl <#lifetime> :: der:: Sequence <#lifetime> for #ident<#lt_params> { }
138
+ impl <#lifetime, #type_params > :: der:: Sequence <#lifetime> for #ident<#lt_params #type_params_names > { }
126
139
}
127
140
}
128
141
}
0 commit comments