style: Make rust Atom use NonZeroUsize.

At first I thought this was going to enable simplifications in the selector
parser (to simplify the attribute selector setup), but I couldn't end up
shrinking the layout enough.

However this should help with bug 1559076, which returns Option<Atom>, and it
was easy to write.

Differential Revision: https://phabricator.services.mozilla.com/D53766
This commit is contained in:
Emilio Cobos Álvarez 2019-11-20 05:46:56 +00:00
parent fe93be82d2
commit e48c4b88f1
No known key found for this signature in database
GPG key ID: E1152D0994E4BF8A

View file

@ -26,6 +26,7 @@ use std::fmt::{self, Write};
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::iter::Cloned; use std::iter::Cloned;
use std::mem::{self, ManuallyDrop}; use std::mem::{self, ManuallyDrop};
use std::num::NonZeroUsize;
use std::ops::Deref; use std::ops::Deref;
use std::{slice, str}; use std::{slice, str};
use style_traits::SpecifiedValueInfo; use style_traits::SpecifiedValueInfo;
@ -54,7 +55,7 @@ macro_rules! local_name {
/// or an offset from gGkAtoms to the nsStaticAtom object. /// or an offset from gGkAtoms to the nsStaticAtom object.
#[derive(Eq, PartialEq)] #[derive(Eq, PartialEq)]
#[repr(C)] #[repr(C)]
pub struct Atom(usize); pub struct Atom(NonZeroUsize);
/// An atom *without* a strong reference. /// An atom *without* a strong reference.
/// ///
@ -101,9 +102,9 @@ impl Deref for Atom {
fn deref(&self) -> &WeakAtom { fn deref(&self) -> &WeakAtom {
unsafe { unsafe {
let addr = if self.is_static() { let addr = if self.is_static() {
(&gGkAtoms as *const _ as usize) + (self.0 >> 1) (&gGkAtoms as *const _ as usize) + (self.0.get() >> 1)
} else { } else {
self.0 self.0.get()
}; };
debug_assert!(!self.is_static() || valid_static_atom_addr(addr)); debug_assert!(!self.is_static() || valid_static_atom_addr(addr));
WeakAtom::new(addr as *const nsAtom) WeakAtom::new(addr as *const nsAtom)
@ -341,29 +342,29 @@ impl fmt::Display for WeakAtom {
} }
#[inline] #[inline]
unsafe fn make_handle(ptr: *const nsAtom) -> usize { unsafe fn make_handle(ptr: *const nsAtom) -> NonZeroUsize {
debug_assert!(!ptr.is_null()); debug_assert!(!ptr.is_null());
if !WeakAtom::new(ptr).is_static() { if !WeakAtom::new(ptr).is_static() {
ptr as usize NonZeroUsize::new_unchecked(ptr as usize)
} else { } else {
make_static_handle(ptr as *mut nsStaticAtom) make_static_handle(ptr as *mut nsStaticAtom)
} }
} }
#[inline] #[inline]
unsafe fn make_static_handle(ptr: *const nsStaticAtom) -> usize { unsafe fn make_static_handle(ptr: *const nsStaticAtom) -> NonZeroUsize {
// FIXME(heycam): Use offset_from once it's stabilized. // FIXME(heycam): Use offset_from once it's stabilized.
// https://github.com/rust-lang/rust/issues/41079 // https://github.com/rust-lang/rust/issues/41079
debug_assert!(valid_static_atom_addr(ptr as usize)); debug_assert!(valid_static_atom_addr(ptr as usize));
let base = &gGkAtoms as *const _; let base = &gGkAtoms as *const _;
let offset = ptr as usize - base as usize; let offset = ptr as usize - base as usize;
(offset << 1) | 1 NonZeroUsize::new_unchecked((offset << 1) | 1)
} }
impl Atom { impl Atom {
#[inline] #[inline]
fn is_static(&self) -> bool { fn is_static(&self) -> bool {
self.0 & 1 == 1 self.0.get() & 1 == 1
} }
/// Execute a callback with the atom represented by `ptr`. /// Execute a callback with the atom represented by `ptr`.