gecko-dev/servo/components/style/gecko_string_cache/namespace.rs
Emilio Cobos Álvarez ef539fe318 servo: Merge #16070 - selectors: Get rid of hashing overhead using the precomputed hash atoms have (from emilio:selectors-bloom-hash-less); r=bholley
I realized of this when @bzbarsky  mentioned the bloom filter in https://bugzilla.mozilla.org/show_bug.cgi?id=1348935#c7.

Right now we hash (the hash) all the time, when we can do better.

This requires a change in string-cache, which is at https://github.com/servo/string-cache/pull/183.

Source-Repo: https://github.com/servo/servo
Source-Revision: a811776df478a009ac6a7785ad13684e3a8e0925

--HG--
extra : subtree_source : https%3A//hg.mozilla.org/projects/converted-servo-linear
extra : subtree_revision : a84fb1b7a5ccab19ecfa8bb57499719f881fd1c1
2017-04-07 19:03:12 -05:00

99 lines
2.2 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, Cow};
use std::fmt;
use std::ops::Deref;
use string_cache::{Atom, WeakAtom};
#[macro_export]
macro_rules! ns {
() => { $crate::string_cache::Namespace(atom!("")) }
}
/// A Gecko namespace is just a wrapped atom.
#[derive(Debug, PartialEq, Eq, Clone, Default, Hash)]
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<Cow<'a, str>> for Namespace {
fn from(s: Cow<'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
}
}