diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2017-05-21 05:28:58 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-21 05:28:58 -0500 |
commit | 3d40b516c8ac6e946cadace2378581cbac7b1e1b (patch) | |
tree | f279772930907e25c6f515074e4e398c765765e1 /components | |
parent | 255387a915ef83db6b11d976f89c10b518c97487 (diff) | |
parent | e30e676658a869b345b2ce1ef1619b2f78f4824b (diff) | |
download | servo-3d40b516c8ac6e946cadace2378581cbac7b1e1b.tar.gz servo-3d40b516c8ac6e946cadace2378581cbac7b1e1b.zip |
Auto merge of #16976 - upsuper:bug1366247, r=nox
Add sugar for already_AddRefed
This is Servo side change of [bug 1366247](https://bugzilla.mozilla.org/show_bug.cgi?id=1366247).
<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/16976)
<!-- Reviewable:end -->
Diffstat (limited to 'components')
-rw-r--r-- | components/style/gecko/generated/bindings.rs | 7 | ||||
-rw-r--r-- | components/style/gecko/wrapper.rs | 7 | ||||
-rw-r--r-- | components/style/gecko_bindings/sugar/already_addrefed.rs | 48 | ||||
-rw-r--r-- | components/style/gecko_bindings/sugar/mod.rs | 1 | ||||
-rw-r--r-- | components/style/gecko_bindings/sugar/ns_css_value.rs | 2 | ||||
-rw-r--r-- | components/style/gecko_string_cache/mod.rs | 36 |
6 files changed, 75 insertions, 26 deletions
diff --git a/components/style/gecko/generated/bindings.rs b/components/style/gecko/generated/bindings.rs index c88d389bd3b..2987408705d 100644 --- a/components/style/gecko/generated/bindings.rs +++ b/components/style/gecko/generated/bindings.rs @@ -1,13 +1,13 @@ /* automatically generated by rust-bindgen */ pub use nsstring::{nsACString, nsAString, nsString, nsStringRepr}; -use gecko_bindings::structs::nsTArray; type nsACString_internal = nsACString; type nsAString_internal = nsAString; use gecko_bindings::structs::mozilla::css::GridTemplateAreasValue; use gecko_bindings::structs::mozilla::css::ImageValue; use gecko_bindings::structs::mozilla::css::URLValue; use gecko_bindings::structs::mozilla::Side; +use gecko_bindings::structs::already_AddRefed; use gecko_bindings::structs::RawGeckoAnimationPropertySegment; use gecko_bindings::structs::RawGeckoComputedTiming; use gecko_bindings::structs::RawGeckoCSSPropertyIDList; @@ -190,6 +190,7 @@ unsafe impl Sync for nsStyleVisibility {} use gecko_bindings::structs::nsStyleXUL; unsafe impl Send for nsStyleXUL {} unsafe impl Sync for nsStyleXUL {} +use gecko_bindings::structs::nsTArray; use gecko_bindings::structs::nsTimingFunction; use gecko_bindings::structs::nscolor; use gecko_bindings::structs::nscoord; @@ -1237,7 +1238,7 @@ extern "C" { } extern "C" { pub fn Gecko_CSSValue_SetAtomIdent(css_value: nsCSSValueBorrowedMut, - atom: *mut nsIAtom); + atom: already_AddRefed<nsIAtom>); } extern "C" { pub fn Gecko_CSSValue_SetArray(css_value: nsCSSValueBorrowedMut, @@ -1981,7 +1982,7 @@ extern "C" { } extern "C" { pub fn Servo_KeyframesRule_SetName(rule: RawServoKeyframesRuleBorrowed, - name: *mut nsIAtom); + name: already_AddRefed<nsIAtom>); } extern "C" { pub fn Servo_KeyframesRule_GetCount(rule: RawServoKeyframesRuleBorrowed) diff --git a/components/style/gecko/wrapper.rs b/components/style/gecko/wrapper.rs index e043ff57223..71e26e104a4 100644 --- a/components/style/gecko/wrapper.rs +++ b/components/style/gecko/wrapper.rs @@ -54,6 +54,7 @@ use gecko_bindings::structs::ELEMENT_HAS_SNAPSHOT; use gecko_bindings::structs::EffectCompositor_CascadeLevel as CascadeLevel; use gecko_bindings::structs::NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE; use gecko_bindings::structs::NODE_IS_NATIVE_ANONYMOUS; +use gecko_bindings::structs::already_AddRefed; use gecko_bindings::sugar::ownership::HasArcFFI; use logical_geometry::WritingMode; use media_queries::Device; @@ -1074,13 +1075,13 @@ impl<'le> PresentationalHintsSynthesizer for GeckoElement<'le> { // // http://www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#language let ptr = unsafe { - bindings::Gecko_GetXMLLangValue(self.0) + already_AddRefed::new(bindings::Gecko_GetXMLLangValue(self.0)) }; - if !ptr.is_null() { + if let Some(ptr) = ptr { let global_style_data = &*GLOBAL_STYLE_DATA; let pdb = PropertyDeclarationBlock::with_one( - PropertyDeclaration::XLang(SpecifiedLang(unsafe { Atom::from_addrefed(ptr) })), + PropertyDeclaration::XLang(SpecifiedLang(ptr.into())), Importance::Normal ); let arc = Arc::new(global_style_data.shared_lock.wrap(pdb)); diff --git a/components/style/gecko_bindings/sugar/already_addrefed.rs b/components/style/gecko_bindings/sugar/already_addrefed.rs new file mode 100644 index 00000000000..01e209fc81f --- /dev/null +++ b/components/style/gecko_bindings/sugar/already_addrefed.rs @@ -0,0 +1,48 @@ +/* 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/. */ + +//! little helpers for `already_AddRefed`. + +use gecko_bindings::structs::already_AddRefed; +use std::marker::PhantomData; +use std::mem; + +impl<T> already_AddRefed<T> { + /// Create an already_AddRefed from an addrefed pointer. + #[inline] + pub unsafe fn new(ptr: *mut T) -> Option<Self> { + if !ptr.is_null() { + Some(Self::new_unchecked(ptr)) + } else { + None + } + } + + /// Create an already_AddRefed from an non-nullable addrefed pointer. + #[inline] + pub unsafe fn new_unchecked(ptr: *mut T) -> Self { + debug_assert!(!ptr.is_null()); + already_AddRefed { + mRawPtr: ptr, + _phantom_0: PhantomData, + } + } + + /// Take the addrefed pointer from this struct. + #[inline] + pub fn take(self) -> *mut T { + let ptr = self.mRawPtr; + mem::forget(self); + ptr + } +} + +#[cfg(debug_assertions)] +impl<T> Drop for already_AddRefed<T> { + fn drop(&mut self) { + // We really should instead mark already_AddRefed must_use, but + // we cannot currently, which is servo/rust-bindgen#710. + unreachable!("Destructor shouldn't be called, otherwise we are leaking") + } +} diff --git a/components/style/gecko_bindings/sugar/mod.rs b/components/style/gecko_bindings/sugar/mod.rs index f241c94810b..112a8d8e4fa 100644 --- a/components/style/gecko_bindings/sugar/mod.rs +++ b/components/style/gecko_bindings/sugar/mod.rs @@ -4,6 +4,7 @@ //! Rust sugar and convenience methods for Gecko types. +mod already_addrefed; mod ns_com_ptr; mod ns_compatibility; mod ns_css_shadow_array; diff --git a/components/style/gecko_bindings/sugar/ns_css_value.rs b/components/style/gecko_bindings/sugar/ns_css_value.rs index 0f91fc4f27a..13c3c5a825c 100644 --- a/components/style/gecko_bindings/sugar/ns_css_value.rs +++ b/components/style/gecko_bindings/sugar/ns_css_value.rs @@ -152,7 +152,7 @@ impl nsCSSValue { /// Set to an atom identifier value pub fn set_atom_ident(&mut self, s: Atom) { - unsafe { bindings::Gecko_CSSValue_SetAtomIdent(self, s.into_addrefed()) } + unsafe { bindings::Gecko_CSSValue_SetAtomIdent(self, s.into()) } } /// Set to a font format diff --git a/components/style/gecko_string_cache/mod.rs b/components/style/gecko_string_cache/mod.rs index fa89f5eab48..08933c053ff 100644 --- a/components/style/gecko_string_cache/mod.rs +++ b/components/style/gecko_string_cache/mod.rs @@ -10,7 +10,7 @@ use gecko_bindings::bindings::Gecko_AddRefAtom; use gecko_bindings::bindings::Gecko_Atomize; use gecko_bindings::bindings::Gecko_Atomize16; use gecko_bindings::bindings::Gecko_ReleaseAtom; -use gecko_bindings::structs::nsIAtom; +use gecko_bindings::structs::{already_AddRefed, nsIAtom}; use nsstring::nsAString; use precomputed_hash::PrecomputedHash; use std::ascii::AsciiExt; @@ -219,24 +219,6 @@ impl Atom { atom } - /// Creates an atom from a dynamic atom pointer that has already had AddRef - /// called on it. - #[inline] - pub unsafe fn from_addrefed(ptr: *mut nsIAtom) -> Self { - debug_assert!(!ptr.is_null()); - unsafe { - Atom(WeakAtom::new(ptr)) - } - } - - /// Convert this atom into an addrefed nsIAtom pointer. - #[inline] - pub fn into_addrefed(self) -> *mut nsIAtom { - let ptr = self.as_ptr(); - mem::forget(self); - ptr - } - /// Return whether two atoms are ASCII-case-insensitive matches pub fn eq_ignore_ascii_case(&self, other: &Self) -> bool { let a = self.as_slice(); @@ -358,3 +340,19 @@ impl From<*mut nsIAtom> for Atom { } } } + +impl From<already_AddRefed<nsIAtom>> for Atom { + #[inline] + fn from(ptr: already_AddRefed<nsIAtom>) -> Atom { + unsafe { Atom(WeakAtom::new(ptr.take())) } + } +} + +impl From<Atom> for already_AddRefed<nsIAtom> { + #[inline] + fn from(atom: Atom) -> already_AddRefed<nsIAtom> { + let ptr = atom.as_ptr(); + mem::forget(atom); + unsafe { already_AddRefed::new_unchecked(ptr) } + } +} |