mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
Rustfmt malloc_size_of & derive
This commit is contained in:
parent
41a2010ee7
commit
5ba4df0441
2 changed files with 161 additions and 107 deletions
|
@ -11,25 +11,36 @@
|
|||
//! A crate for deriving the MallocSizeOf trait.
|
||||
|
||||
extern crate quote;
|
||||
#[macro_use] extern crate syn;
|
||||
#[macro_use] extern crate synstructure;
|
||||
#[macro_use]
|
||||
extern crate syn;
|
||||
#[macro_use]
|
||||
extern crate synstructure;
|
||||
|
||||
#[cfg(not(test))]
|
||||
decl_derive!([MallocSizeOf, attributes(ignore_malloc_size_of)] => malloc_size_of_derive);
|
||||
|
||||
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, \
|
||||
e.g. #[ignore_malloc_size_of = \"because reasons\"]");
|
||||
}
|
||||
syn::Meta::NameValue(syn::MetaNameValue { ref ident, .. }) if ident == "ignore_malloc_size_of" => {
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
});
|
||||
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, \
|
||||
e.g. #[ignore_malloc_size_of = \"because reasons\"]"
|
||||
);
|
||||
}
|
||||
syn::Meta::NameValue(syn::MetaNameValue { ref ident, .. })
|
||||
if ident == "ignore_malloc_size_of" =>
|
||||
{
|
||||
true
|
||||
},
|
||||
_ => false,
|
||||
});
|
||||
if ignore {
|
||||
None
|
||||
} else if let syn::Type::Array(..) = binding.ast().ty {
|
||||
|
@ -51,7 +62,9 @@ fn malloc_size_of_derive(s: synstructure::Structure) -> quote::Tokens {
|
|||
let mut where_clause = where_clause.unwrap_or(&parse_quote!(where)).clone();
|
||||
for param in ast.generics.type_params() {
|
||||
let ident = param.ident;
|
||||
where_clause.predicates.push(parse_quote!(#ident: ::malloc_size_of::MallocSizeOf));
|
||||
where_clause
|
||||
.predicates
|
||||
.push(parse_quote!(#ident: ::malloc_size_of::MallocSizeOf));
|
||||
}
|
||||
|
||||
let tokens = quote! {
|
||||
|
@ -73,18 +86,23 @@ fn malloc_size_of_derive(s: synstructure::Structure) -> quote::Tokens {
|
|||
|
||||
#[test]
|
||||
fn test_struct() {
|
||||
let source =
|
||||
syn::parse_str("struct Foo<T> { bar: Bar, baz: T, #[ignore_malloc_size_of = \"\"] z: Arc<T> }").unwrap();
|
||||
let 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(" ", "");
|
||||
macro_rules! match_count {
|
||||
($e: expr, $count: expr) => {
|
||||
assert_eq!(no_space.matches(&$e.replace(" ", "")).count(), $count,
|
||||
"counting occurences of {:?} in {:?} (whitespace-insensitive)",
|
||||
$e, expanded)
|
||||
}
|
||||
assert_eq!(
|
||||
no_space.matches(&$e.replace(" ", "")).count(),
|
||||
$count,
|
||||
"counting occurences of {:?} in {:?} (whitespace-insensitive)",
|
||||
$e,
|
||||
expanded
|
||||
)
|
||||
};
|
||||
}
|
||||
match_count!("struct", 0);
|
||||
match_count!("ignore_malloc_size_of", 0);
|
||||
|
@ -104,4 +122,3 @@ fn test_no_reason() {
|
|||
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