Opt into field bounds when deriving ToCss, instead of opting out

This commit is contained in:
Anthony Ramine 2018-03-08 11:07:57 +01:00
parent 8133f788cf
commit 859002a81f
8 changed files with 89 additions and 67 deletions

View file

@ -5,11 +5,11 @@
use darling::{FromDeriveInput, FromField, FromVariant};
use quote::{ToTokens, Tokens};
use std::collections::HashSet;
use syn::{self, DeriveInput, Field, Ident};
use syn::{ImplGenerics, Path, PathArguments, PathSegment, AngleBracketedGenericArguments, GenericParam};
use syn::{QSelf, Type, TypeGenerics, TypeParam};
use syn::{TypeSlice, TypeArray, TypeTuple, TypePath, TypeParen};
use syn::{Variant, WherePredicate, GenericArgument, Binding};
use syn::{self, AngleBracketedGenericArguments, Binding, DeriveInput, Field};
use syn::{GenericArgument, GenericParam, Ident, ImplGenerics, Path};
use syn::{PathArguments, PathSegment, QSelf, Type, TypeArray, TypeGenerics};
use syn::{TypeParam, TypeParen, TypePath, TypeSlice, TypeTuple};
use syn::{Variant, WherePredicate};
use syn::visit::{self, Visit};
use synstructure::{self, BindingInfo, BindStyle, VariantAst, VariantInfo};
@ -42,13 +42,13 @@ impl<'input, 'path> WhereClause<'input, 'path> {
let output = if let Some(output) = self.trait_output {
output
} else {
self.add_predicate(where_predicate(ty.clone(), trait_path, None));
add_predicate(&mut self.inner, where_predicate(ty.clone(), trait_path, None));
return;
};
if let Type::Path(syn::TypePath { ref path, .. }) = *ty {
if path_to_ident(path).is_some() {
self.add_predicate(where_predicate(ty.clone(), trait_path, None));
add_predicate(&mut self.inner, where_predicate(ty.clone(), trait_path, None));
return;
}
}
@ -64,29 +64,28 @@ impl<'input, 'path> WhereClause<'input, 'path> {
Some((output, output_type)),
);
self.add_predicate(pred);
add_predicate(&mut self.inner, pred);
if let Some(found) = found {
for ident in found {
let ty = Type::Path(syn::TypePath { qself: None, path: ident.into() });
if !self.bounded_types.contains(&ty) {
self.bounded_types.insert(ty.clone());
self.add_predicate(
add_predicate(
&mut self.inner,
where_predicate(ty, trait_path, None),
);
};
}
}
}
}
pub fn add_predicate(&mut self, pred: WherePredicate) {
if let Some(ref mut inner) = self.inner {
inner.predicates.push(pred);
} else {
self.inner = Some(parse_quote!(where));
self.add_predicate(pred);
}
}
pub fn add_predicate(
where_clause: &mut Option<syn::WhereClause>,
pred: WherePredicate,
) {
where_clause.get_or_insert(parse_quote!(where)).predicates.push(pred);
}
pub fn fmap_match<F>(