Source code

Revision control

Other Tools

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 https://mozilla.org/MPL/2.0/. */
4
5
//! A type to represent a namespace.
6
7
use crate::gecko_bindings::structs::nsAtom;
8
use crate::string_cache::{Atom, WeakAtom};
9
use precomputed_hash::PrecomputedHash;
10
use std::borrow::Borrow;
11
use std::fmt;
12
use std::ops::Deref;
13
14
/// In Gecko namespaces are just regular atoms, so this is a simple macro to
15
/// forward one macro to the other.
16
#[macro_export]
17
macro_rules! ns {
18
() => {
19
$crate::string_cache::Namespace(atom!(""))
20
};
21
($s:tt) => {
22
$crate::string_cache::Namespace(atom!($s))
23
};
24
}
25
26
/// A Gecko namespace is just a wrapped atom.
27
#[derive(Clone, Debug, Default, Eq, Hash, MallocSizeOf, PartialEq, ToComputedValue, ToResolvedValue, ToShmem)]
28
#[repr(transparent)]
29
pub struct Namespace(pub Atom);
30
31
impl PrecomputedHash for Namespace {
32
#[inline]
33
fn precomputed_hash(&self) -> u32 {
34
self.0.precomputed_hash()
35
}
36
}
37
38
/// A Gecko WeakNamespace is a wrapped WeakAtom.
39
#[derive(Hash)]
40
pub struct WeakNamespace(WeakAtom);
41
42
impl Deref for WeakNamespace {
43
type Target = WeakAtom;
44
45
#[inline]
46
fn deref(&self) -> &WeakAtom {
47
&self.0
48
}
49
}
50
51
impl Deref for Namespace {
52
type Target = WeakNamespace;
53
54
#[inline]
55
fn deref(&self) -> &WeakNamespace {
56
let weak: *const WeakAtom = &*self.0;
57
unsafe { &*(weak as *const WeakNamespace) }
58
}
59
}
60
61
impl<'a> From<&'a str> for Namespace {
62
fn from(s: &'a str) -> Self {
63
Namespace(Atom::from(s))
64
}
65
}
66
67
impl fmt::Display for Namespace {
68
fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result {
69
self.0.fmt(w)
70
}
71
}
72
73
impl Borrow<WeakNamespace> for Namespace {
74
#[inline]
75
fn borrow(&self) -> &WeakNamespace {
76
self
77
}
78
}
79
80
impl WeakNamespace {
81
/// Trivially construct a WeakNamespace.
82
#[inline]
83
pub unsafe fn new<'a>(atom: *mut nsAtom) -> &'a Self {
84
&*(atom as *const WeakNamespace)
85
}
86
87
/// Clone this WeakNamespace to obtain a strong reference to the same
88
/// underlying namespace.
89
#[inline]
90
pub fn clone(&self) -> Namespace {
91
Namespace(self.0.clone())
92
}
93
}
94
95
impl Eq for WeakNamespace {}
96
impl PartialEq for WeakNamespace {
97
#[inline]
98
fn eq(&self, other: &Self) -> bool {
99
let weak: *const WeakNamespace = self;
100
let other: *const WeakNamespace = other;
101
weak == other
102
}
103
}