Add sugar for already_AddRefed and use it for conversion between Atom and nsIAtom pointer.

This commit is contained in:
Xidorn Quan 2017-05-21 13:06:31 +10:00
parent 1602edb04a
commit 2b1f7f6081
6 changed files with 75 additions and 25 deletions

View file

@ -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")
}
}

View file

@ -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;

View file

@ -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