Skip to content

Commit 3ae2700

Browse files
committed
Bring back the Namespace newtype.
1 parent 676a110 commit 3ae2700

File tree

4 files changed

+84
-21
lines changed

4 files changed

+84
-21
lines changed

components/style/gecko_selector_impl.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use element_state::ElementState;
66
use selector_impl::PseudoElementCascadeType;
77
use selector_impl::{attr_exists_selector_is_shareable, attr_equals_selector_is_shareable};
88
use selectors::parser::{ParserContext, SelectorImpl, AttrSelector};
9-
use string_cache::{Atom, WeakAtom};
9+
use string_cache::{Atom, WeakAtom, Namespace, WeakNamespace};
1010
use stylesheets::Stylesheet;
1111

1212
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -154,8 +154,8 @@ impl SelectorImpl for GeckoSelectorImpl {
154154
type Identifier = Atom;
155155
type ClassName = Atom;
156156
type LocalName = Atom;
157-
type Namespace = Atom;
158-
type BorrowedNamespace = WeakAtom;
157+
type Namespace = Namespace;
158+
type BorrowedNamespace = WeakNamespace;
159159
type BorrowedLocalName = WeakAtom;
160160

161161
type PseudoElement = PseudoElement;

ports/geckolib/string_cache/lib.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,9 @@ use std::slice;
3030

3131
#[macro_use]
3232
pub mod atom_macro;
33+
pub mod namespace;
3334

34-
#[macro_export]
35-
macro_rules! ns {
36-
() => { atom!("") }
37-
}
38-
39-
pub type Namespace = Atom;
40-
41-
#[allow(non_snake_case)]
42-
#[inline]
43-
pub fn Namespace(atom: Atom) -> Atom {
44-
atom
45-
}
35+
pub use namespace::{Namespace, WeakNamespace};
4636

4737
/// A strong reference to a Gecko atom.
4838
#[derive(PartialEq, Eq)]
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
use gecko_bindings::structs::nsIAtom;
6+
use selectors::bloom::BloomHash;
7+
use std::borrow::Borrow;
8+
use std::ops::Deref;
9+
use {Atom, WeakAtom};
10+
11+
#[macro_export]
12+
macro_rules! ns {
13+
() => { $crate::Namespace(atom!("")) }
14+
}
15+
16+
#[derive(Debug, PartialEq, Eq, Clone, Default, Hash)]
17+
pub struct Namespace(pub Atom);
18+
pub struct WeakNamespace(WeakAtom);
19+
20+
impl Deref for Namespace {
21+
type Target = WeakNamespace;
22+
23+
#[inline]
24+
fn deref(&self) -> &WeakNamespace {
25+
let weak: *const WeakAtom = &*self.0;
26+
unsafe {
27+
&*(weak as *const WeakNamespace)
28+
}
29+
}
30+
}
31+
32+
impl Borrow<WeakNamespace> for Namespace {
33+
#[inline]
34+
fn borrow(&self) -> &WeakNamespace {
35+
self
36+
}
37+
}
38+
39+
impl WeakNamespace {
40+
#[inline]
41+
pub unsafe fn new<'a>(atom: *mut nsIAtom) -> &'a Self {
42+
&*(atom as *const WeakNamespace)
43+
}
44+
45+
#[inline]
46+
pub fn clone(&self) -> Namespace {
47+
Namespace(self.0.clone())
48+
}
49+
}
50+
51+
impl Eq for WeakNamespace {}
52+
impl PartialEq for WeakNamespace {
53+
#[inline]
54+
fn eq(&self, other: &Self) -> bool {
55+
let weak: *const WeakNamespace = self;
56+
let other: *const WeakNamespace = other;
57+
weak == other
58+
}
59+
}
60+
61+
impl BloomHash for Namespace {
62+
#[inline]
63+
fn bloom_hash(&self) -> u32 {
64+
self.0.get_hash()
65+
}
66+
}
67+
68+
impl BloomHash for WeakNamespace {
69+
#[inline]
70+
fn bloom_hash(&self) -> u32 {
71+
self.0.get_hash()
72+
}
73+
}

ports/geckolib/wrapper.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use gecko_bindings::bindings::{Gecko_LocalName, Gecko_Namespace, Gecko_NodeIsEle
2626
use gecko_bindings::bindings::{RawGeckoDocument, RawGeckoElement, RawGeckoNode};
2727
use gecko_bindings::structs::{NODE_HAS_DIRTY_DESCENDANTS_FOR_SERVO, NODE_IS_DIRTY_FOR_SERVO};
2828
use gecko_bindings::structs::{nsIAtom, nsChangeHint, nsStyleContext};
29-
use gecko_string_cache::{Atom, Namespace, WeakAtom};
29+
use gecko_string_cache::{Atom, Namespace, WeakAtom, WeakNamespace};
3030
use glue::GeckoDeclarationBlock;
3131
use libc::uintptr_t;
3232
use selectors::Element;
@@ -433,7 +433,7 @@ impl<'le> TElement for GeckoElement<'le> {
433433
fn has_attr(&self, namespace: &Namespace, attr: &Atom) -> bool {
434434
unsafe {
435435
bindings::Gecko_HasAttr(self.element,
436-
namespace.as_ptr(),
436+
namespace.0.as_ptr(),
437437
attr.as_ptr())
438438
}
439439
}
@@ -442,7 +442,7 @@ impl<'le> TElement for GeckoElement<'le> {
442442
fn attr_equals(&self, namespace: &Namespace, attr: &Atom, val: &Atom) -> bool {
443443
unsafe {
444444
bindings::Gecko_AttrEquals(self.element,
445-
namespace.as_ptr(),
445+
namespace.0.as_ptr(),
446446
attr.as_ptr(),
447447
val.as_ptr(),
448448
/* ignoreCase = */ false)
@@ -506,9 +506,9 @@ impl<'le> ::selectors::Element for GeckoElement<'le> {
506506
}
507507
}
508508

509-
fn get_namespace(&self) -> &WeakAtom {
509+
fn get_namespace(&self) -> &WeakNamespace {
510510
unsafe {
511-
WeakAtom::new(Gecko_Namespace(self.element))
511+
WeakNamespace::new(Gecko_Namespace(self.element))
512512
}
513513
}
514514

@@ -577,7 +577,7 @@ impl AttrSelectorHelpers for AttrSelector<GeckoSelectorImpl> {
577577
fn ns_or_null(&self) -> *mut nsIAtom {
578578
match self.namespace {
579579
NamespaceConstraint::Any => ptr::null_mut(),
580-
NamespaceConstraint::Specific(ref ns) => ns.as_ptr(),
580+
NamespaceConstraint::Specific(ref ns) => ns.0.as_ptr(),
581581
}
582582
}
583583

0 commit comments

Comments
 (0)