Simplify with_where_predicates_from_fields

This commit is contained in:
David Tolnay 2018-04-12 18:27:19 -07:00
parent a6e94e7122
commit da8b457f66
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82

View File

@ -9,7 +9,7 @@
use std::collections::HashSet;
use syn::{self, visit};
use syn::punctuated::Punctuated;
use syn::punctuated::{Punctuated, Pair};
use internals::ast::{Data, Container};
use internals::attr;
@ -61,33 +61,35 @@ where
F: Fn(&attr::Field) -> Option<&[syn::WherePredicate]>,
W: Fn(&attr::Field) -> bool,
{
let predicates = cont.data
.all_fields()
.flat_map(|field| {
let field_ty = field.ty;
let matching_generic = |t: &syn::PathSegment, g: &syn::GenericParam| match *g {
syn::GenericParam::Type(ref generic_ty)
if generic_ty.ident == t.ident => true,
_ => false
};
let type_params = generics.type_params()
.map(|param| param.ident)
.collect::<HashSet<_>>();
let mut field_bound: Option<syn::WherePredicate> = None;
if let syn::Type::Path(ref ty_path) = *field_ty {
field_bound = match (gen_bound_where(&field.attrs), ty_path.path.segments.first()) {
(true, Some(syn::punctuated::Pair::Punctuated(ref t, _))) =>
if generics.params.iter().any(|g| matching_generic(t, g)) {
Some(parse_quote!(#field_ty: #trait_bound))
} else {None},
(_, _) => None
};
let predicates_from_associated_types = cont.data
.all_fields()
.filter(|field| gen_bound_where(&field.attrs))
.filter_map(|field| {
if let syn::Type::Path(ref ty) = *field.ty {
if let Some(Pair::Punctuated(ref t, _)) = ty.path.segments.first() {
if type_params.contains(&t.ident) {
return Some(parse_quote!(#ty: #trait_bound));
}
}
}
field_bound.into_iter().chain(from_field(&field.attrs).into_iter().flat_map(|predicates| predicates.to_vec()))
None::<syn::WherePredicate>
});
let predicates_from_field_attrs = cont.data
.all_fields()
.flat_map(|field| from_field(&field.attrs))
.flat_map(|predicates| predicates.to_vec());
let mut generics = generics.clone();
generics.make_where_clause()
.predicates
.extend(predicates);
{
let predicates = &mut generics.make_where_clause().predicates;
predicates.extend(predicates_from_associated_types);
predicates.extend(predicates_from_field_attrs);
}
generics
}