diff --git a/components/malloc_size_of/lib.rs b/components/malloc_size_of/lib.rs index f4774d66a3e6..7ef851011057 100644 --- a/components/malloc_size_of/lib.rs +++ b/components/malloc_size_of/lib.rs @@ -107,9 +107,11 @@ pub struct MallocSizeOfOps { } impl MallocSizeOfOps { - pub fn new(size_of: VoidPtrToSizeFn, - malloc_enclosing_size_of: Option, - have_seen_ptr: Option>) -> Self { + pub fn new( + size_of: VoidPtrToSizeFn, + malloc_enclosing_size_of: Option, + have_seen_ptr: Option>, + ) -> 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(&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 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 MallocSizeOf for (T1, T2) } impl 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 MallocSizeOf for (T1, T2, T3) } impl 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 MallocSizeOf for std::cell::RefCell { } 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 MallocShallowSizeOf for smallvec::SmallVec { } impl MallocSizeOf for smallvec::SmallVec - 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 MallocSizeOf for smallvec::SmallVec } impl MallocShallowSizeOf for std::collections::HashSet - 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 MallocShallowSizeOf for std::collections::HashSet // `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::() + size_of::()) @@ -435,8 +454,9 @@ impl MallocShallowSizeOf for std::collections::HashSet } impl MallocSizeOf for std::collections::HashSet - 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 MallocSizeOf for std::collections::HashSet } impl MallocShallowSizeOf for hashglobe::hash_set::HashSet - 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::() + size_of::()) } @@ -462,8 +485,9 @@ impl MallocShallowSizeOf for hashglobe::hash_set::HashSet } impl MallocSizeOf for hashglobe::hash_set::HashSet - 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 MallocSizeOf for hashglobe::hash_set::HashSet } impl MallocShallowSizeOf for hashglobe::fake::HashSet - 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 MallocShallowSizeOf for hashglobe::fake::HashSet } impl MallocSizeOf for hashglobe::fake::HashSet - 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 MallocSizeOf for hashglobe::fake::HashSet } impl MallocShallowSizeOf for std::collections::HashMap - 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::() + size_of::() + size_of::()) } @@ -509,9 +538,10 @@ impl MallocShallowSizeOf for std::collections::HashMap } impl MallocSizeOf for std::collections::HashMap - 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 MallocSizeOf for std::collections::HashMap } impl MallocShallowSizeOf for hashglobe::hash_map::HashMap - 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::() + size_of::() + size_of::()) } @@ -538,9 +571,10 @@ impl MallocShallowSizeOf for hashglobe::hash_map::HashMap } impl MallocSizeOf for hashglobe::hash_map::HashMap - 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 MallocSizeOf for hashglobe::hash_map::HashMap } impl MallocShallowSizeOf for hashglobe::fake::HashMap - 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 MallocShallowSizeOf for hashglobe::fake::HashMap } impl MallocSizeOf for hashglobe::fake::HashMap - 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 MallocSizeOf for euclid::TypedRect { impl MallocSizeOf for euclid::TypedSideOffsets2D { 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 MallocSizeOf for euclid::TypedSize2D { impl MallocSizeOf for euclid::TypedTransform2D { 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 MallocSizeOf for euclid::TypedTransform3D { 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 MallocSizeOf - for selectors::parser::Selector +impl MallocSizeOf for selectors::parser::Selector where Impl::NonTSPseudoClass: MallocSizeOf, Impl::PseudoElement: MallocSizeOf, @@ -733,8 +781,7 @@ where } } -impl MallocSizeOf - for selectors::parser::Component +impl MallocSizeOf for selectors::parser::Component 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, 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, Range, Range, Range, Range); malloc_size_of_is_0!(Range, Range, Range, Range, Range); @@ -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 MallocSizeOf for hyper_serde::Serde where - for <'de> hyper_serde::De: serde::Deserialize<'de>, - for <'a> hyper_serde::Ser<'a, T>: serde::Serialize, - T: MallocSizeOf { +impl MallocSizeOf for hyper_serde::Serde +where + for<'de> hyper_serde::De: 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 (pub T); +pub struct Measurable(pub T); impl Deref for Measurable { type Target = T; diff --git a/components/malloc_size_of_derive/lib.rs b/components/malloc_size_of_derive/lib.rs index 20e9225277ee..59446131508f 100644 --- a/components/malloc_size_of_derive/lib.rs +++ b/components/malloc_size_of_derive/lib.rs @@ -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 { bar: Bar, baz: T, #[ignore_malloc_size_of = \"\"] z: Arc }").unwrap(); + let source = syn::parse_str( + "struct Foo { bar: Bar, baz: T, #[ignore_malloc_size_of = \"\"] z: Arc }", + ).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)); } -