Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No more syntex for serde_derive #548

Merged
merged 19 commits into from
Sep 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions serde_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,22 @@ homepage = "https://serde.rs"
repository = "https://github.com/serde-rs/serde"
documentation = "https://serde.rs/codegen.html"
keywords = ["serde", "serialization"]
build = "build.rs"
include = ["Cargo.toml", "build.rs", "src/**/*.rs", "src/lib.rs.in"]
include = ["Cargo.toml", "src/**/*.rs"]

[features]
default = ["with-syntex"]
unstable = ["quasi_macros"]
unstable = []
unstable-testing = ["clippy"]
with-syntex = [
"quasi/with-syntex",
"quasi_codegen",
"quasi_codegen/with-syntex",
"serde_codegen_internals/with-syntex",
"syntex",
"syntex_syntax",
]

[build-dependencies]
quasi_codegen = { version = "^0.21.0", optional = true }
syntex = { version = "^0.44.0", optional = true }
with-syn = []

[dependencies]
aster = { version = "^0.29.0", default-features = false }
clippy = { version = "^0.*", optional = true }
quasi = { version = "^0.21.0", default-features = false }
quasi_macros = { version = "^0.21.0", optional = true }
quote = "0.2"
serde_codegen_internals = { version = "=0.8.9", default-features = false, path = "../serde_codegen_internals" }
syn = { version = "0.8", features = ["aster", "visit"] }
syntex = { version = "^0.44.0", optional = true }
syntex_syntax = { version = "^0.44.0", optional = true }
28 changes: 0 additions & 28 deletions serde_codegen/build.rs

This file was deleted.

56 changes: 25 additions & 31 deletions serde_codegen/src/bound.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
use std::collections::HashSet;

use aster::AstBuilder;

use syntax::ast;
use syntax::visit;
use syn::{self, aster, visit};

use internals::ast::Item;
use internals::attr;

// 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 {
pub fn without_defaults(generics: &syn::Generics) -> syn::Generics {
syn::Generics {
ty_params: generics.ty_params.iter().map(|ty_param| {
ast::TyParam {
syn::TyParam {
default: None,
.. ty_param.clone()
}}).collect(),
Expand All @@ -23,24 +20,22 @@ pub fn without_defaults(generics: &ast::Generics) -> ast::Generics {
}

pub fn with_where_predicates(
builder: &AstBuilder,
generics: &ast::Generics,
predicates: &[ast::WherePredicate],
) -> ast::Generics {
builder.from_generics(generics.clone())
generics: &syn::Generics,
predicates: &[syn::WherePredicate],
) -> syn::Generics {
aster::from_generics(generics.clone())
.with_predicates(predicates.to_vec())
.build()
}

pub fn with_where_predicates_from_fields<F>(
builder: &AstBuilder,
item: &Item,
generics: &ast::Generics,
generics: &syn::Generics,
from_field: F,
) -> ast::Generics
where F: Fn(&attr::Field) -> Option<&[ast::WherePredicate]>,
) -> syn::Generics
where F: Fn(&attr::Field) -> Option<&[syn::WherePredicate]>,
{
builder.from_generics(generics.clone())
aster::from_generics(generics.clone())
.with_predicates(
item.body.all_fields()
.flat_map(|field| from_field(&field.attrs))
Expand All @@ -60,34 +55,33 @@ pub fn with_where_predicates_from_fields<F>(
// c: C,
// }
pub fn with_bound<F>(
builder: &AstBuilder,
item: &Item,
generics: &ast::Generics,
generics: &syn::Generics,
filter: F,
bound: &ast::Path,
) -> ast::Generics
bound: &syn::Path,
) -> syn::Generics
where F: Fn(&attr::Field) -> bool,
{
struct FindTyParams {
// Set of all generic type parameters on the current struct (A, B, C in
// the example). Initialized up front.
all_ty_params: HashSet<ast::Name>,
all_ty_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.
relevant_ty_params: HashSet<ast::Name>,
relevant_ty_params: HashSet<syn::Ident>,
}
impl visit::Visitor for FindTyParams {
fn visit_path(&mut self, path: &ast::Path, _id: ast::NodeId) {
fn visit_path(&mut self, path: &syn::Path) {
if let Some(seg) = path.segments.last() {
if seg.identifier.name.as_str() == "PhantomData" {
if seg.ident == "PhantomData" {
// Hardcoded exception, because PhantomData<T> implements
// Serialize and Deserialize whether or not T implements it.
return;
}
}
if !path.global && path.segments.len() == 1 {
let id = path.segments[0].identifier.name;
let id = path.segments[0].ident.clone();
if self.all_ty_params.contains(&id) {
self.relevant_ty_params.insert(id);
}
Expand All @@ -97,7 +91,7 @@ pub fn with_bound<F>(
}

let all_ty_params: HashSet<_> = generics.ty_params.iter()
.map(|ty_param| ty_param.ident.name)
.map(|ty_param| ty_param.ident.clone())
.collect();

let relevant_tys = item.body.all_fields()
Expand All @@ -112,14 +106,14 @@ pub fn with_bound<F>(
visit::walk_ty(&mut visitor, ty);
}

builder.from_generics(generics.clone())
aster::from_generics(generics.clone())
.with_predicates(
generics.ty_params.iter()
.map(|ty_param| ty_param.ident.name)
.map(|ty_param| ty_param.ident.clone())
.filter(|id| visitor.relevant_ty_params.contains(id))
.map(|id| builder.where_predicate()
.map(|id| aster::where_predicate()
// the type parameter that is being bounded e.g. T
.bound().build(builder.ty().id(id))
.bound().build(aster::ty().id(id))
// the bound e.g. Serialize
.bound().trait_(bound.clone()).build()
.build()))
Expand Down
Loading