2016-07-21 22:51:28 -07:00
|
|
|
use std::collections::HashSet;
|
|
|
|
|
2016-04-13 00:34:29 -07:00
|
|
|
use aster::AstBuilder;
|
|
|
|
|
|
|
|
use syntax::ast;
|
|
|
|
use syntax::visit;
|
|
|
|
|
2016-06-30 19:53:57 -07:00
|
|
|
use internals::ast::Item;
|
|
|
|
use internals::attr;
|
2016-05-20 22:03:20 -07:00
|
|
|
|
2016-04-13 23:51:04 -07: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".
|
|
|
|
pub fn without_defaults(generics: &ast::Generics) -> ast::Generics {
|
|
|
|
ast::Generics {
|
2016-05-02 07:08:43 +02:00
|
|
|
ty_params: generics.ty_params.iter().map(|ty_param| {
|
2016-04-13 23:51:04 -07:00
|
|
|
ast::TyParam {
|
|
|
|
default: None,
|
|
|
|
.. ty_param.clone()
|
2016-05-02 07:08:43 +02:00
|
|
|
}}).collect(),
|
2016-04-13 23:51:04 -07:00
|
|
|
.. generics.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-20 22:03:20 -07:00
|
|
|
pub fn with_where_predicates(
|
|
|
|
builder: &AstBuilder,
|
|
|
|
generics: &ast::Generics,
|
2016-06-04 14:24:30 -07:00
|
|
|
predicates: &[ast::WherePredicate],
|
2016-05-20 22:03:20 -07:00
|
|
|
) -> ast::Generics {
|
|
|
|
builder.from_generics(generics.clone())
|
2016-06-04 14:24:30 -07:00
|
|
|
.with_predicates(predicates.to_vec())
|
2016-05-20 22:03:20 -07:00
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_where_predicates_from_fields<F>(
|
2016-04-13 00:34:29 -07:00
|
|
|
builder: &AstBuilder,
|
2016-06-14 23:37:20 -07:00
|
|
|
item: &Item,
|
2016-04-13 00:34:29 -07:00
|
|
|
generics: &ast::Generics,
|
2016-05-20 22:03:20 -07:00
|
|
|
from_field: F,
|
2016-06-14 23:37:20 -07:00
|
|
|
) -> ast::Generics
|
2016-06-19 20:15:49 -07:00
|
|
|
where F: Fn(&attr::Field) -> Option<&[ast::WherePredicate]>,
|
2016-05-20 22:03:20 -07:00
|
|
|
{
|
2016-06-14 23:37:20 -07:00
|
|
|
builder.from_generics(generics.clone())
|
2016-05-20 22:03:20 -07:00
|
|
|
.with_predicates(
|
2016-06-14 23:37:20 -07:00
|
|
|
item.body.all_fields()
|
|
|
|
.flat_map(|field| from_field(&field.attrs))
|
2016-06-04 14:24:30 -07:00
|
|
|
.flat_map(|predicates| predicates.to_vec()))
|
2016-06-14 23:37:20 -07:00
|
|
|
.build()
|
2016-05-20 22:03:20 -07:00
|
|
|
}
|
|
|
|
|
2016-07-21 22:51:28 -07:00
|
|
|
// 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,
|
|
|
|
// }
|
2016-05-20 22:03:20 -07:00
|
|
|
pub fn with_bound<F>(
|
|
|
|
builder: &AstBuilder,
|
2016-06-14 23:37:20 -07:00
|
|
|
item: &Item,
|
2016-05-20 22:03:20 -07:00
|
|
|
generics: &ast::Generics,
|
|
|
|
filter: F,
|
2016-04-24 10:59:57 -07:00
|
|
|
bound: &ast::Path,
|
2016-06-14 23:37:20 -07:00
|
|
|
) -> ast::Generics
|
2016-06-19 20:15:49 -07:00
|
|
|
where F: Fn(&attr::Field) -> bool,
|
2016-05-20 22:03:20 -07:00
|
|
|
{
|
2016-07-21 22:51:28 -07:00
|
|
|
struct FindTyParams {
|
|
|
|
// Set of all generic type parameters on the current struct (A, B, C in
|
|
|
|
// the example). Initialized up front.
|
2016-07-22 14:09:14 +02:00
|
|
|
all_ty_params: HashSet<ast::Name>,
|
2016-07-21 22:51:28 -07:00
|
|
|
// 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.
|
2016-07-22 14:09:14 +02:00
|
|
|
relevant_ty_params: HashSet<ast::Name>,
|
2016-06-04 16:12:01 -07:00
|
|
|
}
|
2016-07-21 22:51:28 -07:00
|
|
|
impl visit::Visitor for FindTyParams {
|
2016-07-02 18:12:26 -07:00
|
|
|
fn visit_path(&mut self, path: &ast::Path, _id: ast::NodeId) {
|
2016-07-21 22:51:28 -07:00
|
|
|
if !path.global && path.segments.len() == 1 {
|
2016-07-22 14:09:14 +02:00
|
|
|
let id = path.segments[0].identifier.name;
|
2016-07-21 22:51:28 -07:00
|
|
|
if self.all_ty_params.contains(&id) {
|
|
|
|
self.relevant_ty_params.insert(id);
|
|
|
|
}
|
2016-06-04 16:12:01 -07:00
|
|
|
}
|
2016-07-21 22:51:28 -07:00
|
|
|
visit::walk_path(self, path);
|
2016-06-04 16:12:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 22:51:28 -07:00
|
|
|
let all_ty_params: HashSet<_> = generics.ty_params.iter()
|
2016-07-22 14:09:14 +02:00
|
|
|
.map(|ty_param| ty_param.ident.name)
|
2016-07-21 22:51:28 -07:00
|
|
|
.collect();
|
2016-06-04 16:12:01 -07:00
|
|
|
|
2016-07-21 22:51:28 -07:00
|
|
|
let relevant_tys = item.body.all_fields()
|
|
|
|
.filter(|&field| filter(&field.attrs))
|
|
|
|
.map(|field| &field.ty);
|
|
|
|
|
|
|
|
let mut visitor = FindTyParams {
|
|
|
|
all_ty_params: all_ty_params,
|
|
|
|
relevant_ty_params: HashSet::new(),
|
|
|
|
};
|
|
|
|
for ty in relevant_tys {
|
|
|
|
visit::walk_ty(&mut visitor, ty);
|
2016-04-13 00:34:29 -07:00
|
|
|
}
|
2016-07-21 22:51:28 -07:00
|
|
|
|
|
|
|
builder.from_generics(generics.clone())
|
|
|
|
.with_predicates(
|
|
|
|
generics.ty_params.iter()
|
2016-07-22 14:09:14 +02:00
|
|
|
.map(|ty_param| ty_param.ident.name)
|
2016-07-21 22:51:28 -07:00
|
|
|
.filter(|id| visitor.relevant_ty_params.contains(id))
|
|
|
|
.map(|id| builder.where_predicate()
|
|
|
|
// the type parameter that is being bounded e.g. T
|
|
|
|
.bound().build(builder.ty().id(id))
|
|
|
|
// the bound e.g. Serialize
|
|
|
|
.bound().trait_(bound.clone()).build()
|
|
|
|
.build()))
|
|
|
|
.build()
|
2016-04-13 00:34:29 -07:00
|
|
|
}
|