tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

namespace.rs (2460B)


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