mirror of
https://github.com/servo/servo.git
synced 2025-08-05 13:40:08 +01:00
Bump syn/quote in malloc_size_of_derive
This commit is contained in:
parent
3670d4d3e5
commit
cf6330b6c1
3 changed files with 39 additions and 43 deletions
6
Cargo.lock
generated
6
Cargo.lock
generated
|
@ -1571,9 +1571,9 @@ dependencies = [
|
||||||
name = "malloc_size_of_derive"
|
name = "malloc_size_of_derive"
|
||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
|
"quote 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
"syn 0.12.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"synstructure 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"synstructure 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
@ -10,6 +10,6 @@ path = "lib.rs"
|
||||||
proc-macro = true
|
proc-macro = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
quote = "0.3.15"
|
quote = "0.4.2"
|
||||||
syn = "0.11"
|
syn = { version = "0.12.12", features = ["full"] }
|
||||||
synstructure = "0.5"
|
synstructure = "0.7"
|
||||||
|
|
|
@ -10,36 +10,35 @@
|
||||||
|
|
||||||
//! A crate for deriving the MallocSizeOf trait.
|
//! A crate for deriving the MallocSizeOf trait.
|
||||||
|
|
||||||
#[cfg(not(test))] extern crate proc_macro;
|
|
||||||
#[macro_use] extern crate quote;
|
#[macro_use] extern crate quote;
|
||||||
extern crate syn;
|
#[macro_use] extern crate syn;
|
||||||
extern crate synstructure;
|
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
#[proc_macro_derive(MallocSizeOf, attributes(ignore_malloc_size_of))]
|
#[macro_use] extern crate synstructure;
|
||||||
pub fn expand_token_stream(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
|
||||||
expand_string(&input.to_string()).parse().unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn expand_string(input: &str) -> String {
|
#[cfg(test)]
|
||||||
let mut type_ = syn::parse_macro_input(input).unwrap();
|
extern crate synstructure;
|
||||||
|
|
||||||
let style = synstructure::BindStyle::Ref.into();
|
|
||||||
let match_body = synstructure::each_field(&mut type_, &style, |binding| {
|
#[cfg(not(test))]
|
||||||
let ignore = binding.field.attrs.iter().any(|attr| match attr.value {
|
decl_derive!([MallocSizeOf, attributes(ignore_malloc_size_of)] => malloc_size_of_derive);
|
||||||
syn::MetaItem::Word(ref ident) |
|
|
||||||
syn::MetaItem::List(ref ident, _) if ident == "ignore_malloc_size_of" => {
|
fn malloc_size_of_derive(s: synstructure::Structure) -> quote::Tokens {
|
||||||
|
let match_body = s.each(|binding| {
|
||||||
|
let ignore = binding.ast().attrs.iter().any(|attr| match attr.interpret_meta().unwrap() {
|
||||||
|
syn::Meta::Word(ref ident) |
|
||||||
|
syn::Meta::List(syn::MetaList { ref ident, .. }) if ident == "ignore_malloc_size_of" => {
|
||||||
panic!("#[ignore_malloc_size_of] should have an explanation, \
|
panic!("#[ignore_malloc_size_of] should have an explanation, \
|
||||||
e.g. #[ignore_malloc_size_of = \"because reasons\"]");
|
e.g. #[ignore_malloc_size_of = \"because reasons\"]");
|
||||||
}
|
}
|
||||||
syn::MetaItem::NameValue(ref ident, _) if ident == "ignore_malloc_size_of" => {
|
syn::Meta::NameValue(syn::MetaNameValue { ref ident, .. }) if ident == "ignore_malloc_size_of" => {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
_ => false,
|
_ => false,
|
||||||
});
|
});
|
||||||
if ignore {
|
if ignore {
|
||||||
None
|
None
|
||||||
} else if let syn::Ty::Array(..) = binding.field.ty {
|
} else if let syn::Type::Array(..) = binding.ast().ty {
|
||||||
Some(quote! {
|
Some(quote! {
|
||||||
for item in #binding.iter() {
|
for item in #binding.iter() {
|
||||||
sum += ::malloc_size_of::MallocSizeOf::size_of(item, ops);
|
sum += ::malloc_size_of::MallocSizeOf::size_of(item, ops);
|
||||||
|
@ -52,21 +51,13 @@ fn expand_string(input: &str) -> String {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let name = &type_.ident;
|
let ast = s.ast();
|
||||||
let (impl_generics, ty_generics, where_clause) = type_.generics.split_for_impl();
|
let name = &ast.ident;
|
||||||
let mut where_clause = where_clause.clone();
|
let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
|
||||||
for param in &type_.generics.ty_params {
|
let mut where_clause = where_clause.unwrap_or(&parse_quote!(where)).clone();
|
||||||
where_clause.predicates.push(syn::WherePredicate::BoundPredicate(syn::WhereBoundPredicate {
|
for param in ast.generics.type_params() {
|
||||||
bound_lifetimes: Vec::new(),
|
let ident = param.ident;
|
||||||
bounded_ty: syn::Ty::Path(None, param.ident.clone().into()),
|
where_clause.predicates.push(parse_quote!(#ident: ::malloc_size_of::MallocSizeOf));
|
||||||
bounds: vec![syn::TyParamBound::Trait(
|
|
||||||
syn::PolyTraitRef {
|
|
||||||
bound_lifetimes: Vec::new(),
|
|
||||||
trait_ref: syn::parse_path("::malloc_size_of::MallocSizeOf").unwrap(),
|
|
||||||
},
|
|
||||||
syn::TraitBoundModifier::None
|
|
||||||
)],
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let tokens = quote! {
|
let tokens = quote! {
|
||||||
|
@ -83,13 +74,16 @@ fn expand_string(input: &str) -> String {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
tokens.to_string()
|
tokens
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_struct() {
|
fn test_struct() {
|
||||||
let mut source = "struct Foo<T> { bar: Bar, baz: T, #[ignore_malloc_size_of = \"\"] z: Arc<T> }";
|
let source =
|
||||||
let mut expanded = expand_string(source);
|
syn::parse_str("struct Foo<T> { bar: Bar, baz: T, #[ignore_malloc_size_of = \"\"] z: Arc<T> }").unwrap();
|
||||||
|
let source = synstructure::Structure::new(&source);
|
||||||
|
|
||||||
|
let expanded = malloc_size_of_derive(source).to_string();
|
||||||
let mut no_space = expanded.replace(" ", "");
|
let mut no_space = expanded.replace(" ", "");
|
||||||
macro_rules! match_count {
|
macro_rules! match_count {
|
||||||
($e: expr, $count: expr) => {
|
($e: expr, $count: expr) => {
|
||||||
|
@ -103,8 +97,9 @@ fn test_struct() {
|
||||||
match_count!("impl<T> ::malloc_size_of::MallocSizeOf for Foo<T> where T: ::malloc_size_of::MallocSizeOf {", 1);
|
match_count!("impl<T> ::malloc_size_of::MallocSizeOf for Foo<T> where T: ::malloc_size_of::MallocSizeOf {", 1);
|
||||||
match_count!("sum += ::malloc_size_of::MallocSizeOf::size_of(", 2);
|
match_count!("sum += ::malloc_size_of::MallocSizeOf::size_of(", 2);
|
||||||
|
|
||||||
source = "struct Bar([Baz; 3]);";
|
let source = syn::parse_str("struct Bar([Baz; 3]);").unwrap();
|
||||||
expanded = expand_string(source);
|
let source = synstructure::Structure::new(&source);
|
||||||
|
let expanded = malloc_size_of_derive(source).to_string();
|
||||||
no_space = expanded.replace(" ", "");
|
no_space = expanded.replace(" ", "");
|
||||||
match_count!("for item in", 1);
|
match_count!("for item in", 1);
|
||||||
}
|
}
|
||||||
|
@ -112,6 +107,7 @@ fn test_struct() {
|
||||||
#[should_panic(expected = "should have an explanation")]
|
#[should_panic(expected = "should have an explanation")]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_no_reason() {
|
fn test_no_reason() {
|
||||||
expand_string("struct A { #[ignore_malloc_size_of] b: C }");
|
let input = syn::parse_str("struct A { #[ignore_malloc_size_of] b: C }").unwrap();
|
||||||
|
malloc_size_of_derive(synstructure::Structure::new(&input));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue