Skip to content
Myr

Merge pull request #3035 from dishmaker/dishmaker/serde_derive_with_bound_llvml

Before adoption: this commit came in with the history the project adopted, so no landing records it.

serde_derive: rewrite with_bound without filter/chain/vec allocation

authored by David Tolnay · committed by GitHub · parents 888de67, bfc9ccf · Browse files · 1 file+36 added−28 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+36 added−28 removed

  • other:+36 added−28 removed

1 file+36 added−28 removed

modified:serde_derive/src/bound.rs+36 added-28 removed
279 unmodified lines
280 }281282 let relevant_type_params = visitor.relevant_type_params;283 let associated_type_usage = visitor.associated_type_usage;284 let new_predicates = generics285 .type_params()286 .map(|param| param.ident.clone())287 .filter(|id| relevant_type_params.contains(id))288 .map(|id| syn::TypePath {289 qself: None,290 path: id.into(),291 })292 .chain(associated_type_usage.into_iter().cloned())293 .map(|bounded_ty| {294 syn::WherePredicate::Type(syn::PredicateType {295 lifetimes: None,296 // the type parameter that is being bounded e.g. T297 bounded_ty: syn::Type::Path(bounded_ty),298 colon_token: <Token![:]>::default(),299 // the bound e.g. Serialize300 bounds: vec![syn::TypeParamBound::Trait(syn::TraitBound {284285 fn make_where_bounded_type(286 bounded_ty: syn::TypePath,287 bound: &syn::Path,288 ) -> syn::WherePredicate {289 syn::WherePredicate::Type(syn::PredicateType {290 lifetimes: None,291 // the type parameter that is being bounded e.g. T292 bounded_ty: syn::Type::Path(bounded_ty),293 colon_token: <Token![:]>::default(),294 // the bound e.g. Serialize295 bounds: {296 let mut punct = Punctuated::new();297 punct.push(syn::TypeParamBound::Trait(syn::TraitBound {298 paren_token: None,299 modifier: syn::TraitBoundModifier::None,300 lifetimes: None,301 path: bound.clone(),305 })]306 .into_iter()307 .collect(),308 })309 });302 }));303 punct304 },305 })306 }307311 let mut generics = generics.clone();312 generics313 .make_where_clause()314 .predicates315 .extend(new_predicates);316 generics308 let mut dst_generics = generics.clone();309 let dst_predicates = &mut dst_generics.make_where_clause().predicates;310 for param in generics.type_params() {311 let id = &param.ident;312 if !relevant_type_params.contains(id) {313 continue;314 }315 let bounded_ty = syn::TypePath {316 qself: None,317 path: id.clone().into(),318 };319 dst_predicates.push(make_where_bounded_type(bounded_ty, bound));320 }321 for bounded_ty in associated_type_usage {322 dst_predicates.push(make_where_bounded_type(bounded_ty.clone(), bound));323 }324 dst_generics325}326327pub fn with_self_bound(328 cont: &Container,
97 unmodified lines