stylo: Add sugar over the bitfield accessors in nsIAtom.

This commit is contained in:
Emilio Cobos Álvarez 2016-08-12 19:10:23 -07:00
parent 24168f87eb
commit 2b3c684b37
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
2 changed files with 25 additions and 6 deletions

View file

@ -264,7 +264,7 @@ pub extern "C" fn Servo_GetComputedValuesForAnonymousBox(parent_style_or_null: *
let data = PerDocumentStyleData::borrow_mut_from_raw(raw_data); let data = PerDocumentStyleData::borrow_mut_from_raw(raw_data);
data.flush_stylesheets(); data.flush_stylesheets();
let atom = unsafe { Atom::from_static(pseudo_tag) }; let atom = Atom::from(pseudo_tag);
let pseudo = PseudoElement::from_atom_unchecked(atom, /* anon_box = */ true); let pseudo = PseudoElement::from_atom_unchecked(atom, /* anon_box = */ true);
type Helpers = ArcHelpers<ServoComputedValues, ComputedValues>; type Helpers = ArcHelpers<ServoComputedValues, ComputedValues>;
@ -293,7 +293,7 @@ pub extern "C" fn Servo_GetComputedValuesForPseudoElement(parent_style: *mut Ser
} }
}; };
let atom = unsafe { Atom::from_static(pseudo_tag) }; let atom = Atom::from(pseudo_tag);
let pseudo = PseudoElement::from_atom_unchecked(atom, /* anon_box = */ false); let pseudo = PseudoElement::from_atom_unchecked(atom, /* anon_box = */ false);
// The stylist consumes stylesheets lazily. // The stylist consumes stylesheets lazily.

View file

@ -128,6 +128,20 @@ impl WeakAtom {
String::from_utf16(self.as_slice()).unwrap() String::from_utf16(self.as_slice()).unwrap()
} }
#[inline]
pub fn is_static(&self) -> bool {
unsafe {
(*self.as_ptr()).mIsStatic() != 0
}
}
#[inline]
pub fn len(&self) -> u32 {
unsafe {
(*self.as_ptr()).mLength()
}
}
#[inline] #[inline]
pub fn as_ptr(&self) -> *mut nsIAtom { pub fn as_ptr(&self) -> *mut nsIAtom {
let const_ptr: *const nsIAtom = &self.0; let const_ptr: *const nsIAtom = &self.0;
@ -158,7 +172,7 @@ impl Atom {
} }
#[inline] #[inline]
pub unsafe fn from_static(ptr: *mut nsIAtom) -> Self { unsafe fn from_static(ptr: *mut nsIAtom) -> Self {
Atom(ptr as *mut WeakAtom) Atom(ptr as *mut WeakAtom)
} }
} }
@ -199,8 +213,10 @@ impl Clone for Atom {
impl Drop for Atom { impl Drop for Atom {
#[inline] #[inline]
fn drop(&mut self) { fn drop(&mut self) {
unsafe { if !self.is_static() {
Gecko_ReleaseAtom(self.as_ptr()); unsafe {
Gecko_ReleaseAtom(self.as_ptr());
}
} }
} }
} }
@ -281,8 +297,11 @@ impl From<String> for Atom {
impl From<*mut nsIAtom> for Atom { impl From<*mut nsIAtom> for Atom {
#[inline] #[inline]
fn from(ptr: *mut nsIAtom) -> Atom { fn from(ptr: *mut nsIAtom) -> Atom {
debug_assert!(!ptr.is_null());
unsafe { unsafe {
Gecko_AddRefAtom(ptr); if (*ptr).mIsStatic() == 0 {
Gecko_AddRefAtom(ptr);
}
Atom(WeakAtom::new(ptr)) Atom(WeakAtom::new(ptr))
} }
} }