Skip to content

Commit f9da73a

Browse files
committed
der_derive: handle type generics in sequence
1 parent b1b6b73 commit f9da73a

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

der/derive/src/sequence.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use field::SequenceField;
88
use proc_macro2::TokenStream;
99
use proc_macro_error::abort;
1010
use quote::quote;
11-
use syn::{DeriveInput, Ident, Lifetime};
11+
use syn::{DeriveInput, Ident, Lifetime, TypeParam};
1212

1313
/// Derive the `Sequence` trait for a struct
1414
pub(crate) struct DeriveSequence {
@@ -18,6 +18,9 @@ pub(crate) struct DeriveSequence {
1818
/// Lifetime of the struct.
1919
lifetime: Option<Lifetime>,
2020

21+
/// Type param of the struct.
22+
type_param: Option<TypeParam>,
23+
2124
/// Fields of the struct.
2225
fields: Vec<SequenceField>,
2326
}
@@ -40,6 +43,8 @@ impl DeriveSequence {
4043
.next()
4144
.map(|lt| lt.lifetime.clone());
4245

46+
let type_param = input.generics.type_params().next().cloned();
47+
4348
let type_attrs = TypeAttrs::parse(&input.attrs);
4449

4550
let fields = data
@@ -51,6 +56,7 @@ impl DeriveSequence {
5156
Self {
5257
ident: input.ident,
5358
lifetime,
59+
type_param,
5460
fields,
5561
}
5662
}
@@ -72,6 +78,13 @@ impl DeriveSequence {
7278
.map(|_| lifetime.clone())
7379
.unwrap_or_default();
7480

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+
7588
let mut decode_body = Vec::new();
7689
let mut decode_result = Vec::new();
7790
let mut encoded_lengths = Vec::new();
@@ -87,7 +100,7 @@ impl DeriveSequence {
87100
}
88101

89102
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> {
91104
fn decode_value<R: ::der::Reader<#lifetime>>(
92105
reader: &mut R,
93106
header: ::der::Header,
@@ -104,7 +117,7 @@ impl DeriveSequence {
104117
}
105118
}
106119

107-
impl<#lifetime> ::der::EncodeValue for #ident<#lt_params> {
120+
impl<#lifetime, #type_params> ::der::EncodeValue for #ident<#lt_params #type_params_names> {
108121
fn value_len(&self) -> ::der::Result<::der::Length> {
109122
use ::der::Encode as _;
110123

@@ -122,7 +135,7 @@ impl DeriveSequence {
122135
}
123136
}
124137

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> {}
126139
}
127140
}
128141
}

0 commit comments

Comments
 (0)