318 lines
10 KiB
Rust
Raw Normal View History

use std::collections::HashSet;
use syn;
2018-04-12 22:58:24 -07:00
use syn::punctuated::{Pair, Punctuated};
use syn::visit::{self, Visit};
2018-04-12 22:58:24 -07:00
use internals::ast::{Container, Data};
use internals::attr;
2018-03-31 23:46:25 +02:00
use proc_macro2::Span;
2017-02-20 13:18:38 -08:00
// Remove the default from every type parameter because in the generated impls
// they look like associated types: "error: associated type bindings are not
// allowed here".
2016-09-10 21:53:14 -07:00
pub fn without_defaults(generics: &syn::Generics) -> syn::Generics {
syn::Generics {
2018-01-08 21:49:09 -08:00
params: generics
.params
2017-02-12 21:59:04 -08:00
.iter()
.map(|param| match param {
syn::GenericParam::Type(param) => syn::GenericParam::Type(syn::TypeParam {
2018-04-12 22:58:24 -07:00
eq_token: None,
default: None,
..param.clone()
}),
2018-01-08 21:49:09 -08:00
_ => param.clone(),
2018-11-21 01:13:17 -08:00
})
.collect(),
2017-02-12 21:59:04 -08:00
..generics.clone()
}
}
2017-04-13 12:28:23 -07:00
pub fn with_where_predicates(
generics: &syn::Generics,
predicates: &[syn::WherePredicate],
) -> syn::Generics {
2017-02-20 13:18:38 -08:00
let mut generics = generics.clone();
2018-04-12 22:58:24 -07:00
generics
.make_where_clause()
2017-04-13 12:28:23 -07:00
.predicates
.extend(predicates.iter().cloned());
2017-02-20 13:18:38 -08:00
generics
}
pub fn with_where_predicates_from_fields(
cont: &Container,
2017-04-13 12:28:23 -07:00
generics: &syn::Generics,
from_field: fn(&attr::Field) -> Option<&[syn::WherePredicate]>,
) -> syn::Generics {
2018-05-19 17:33:30 -07:00
let predicates = cont
.data
.all_fields()
.flat_map(|field| from_field(&field.attrs))
.flat_map(|predicates| predicates.to_vec());
2017-02-20 13:18:38 -08:00
let mut generics = generics.clone();
generics.make_where_clause().predicates.extend(predicates);
generics
}
pub fn with_where_predicates_from_variants(
cont: &Container,
generics: &syn::Generics,
from_variant: fn(&attr::Variant) -> Option<&[syn::WherePredicate]>,
) -> syn::Generics {
let variants = match &cont.data {
Data::Enum(variants) => variants,
Data::Struct(_, _) => {
return generics.clone();
}
};
let predicates = variants
.iter()
.flat_map(|variant| from_variant(&variant.attrs))
.flat_map(|predicates| predicates.to_vec());
let mut generics = generics.clone();
2018-04-12 22:58:24 -07:00
generics.make_where_clause().predicates.extend(predicates);
2017-02-20 13:18:38 -08:00
generics
}
// Puts the given bound on any generic type parameters that are used in fields
// for which filter returns true.
//
// For example, the following struct needs the bound `A: Serialize, B:
// Serialize`.
//
// struct S<'b, A, B: 'b, C> {
// a: A,
// b: Option<&'b B>
// #[serde(skip_serializing)]
// c: C,
// }
pub fn with_bound(
cont: &Container,
2017-04-13 12:28:23 -07:00
generics: &syn::Generics,
filter: fn(&attr::Field, Option<&attr::Variant>) -> bool,
2017-04-13 12:28:23 -07:00
bound: &syn::Path,
) -> syn::Generics {
struct FindTyParams<'ast> {
// Set of all generic type parameters on the current struct (A, B, C in
// the example). Initialized up front.
2018-04-12 22:04:34 -07:00
all_type_params: HashSet<syn::Ident>,
// Set of generic type parameters used in fields for which filter
// returns true (A and B in the example). Filled in as the visitor sees
// them.
2018-04-12 22:04:34 -07:00
relevant_type_params: HashSet<syn::Ident>,
// Fields whose type is an associated type of one of the generic type
// parameters.
associated_type_usage: Vec<&'ast syn::TypePath>,
}
impl<'ast> Visit<'ast> for FindTyParams<'ast> {
fn visit_field(&mut self, field: &'ast syn::Field) {
if let syn::Type::Path(ty) = &field.ty {
if let Some(Pair::Punctuated(t, _)) = ty.path.segments.pairs().next() {
if self.all_type_params.contains(&t.ident) {
self.associated_type_usage.push(ty);
}
}
}
self.visit_type(&field.ty);
}
fn visit_path(&mut self, path: &'ast syn::Path) {
if let Some(seg) = path.segments.last() {
2019-07-31 21:09:14 -07:00
if seg.ident == "PhantomData" {
// Hardcoded exception, because PhantomData<T> implements
// Serialize and Deserialize whether or not T implements it.
return;
}
}
2018-01-08 21:49:09 -08:00
if path.leading_colon.is_none() && path.segments.len() == 1 {
2018-05-20 19:34:52 -07:00
let id = &path.segments[0].ident;
if self.all_type_params.contains(id) {
self.relevant_type_params.insert(id.clone());
}
}
2018-01-08 21:49:09 -08:00
visit::visit_path(self, path);
}
// Type parameter should not be considered used by a macro path.
//
// struct TypeMacro<T> {
// mac: T!(),
// marker: PhantomData<T>,
// }
fn visit_macro(&mut self, _mac: &'ast syn::Macro) {}
}
2018-05-22 21:06:32 -07:00
let all_type_params = generics
.type_params()
.map(|param| param.ident.clone())
.collect();
let mut visitor = FindTyParams {
all_type_params,
2018-04-12 22:04:34 -07:00
relevant_type_params: HashSet::new(),
associated_type_usage: Vec::new(),
};
match &cont.data {
Data::Enum(variants) => {
2018-11-21 01:13:17 -08:00
for variant in variants.iter() {
let relevant_fields = variant
.fields
.iter()
.filter(|field| filter(&field.attrs, Some(&variant.attrs)));
for field in relevant_fields {
visitor.visit_field(field.original);
}
}
2018-11-21 01:13:17 -08:00
}
Data::Struct(_, fields) => {
for field in fields.iter().filter(|field| filter(&field.attrs, None)) {
visitor.visit_field(field.original);
}
}
}
let relevant_type_params = visitor.relevant_type_params;
let associated_type_usage = visitor.associated_type_usage;
2018-04-12 22:58:24 -07:00
let new_predicates = generics
.type_params()
2018-05-20 19:34:52 -07:00
.map(|param| param.ident.clone())
.filter(|id| relevant_type_params.contains(id))
2018-04-12 22:58:24 -07:00
.map(|id| syn::TypePath {
qself: None,
path: id.into(),
2018-11-21 01:13:17 -08:00
})
.chain(associated_type_usage.into_iter().cloned())
.map(|bounded_ty| {
2018-01-08 21:49:09 -08:00
syn::WherePredicate::Type(syn::PredicateType {
lifetimes: None,
2017-12-23 20:13:08 -08:00
// the type parameter that is being bounded e.g. T
bounded_ty: syn::Type::Path(bounded_ty),
2018-06-26 23:58:02 -07:00
colon_token: <Token![:]>::default(),
2017-12-23 20:13:08 -08:00
// the bound e.g. Serialize
2018-04-12 22:58:24 -07:00
bounds: vec![syn::TypeParamBound::Trait(syn::TraitBound {
paren_token: None,
modifier: syn::TraitBoundModifier::None,
lifetimes: None,
path: bound.clone(),
2018-11-21 01:13:17 -08:00
})]
.into_iter()
2018-08-06 22:40:28 -07:00
.collect(),
2017-12-23 20:13:08 -08:00
})
});
2017-02-20 13:18:38 -08:00
let mut generics = generics.clone();
2018-04-12 22:58:24 -07:00
generics
.make_where_clause()
2018-01-08 21:49:09 -08:00
.predicates
.extend(new_predicates);
2017-02-20 13:18:38 -08:00
generics
}
2017-02-20 13:18:38 -08:00
2017-04-18 14:23:21 -07:00
pub fn with_self_bound(
cont: &Container,
generics: &syn::Generics,
bound: &syn::Path,
) -> syn::Generics {
let mut generics = generics.clone();
2018-04-12 22:58:24 -07:00
generics
.make_where_clause()
2017-04-13 12:28:23 -07:00
.predicates
2018-01-08 21:49:09 -08:00
.push(syn::WherePredicate::Type(syn::PredicateType {
lifetimes: None,
// the type that is being bounded e.g. MyStruct<'a, T>
bounded_ty: type_of_item(cont),
2018-06-26 23:58:02 -07:00
colon_token: <Token![:]>::default(),
2018-01-08 21:49:09 -08:00
// the bound e.g. Default
2018-04-12 22:58:24 -07:00
bounds: vec![syn::TypeParamBound::Trait(syn::TraitBound {
paren_token: None,
modifier: syn::TraitBoundModifier::None,
lifetimes: None,
path: bound.clone(),
2018-11-21 01:13:17 -08:00
})]
.into_iter()
2018-08-06 22:40:28 -07:00
.collect(),
2018-01-08 21:49:09 -08:00
}));
generics
}
2017-04-13 12:28:23 -07:00
pub fn with_lifetime_bound(generics: &syn::Generics, lifetime: &str) -> syn::Generics {
2018-03-31 23:46:25 +02:00
let bound = syn::Lifetime::new(lifetime, Span::call_site());
2018-01-08 21:49:09 -08:00
let def = syn::LifetimeDef {
attrs: Vec::new(),
2018-05-20 19:34:52 -07:00
lifetime: bound.clone(),
2018-01-08 21:49:09 -08:00
colon_token: None,
bounds: Punctuated::new(),
};
2017-02-20 13:18:38 -08:00
2018-01-08 21:49:09 -08:00
let params = Some(syn::GenericParam::Lifetime(def))
.into_iter()
2018-04-12 22:58:24 -07:00
.chain(generics.params.iter().cloned().map(|mut param| {
match &mut param {
syn::GenericParam::Lifetime(param) => {
2018-05-20 19:34:52 -07:00
param.bounds.push(bound.clone());
2018-01-08 21:49:09 -08:00
}
syn::GenericParam::Type(param) => {
2018-05-22 21:06:32 -07:00
param
.bounds
.push(syn::TypeParamBound::Lifetime(bound.clone()));
2018-04-12 22:58:24 -07:00
}
syn::GenericParam::Const(_) => {}
}
param
2018-11-21 01:13:17 -08:00
}))
.collect();
2017-02-20 13:18:38 -08:00
2018-01-08 21:49:09 -08:00
syn::Generics {
params,
2018-01-08 21:49:09 -08:00
..generics.clone()
2017-02-20 13:18:38 -08:00
}
}
2018-01-08 21:49:09 -08:00
fn type_of_item(cont: &Container) -> syn::Type {
syn::Type::Path(syn::TypePath {
qself: None,
path: syn::Path {
leading_colon: None,
2018-04-12 22:58:24 -07:00
segments: vec![syn::PathSegment {
2018-05-20 19:34:52 -07:00
ident: cont.ident.clone(),
2018-04-12 22:58:24 -07:00
arguments: syn::PathArguments::AngleBracketed(
syn::AngleBracketedGenericArguments {
colon2_token: None,
2018-06-26 23:58:02 -07:00
lt_token: <Token![<]>::default(),
2018-05-19 17:33:30 -07:00
args: cont
.generics
2018-04-12 22:58:24 -07:00
.params
.iter()
.map(|param| match param {
syn::GenericParam::Type(param) => {
2018-04-12 22:58:24 -07:00
syn::GenericArgument::Type(syn::Type::Path(syn::TypePath {
qself: None,
2018-05-20 19:34:52 -07:00
path: param.ident.clone().into(),
2018-04-12 22:58:24 -07:00
}))
}
syn::GenericParam::Lifetime(param) => {
2018-05-20 19:34:52 -07:00
syn::GenericArgument::Lifetime(param.lifetime.clone())
2018-04-12 22:58:24 -07:00
}
syn::GenericParam::Const(_) => {
panic!("Serde does not support const generics yet");
}
2018-11-21 01:13:17 -08:00
})
.collect(),
2018-06-26 23:58:02 -07:00
gt_token: <Token![>]>::default(),
2018-04-12 22:58:24 -07:00
},
),
2018-11-21 01:13:17 -08:00
}]
.into_iter()
2018-08-06 22:40:28 -07:00
.collect(),
2017-04-13 12:28:23 -07:00
},
2018-01-08 21:49:09 -08:00
})
}