Auto merge of #21615 - pyfisch:malloc_size_of-fmt, r=Manishearth

Rustfmt malloc_size_of & derive

Part of #21373. Formatting looks good.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/21615)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2018-09-05 04:52:26 -04:00 committed by GitHub
commit 4d18014241
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 161 additions and 107 deletions

View file

@ -107,9 +107,11 @@ pub struct MallocSizeOfOps {
}
impl MallocSizeOfOps {
pub fn new(size_of: VoidPtrToSizeFn,
malloc_enclosing_size_of: Option<VoidPtrToSizeFn>,
have_seen_ptr: Option<Box<VoidPtrToBoolFnMut>>) -> Self {
pub fn new(
size_of: VoidPtrToSizeFn,
malloc_enclosing_size_of: Option<VoidPtrToSizeFn>,
have_seen_ptr: Option<Box<VoidPtrToBoolFnMut>>,
) -> Self {
MallocSizeOfOps {
size_of_op: size_of,
enclosing_size_of_op: malloc_enclosing_size_of,
@ -127,7 +129,7 @@ impl MallocSizeOfOps {
// larger than the required alignment, but small enough that it is
// always in the first page of memory and therefore not a legitimate
// address.
return ptr as *const usize as usize <= 256
return ptr as *const usize as usize <= 256;
}
/// Call `size_of_op` on `ptr`, first checking that the allocation isn't
@ -154,7 +156,10 @@ impl MallocSizeOfOps {
/// Call `have_seen_ptr_op` on `ptr`.
pub fn have_seen_ptr<T>(&mut self, ptr: *const T) -> bool {
let have_seen_ptr_op = self.have_seen_ptr_op.as_mut().expect("missing have_seen_ptr_op");
let have_seen_ptr_op = self
.have_seen_ptr_op
.as_mut()
.expect("missing have_seen_ptr_op");
have_seen_ptr_op(ptr as *const c_void)
}
}
@ -257,7 +262,9 @@ impl MallocSizeOf for () {
}
impl<T1, T2> MallocSizeOf for (T1, T2)
where T1: MallocSizeOf, T2: MallocSizeOf
where
T1: MallocSizeOf,
T2: MallocSizeOf,
{
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.0.size_of(ops) + self.1.size_of(ops)
@ -265,7 +272,10 @@ impl<T1, T2> MallocSizeOf for (T1, T2)
}
impl<T1, T2, T3> MallocSizeOf for (T1, T2, T3)
where T1: MallocSizeOf, T2: MallocSizeOf, T3: MallocSizeOf
where
T1: MallocSizeOf,
T2: MallocSizeOf,
T3: MallocSizeOf,
{
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.0.size_of(ops) + self.1.size_of(ops) + self.2.size_of(ops)
@ -273,7 +283,11 @@ impl<T1, T2, T3> MallocSizeOf for (T1, T2, T3)
}
impl<T1, T2, T3, T4> MallocSizeOf for (T1, T2, T3, T4)
where T1: MallocSizeOf, T2: MallocSizeOf, T3: MallocSizeOf, T4: MallocSizeOf
where
T1: MallocSizeOf,
T2: MallocSizeOf,
T3: MallocSizeOf,
T4: MallocSizeOf,
{
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.0.size_of(ops) + self.1.size_of(ops) + self.2.size_of(ops) + self.3.size_of(ops)
@ -312,7 +326,8 @@ impl<T: MallocSizeOf> MallocSizeOf for std::cell::RefCell<T> {
}
impl<'a, B: ?Sized + ToOwned> MallocSizeOf for std::borrow::Cow<'a, B>
where B::Owned: MallocSizeOf
where
B::Owned: MallocSizeOf,
{
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
match *self {
@ -404,8 +419,9 @@ impl<A: smallvec::Array> MallocShallowSizeOf for smallvec::SmallVec<A> {
}
impl<A> MallocSizeOf for smallvec::SmallVec<A>
where A: smallvec::Array,
A::Item: MallocSizeOf
where
A: smallvec::Array,
A::Item: MallocSizeOf,
{
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
let mut n = self.shallow_size_of(ops);
@ -417,8 +433,9 @@ impl<A> MallocSizeOf for smallvec::SmallVec<A>
}
impl<T, S> MallocShallowSizeOf for std::collections::HashSet<T, S>
where T: Eq + Hash,
S: BuildHasher
where
T: Eq + Hash,
S: BuildHasher,
{
fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
if ops.has_malloc_enclosing_size_of() {
@ -426,7 +443,9 @@ impl<T, S> MallocShallowSizeOf for std::collections::HashSet<T, S>
// `ops.malloc_enclosing_size_of()` then gives us the storage size.
// This assumes that the `HashSet`'s contents (values and hashes)
// are all stored in a single contiguous heap allocation.
self.iter().next().map_or(0, |t| unsafe { ops.malloc_enclosing_size_of(t) })
self.iter()
.next()
.map_or(0, |t| unsafe { ops.malloc_enclosing_size_of(t) })
} else {
// An estimate.
self.capacity() * (size_of::<T>() + size_of::<usize>())
@ -435,8 +454,9 @@ impl<T, S> MallocShallowSizeOf for std::collections::HashSet<T, S>
}
impl<T, S> MallocSizeOf for std::collections::HashSet<T, S>
where T: Eq + Hash + MallocSizeOf,
S: BuildHasher,
where
T: Eq + Hash + MallocSizeOf,
S: BuildHasher,
{
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
let mut n = self.shallow_size_of(ops);
@ -448,13 +468,16 @@ impl<T, S> MallocSizeOf for std::collections::HashSet<T, S>
}
impl<T, S> MallocShallowSizeOf for hashglobe::hash_set::HashSet<T, S>
where T: Eq + Hash,
S: BuildHasher
where
T: Eq + Hash,
S: BuildHasher,
{
fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
// See the implementation for std::collections::HashSet for details.
if ops.has_malloc_enclosing_size_of() {
self.iter().next().map_or(0, |t| unsafe { ops.malloc_enclosing_size_of(t) })
self.iter()
.next()
.map_or(0, |t| unsafe { ops.malloc_enclosing_size_of(t) })
} else {
self.capacity() * (size_of::<T>() + size_of::<usize>())
}
@ -462,8 +485,9 @@ impl<T, S> MallocShallowSizeOf for hashglobe::hash_set::HashSet<T, S>
}
impl<T, S> MallocSizeOf for hashglobe::hash_set::HashSet<T, S>
where T: Eq + Hash + MallocSizeOf,
S: BuildHasher,
where
T: Eq + Hash + MallocSizeOf,
S: BuildHasher,
{
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
let mut n = self.shallow_size_of(ops);
@ -475,8 +499,9 @@ impl<T, S> MallocSizeOf for hashglobe::hash_set::HashSet<T, S>
}
impl<T, S> MallocShallowSizeOf for hashglobe::fake::HashSet<T, S>
where T: Eq + Hash,
S: BuildHasher,
where
T: Eq + Hash,
S: BuildHasher,
{
fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
use std::ops::Deref;
@ -485,8 +510,9 @@ impl<T, S> MallocShallowSizeOf for hashglobe::fake::HashSet<T, S>
}
impl<T, S> MallocSizeOf for hashglobe::fake::HashSet<T, S>
where T: Eq + Hash + MallocSizeOf,
S: BuildHasher,
where
T: Eq + Hash + MallocSizeOf,
S: BuildHasher,
{
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
use std::ops::Deref;
@ -495,13 +521,16 @@ impl<T, S> MallocSizeOf for hashglobe::fake::HashSet<T, S>
}
impl<K, V, S> MallocShallowSizeOf for std::collections::HashMap<K, V, S>
where K: Eq + Hash,
S: BuildHasher
where
K: Eq + Hash,
S: BuildHasher,
{
fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
// See the implementation for std::collections::HashSet for details.
if ops.has_malloc_enclosing_size_of() {
self.values().next().map_or(0, |v| unsafe { ops.malloc_enclosing_size_of(v) })
self.values()
.next()
.map_or(0, |v| unsafe { ops.malloc_enclosing_size_of(v) })
} else {
self.capacity() * (size_of::<V>() + size_of::<K>() + size_of::<usize>())
}
@ -509,9 +538,10 @@ impl<K, V, S> MallocShallowSizeOf for std::collections::HashMap<K, V, S>
}
impl<K, V, S> MallocSizeOf for std::collections::HashMap<K, V, S>
where K: Eq + Hash + MallocSizeOf,
V: MallocSizeOf,
S: BuildHasher,
where
K: Eq + Hash + MallocSizeOf,
V: MallocSizeOf,
S: BuildHasher,
{
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
let mut n = self.shallow_size_of(ops);
@ -524,13 +554,16 @@ impl<K, V, S> MallocSizeOf for std::collections::HashMap<K, V, S>
}
impl<K, V, S> MallocShallowSizeOf for hashglobe::hash_map::HashMap<K, V, S>
where K: Eq + Hash,
S: BuildHasher
where
K: Eq + Hash,
S: BuildHasher,
{
fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
// See the implementation for std::collections::HashSet for details.
if ops.has_malloc_enclosing_size_of() {
self.values().next().map_or(0, |v| unsafe { ops.malloc_enclosing_size_of(v) })
self.values()
.next()
.map_or(0, |v| unsafe { ops.malloc_enclosing_size_of(v) })
} else {
self.capacity() * (size_of::<V>() + size_of::<K>() + size_of::<usize>())
}
@ -538,9 +571,10 @@ impl<K, V, S> MallocShallowSizeOf for hashglobe::hash_map::HashMap<K, V, S>
}
impl<K, V, S> MallocSizeOf for hashglobe::hash_map::HashMap<K, V, S>
where K: Eq + Hash + MallocSizeOf,
V: MallocSizeOf,
S: BuildHasher,
where
K: Eq + Hash + MallocSizeOf,
V: MallocSizeOf,
S: BuildHasher,
{
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
let mut n = self.shallow_size_of(ops);
@ -553,8 +587,9 @@ impl<K, V, S> MallocSizeOf for hashglobe::hash_map::HashMap<K, V, S>
}
impl<K, V, S> MallocShallowSizeOf for hashglobe::fake::HashMap<K, V, S>
where K: Eq + Hash,
S: BuildHasher,
where
K: Eq + Hash,
S: BuildHasher,
{
fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
use std::ops::Deref;
@ -563,9 +598,10 @@ impl<K, V, S> MallocShallowSizeOf for hashglobe::fake::HashMap<K, V, S>
}
impl<K, V, S> MallocSizeOf for hashglobe::fake::HashMap<K, V, S>
where K: Eq + Hash + MallocSizeOf,
V: MallocSizeOf,
S: BuildHasher,
where
K: Eq + Hash + MallocSizeOf,
V: MallocSizeOf,
S: BuildHasher,
{
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
use std::ops::Deref;
@ -667,8 +703,10 @@ impl<T: MallocSizeOf, U> MallocSizeOf for euclid::TypedRect<T, U> {
impl<T: MallocSizeOf, U> MallocSizeOf for euclid::TypedSideOffsets2D<T, U> {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.top.size_of(ops) + self.right.size_of(ops) +
self.bottom.size_of(ops) + self.left.size_of(ops)
self.top.size_of(ops) +
self.right.size_of(ops) +
self.bottom.size_of(ops) +
self.left.size_of(ops)
}
}
@ -680,22 +718,33 @@ impl<T: MallocSizeOf, U> MallocSizeOf for euclid::TypedSize2D<T, U> {
impl<T: MallocSizeOf, Src, Dst> MallocSizeOf for euclid::TypedTransform2D<T, Src, Dst> {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.m11.size_of(ops) + self.m12.size_of(ops) +
self.m21.size_of(ops) + self.m22.size_of(ops) +
self.m31.size_of(ops) + self.m32.size_of(ops)
self.m11.size_of(ops) +
self.m12.size_of(ops) +
self.m21.size_of(ops) +
self.m22.size_of(ops) +
self.m31.size_of(ops) +
self.m32.size_of(ops)
}
}
impl<T: MallocSizeOf, Src, Dst> MallocSizeOf for euclid::TypedTransform3D<T, Src, Dst> {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.m11.size_of(ops) + self.m12.size_of(ops) +
self.m13.size_of(ops) + self.m14.size_of(ops) +
self.m21.size_of(ops) + self.m22.size_of(ops) +
self.m23.size_of(ops) + self.m24.size_of(ops) +
self.m31.size_of(ops) + self.m32.size_of(ops) +
self.m33.size_of(ops) + self.m34.size_of(ops) +
self.m41.size_of(ops) + self.m42.size_of(ops) +
self.m43.size_of(ops) + self.m44.size_of(ops)
self.m11.size_of(ops) +
self.m12.size_of(ops) +
self.m13.size_of(ops) +
self.m14.size_of(ops) +
self.m21.size_of(ops) +
self.m22.size_of(ops) +
self.m23.size_of(ops) +
self.m24.size_of(ops) +
self.m31.size_of(ops) +
self.m32.size_of(ops) +
self.m33.size_of(ops) +
self.m34.size_of(ops) +
self.m41.size_of(ops) +
self.m42.size_of(ops) +
self.m43.size_of(ops) +
self.m44.size_of(ops)
}
}
@ -712,8 +761,7 @@ impl MallocSizeOf for selectors::parser::AncestorHashes {
}
}
impl<Impl: selectors::parser::SelectorImpl> MallocSizeOf
for selectors::parser::Selector<Impl>
impl<Impl: selectors::parser::SelectorImpl> MallocSizeOf for selectors::parser::Selector<Impl>
where
Impl::NonTSPseudoClass: MallocSizeOf,
Impl::PseudoElement: MallocSizeOf,
@ -733,8 +781,7 @@ where
}
}
impl<Impl: selectors::parser::SelectorImpl> MallocSizeOf
for selectors::parser::Component<Impl>
impl<Impl: selectors::parser::SelectorImpl> MallocSizeOf for selectors::parser::Component<Impl>
where
Impl::NonTSPseudoClass: MallocSizeOf,
Impl::PseudoElement: MallocSizeOf,
@ -743,22 +790,13 @@ where
use selectors::parser::Component;
match self {
Component::AttributeOther(ref attr_selector) => {
attr_selector.size_of(ops)
}
Component::Negation(ref components) => {
components.size_of(ops)
}
Component::NonTSPseudoClass(ref pseudo) => {
(*pseudo).size_of(ops)
}
Component::Slotted(ref selector) |
Component::Host(Some(ref selector)) => {
Component::AttributeOther(ref attr_selector) => attr_selector.size_of(ops),
Component::Negation(ref components) => components.size_of(ops),
Component::NonTSPseudoClass(ref pseudo) => (*pseudo).size_of(ops),
Component::Slotted(ref selector) | Component::Host(Some(ref selector)) => {
selector.size_of(ops)
}
Component::PseudoElement(ref pseudo) => {
(*pseudo).size_of(ops)
}
},
Component::PseudoElement(ref pseudo) => (*pseudo).size_of(ops),
Component::Combinator(..) |
Component::ExplicitAnyNamespace |
Component::ExplicitNoNamespace |
@ -850,7 +888,8 @@ malloc_size_of_is_0!(i8, i16, i32, i64, i128, isize);
malloc_size_of_is_0!(f32, f64);
malloc_size_of_is_0!(std::sync::atomic::AtomicBool);
malloc_size_of_is_0!(std::sync::atomic::AtomicIsize, std::sync::atomic::AtomicUsize);
malloc_size_of_is_0!(std::sync::atomic::AtomicIsize);
malloc_size_of_is_0!(std::sync::atomic::AtomicUsize);
malloc_size_of_is_0!(Range<u8>, Range<u16>, Range<u32>, Range<u64>, Range<usize>);
malloc_size_of_is_0!(Range<i8>, Range<i16>, Range<i32>, Range<i64>, Range<isize>);
@ -919,9 +958,7 @@ malloc_size_of_is_0!(webrender_api::TransformStyle);
#[cfg(feature = "servo")]
impl MallocSizeOf for xml5ever::QualName {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.prefix.size_of(ops) +
self.ns.size_of(ops) +
self.local.size_of(ops)
self.prefix.size_of(ops) + self.ns.size_of(ops) + self.local.size_of(ops)
}
}
@ -946,9 +983,7 @@ impl MallocSizeOf for hyper::header::ContentType {
#[cfg(feature = "servo")]
impl MallocSizeOf for hyper::mime::Mime {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.0.size_of(ops) +
self.1.size_of(ops) +
self.2.size_of(ops)
self.0.size_of(ops) + self.1.size_of(ops) + self.2.size_of(ops)
}
}
@ -975,10 +1010,12 @@ malloc_size_of_is_0!(time::Duration);
malloc_size_of_is_0!(time::Tm);
#[cfg(feature = "servo")]
impl<T> MallocSizeOf for hyper_serde::Serde<T> where
for <'de> hyper_serde::De<T>: serde::Deserialize<'de>,
for <'a> hyper_serde::Ser<'a, T>: serde::Serialize,
T: MallocSizeOf {
impl<T> MallocSizeOf for hyper_serde::Serde<T>
where
for<'de> hyper_serde::De<T>: serde::Deserialize<'de>,
for<'a> hyper_serde::Ser<'a, T>: serde::Serialize,
T: MallocSizeOf,
{
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.0.size_of(ops)
}
@ -1005,7 +1042,7 @@ impl MallocSizeOf for hyper::status::StatusCode {
/// Measurable that defers to inner value and used to verify MallocSizeOf implementation in a
/// struct.
#[derive(Clone)]
pub struct Measurable<T: MallocSizeOf> (pub T);
pub struct Measurable<T: MallocSizeOf>(pub T);
impl<T: MallocSizeOf> Deref for Measurable<T> {
type Target = T;

View file

@ -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));
}