Bug 1466609: Make clearing atoms slightly more ergonomic. r?xidorn draft
authorEmilio Cobos Álvarez <emilio@crisal.io>
Mon, 04 Jun 2018 20:09:51 +0200
changeset 803678 8fec0079b07d09ffa6cca9d9e7697b8d83ceebc2
parent 803677 50e5960bd977031a405a0cc00445ea27882586ff
child 803761 e1c4791fd793792139e3bda748d9ece4179cbd28
push id112164
push userbmo:emilio@crisal.io
push dateMon, 04 Jun 2018 18:13:16 +0000
reviewersxidorn
bugs1466609
milestone62.0a1
Bug 1466609: Make clearing atoms slightly more ergonomic. r?xidorn I prefer to do it this way because Atom has inline paths for static atoms and such. MozReview-Commit-ID: CFsBHl80KDY
servo/components/style/gecko_bindings/sugar/refptr.rs
servo/components/style/properties/gecko.mako.rs
--- a/servo/components/style/gecko_bindings/sugar/refptr.rs
+++ b/servo/components/style/gecko_bindings/sugar/refptr.rs
@@ -1,14 +1,15 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 //! A rust helper to ease the use of Gecko's refcounted types.
 
+use Atom;
 use gecko_bindings::{structs, bindings};
 use gecko_bindings::sugar::ownership::HasArcFFI;
 use servo_arc::Arc;
 use std::{fmt, mem, ptr};
 use std::marker::PhantomData;
 use std::ops::{Deref, DerefMut};
 
 /// Trait for all objects that have Addref() and Release
@@ -315,8 +316,22 @@ impl_threadsafe_refcount!(
     bindings::Gecko_AddRefSharedFontListArbitraryThread,
     bindings::Gecko_ReleaseSharedFontListArbitraryThread
 );
 impl_threadsafe_refcount!(
     structs::SheetLoadDataHolder,
     bindings::Gecko_AddRefSheetLoadDataHolderArbitraryThread,
     bindings::Gecko_ReleaseSheetLoadDataHolderArbitraryThread
 );
+
+#[inline]
+unsafe fn addref_atom(atom: *mut structs::nsAtom) {
+    mem::forget(Atom::from_raw(atom));
+}
+#[inline]
+unsafe fn release_atom(atom: *mut structs::nsAtom) {
+    let _ = Atom::from_addrefed(atom);
+}
+impl_threadsafe_refcount!(
+    structs::nsAtom,
+    addref_atom,
+    release_atom
+);
--- a/servo/components/style/properties/gecko.mako.rs
+++ b/servo/components/style/properties/gecko.mako.rs
@@ -3269,20 +3269,17 @@ fn static_assert() {
         use gecko_bindings::structs::nsCSSPropertyID::eCSSProperty_UNKNOWN;
 
         let v = v.into_iter();
 
         if v.len() != 0 {
             self.gecko.mTransitions.ensure_len(v.len());
             self.gecko.mTransitionPropertyCount = v.len() as u32;
             for (servo, gecko) in v.zip(self.gecko.mTransitions.iter_mut()) {
-                if !gecko.mUnknownProperty.mRawPtr.is_null() {
-                    unsafe { Atom::from_addrefed(gecko.mUnknownProperty.mRawPtr) };
-                    gecko.mUnknownProperty.mRawPtr = ptr::null_mut();
-                }
+                unsafe { gecko.mUnknownProperty.clear() };
 
                 match servo {
                     TransitionProperty::Unsupported(ident) => {
                         gecko.mProperty = eCSSProperty_UNKNOWN;
                         gecko.mUnknownProperty.mRawPtr = ident.0.into_addrefed();
                     },
                     TransitionProperty::Custom(name) => {
                         gecko.mProperty = eCSSPropertyExtra_variable;
@@ -3350,20 +3347,17 @@ fn static_assert() {
         use gecko_bindings::structs::nsCSSPropertyID::eCSSProperty_UNKNOWN;
         self.gecko.mTransitions.ensure_len(other.gecko.mTransitions.len());
 
         let count = other.gecko.mTransitionPropertyCount;
         self.gecko.mTransitionPropertyCount = count;
 
         for (index, transition) in self.gecko.mTransitions.iter_mut().enumerate().take(count as usize) {
             transition.mProperty = other.gecko.mTransitions[index].mProperty;
-            if !transition.mUnknownProperty.mRawPtr.is_null() {
-                unsafe { Atom::from_addrefed(transition.mUnknownProperty.mRawPtr) };
-                transition.mUnknownProperty.mRawPtr = ptr::null_mut();
-            }
+            unsafe { transition.mUnknownProperty.clear() };
             if transition.mProperty == eCSSProperty_UNKNOWN ||
                transition.mProperty == eCSSPropertyExtra_variable {
                 let atom = other.gecko.mTransitions[index].mUnknownProperty.mRawPtr;
                 debug_assert!(!atom.is_null());
                 transition.mUnknownProperty.mRawPtr = unsafe { Atom::from_raw(atom) }.into_addrefed();
             }
         }
     }