servo/components/style/gecko_string_cache/namespace.rs
Nicholas Nethercote c5aa2cb986 Measure PropertyDeclaration more thoroughly.
This patch replaces the handwritten MallocSizeOf implementation for
PropertyDeclaration with a derived one, which gives much more thorough
measurement.

This requires (a) deriving MallocSizeOf for a *lot* of additional types (most
of which already have `derive(HeapSizeOf)` in Servo builds), and (b)
implementing MallocSizeOf for a few more types in the `malloc_size_of` crate.

These changes would significantly improve the reporting coverage for gmail if
it weren't for the fact that SpecifiedUrl isn't measured due to a lack of
clarity about its fields; that can be fixed as a follow-up once bug 1397971 has
landed.
2017-09-14 13:18:03 +10:00

99 lines
2.3 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! A type to represent a namespace.
use gecko_bindings::structs::nsIAtom;
use precomputed_hash::PrecomputedHash;
use std::borrow::Borrow;
use std::fmt;
use std::ops::Deref;
use string_cache::{Atom, WeakAtom};
#[macro_export]
macro_rules! ns {
() => { $crate::string_cache::Namespace(atom!("")) };
($s: tt) => { $crate::string_cache::Namespace(atom!($s)) };
}
/// A Gecko namespace is just a wrapped atom.
#[derive(Clone, Debug, Default, Eq, Hash, MallocSizeOf, PartialEq)]
pub struct Namespace(pub Atom);
impl PrecomputedHash for Namespace {
#[inline]
fn precomputed_hash(&self) -> u32 {
self.0.precomputed_hash()
}
}
/// A Gecko WeakNamespace is a wrapped WeakAtom.
#[derive(Hash)]
pub struct WeakNamespace(WeakAtom);
impl Deref for WeakNamespace {
type Target = WeakAtom;
#[inline]
fn deref(&self) -> &WeakAtom {
&self.0
}
}
impl Deref for Namespace {
type Target = WeakNamespace;
#[inline]
fn deref(&self) -> &WeakNamespace {
let weak: *const WeakAtom = &*self.0;
unsafe {
&*(weak as *const WeakNamespace)
}
}
}
impl<'a> From<&'a str> for Namespace {
fn from(s: &'a str) -> Self {
Namespace(Atom::from(s))
}
}
impl fmt::Display for Namespace {
fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(w)
}
}
impl Borrow<WeakNamespace> for Namespace {
#[inline]
fn borrow(&self) -> &WeakNamespace {
self
}
}
impl WeakNamespace {
/// Trivially construct a WeakNamespace.
#[inline]
pub unsafe fn new<'a>(atom: *mut nsIAtom) -> &'a Self {
&*(atom as *const WeakNamespace)
}
/// Clone this WeakNamespace to obtain a strong reference to the same
/// underlying namespace.
#[inline]
pub fn clone(&self) -> Namespace {
Namespace(self.0.clone())
}
}
impl Eq for WeakNamespace {}
impl PartialEq for WeakNamespace {
#[inline]
fn eq(&self, other: &Self) -> bool {
let weak: *const WeakNamespace = self;
let other: *const WeakNamespace = other;
weak == other
}
}