Skip to content

Commit ab6588e

Browse files
committed
Extract duplicated code into a function
1 parent 1d11f03 commit ab6588e

File tree

1 file changed

+22
-29
lines changed

1 file changed

+22
-29
lines changed

serde_derive/src/de.rs

+22-29
Original file line numberDiff line numberDiff line change
@@ -3084,23 +3084,29 @@ struct DeTypeGenerics<'a>(&'a Parameters);
30843084
#[cfg(feature = "deserialize_in_place")]
30853085
struct InPlaceTypeGenerics<'a>(&'a Parameters);
30863086

3087+
/// If `'de` lifetime is defined, prepends it to list of generics
3088+
/// and then produces tokens for declaration generics on type
3089+
fn to_tokens(mut generics: syn::Generics, borrowed: &BorrowedLifetimes, tokens: &mut TokenStream) {
3090+
if borrowed.de_lifetime_param().is_some() {
3091+
let def = syn::LifetimeParam {
3092+
attrs: Vec::new(),
3093+
lifetime: syn::Lifetime::new("'de", Span::call_site()),
3094+
colon_token: None,
3095+
bounds: Punctuated::new(),
3096+
};
3097+
// Prepend 'de lifetime to list of generics
3098+
generics.params = Some(syn::GenericParam::Lifetime(def))
3099+
.into_iter()
3100+
.chain(generics.params)
3101+
.collect();
3102+
}
3103+
let (_, ty_generics, _) = generics.split_for_impl();
3104+
ty_generics.to_tokens(tokens);
3105+
}
3106+
30873107
impl<'a> ToTokens for DeTypeGenerics<'a> {
30883108
fn to_tokens(&self, tokens: &mut TokenStream) {
3089-
let mut generics = self.0.generics.clone();
3090-
if self.0.borrowed.de_lifetime_param().is_some() {
3091-
let def = syn::LifetimeParam {
3092-
attrs: Vec::new(),
3093-
lifetime: syn::Lifetime::new("'de", Span::call_site()),
3094-
colon_token: None,
3095-
bounds: Punctuated::new(),
3096-
};
3097-
generics.params = Some(syn::GenericParam::Lifetime(def))
3098-
.into_iter()
3099-
.chain(generics.params)
3100-
.collect();
3101-
}
3102-
let (_, ty_generics, _) = generics.split_for_impl();
3103-
ty_generics.to_tokens(tokens);
3109+
to_tokens(self.0.generics.clone(), &self.0.borrowed, tokens);
31043110
}
31053111
}
31063112

@@ -3113,20 +3119,7 @@ impl<'a> ToTokens for InPlaceTypeGenerics<'a> {
31133119
.chain(generics.params)
31143120
.collect();
31153121

3116-
if self.0.borrowed.de_lifetime_param().is_some() {
3117-
let def = syn::LifetimeParam {
3118-
attrs: Vec::new(),
3119-
lifetime: syn::Lifetime::new("'de", Span::call_site()),
3120-
colon_token: None,
3121-
bounds: Punctuated::new(),
3122-
};
3123-
generics.params = Some(syn::GenericParam::Lifetime(def))
3124-
.into_iter()
3125-
.chain(generics.params)
3126-
.collect();
3127-
}
3128-
let (_, ty_generics, _) = generics.split_for_impl();
3129-
ty_generics.to_tokens(tokens);
3122+
to_tokens(generics, &self.0.borrowed, tokens);
31303123
}
31313124
}
31323125

0 commit comments

Comments
 (0)