Replace NonZero<*{const,mut} _> with std::ptr::NonNull

This commit is contained in:
Simon Sapin 2018-01-22 13:05:38 +01:00
parent 52eda6082f
commit 10ec5a2bb0
11 changed files with 42 additions and 50 deletions

View file

@ -65,7 +65,6 @@ mime_guess = "1.8.0"
mozjs = { version = "0.1.10", features = ["promises"]}
msg = {path = "../msg"}
net_traits = {path = "../net_traits"}
nonzero = {path = "../nonzero"}
num-traits = "0.1.32"
offscreen_gl_context = { version = "0.14", features = ["serde"] }
open = "1.1.1"

View file

@ -34,7 +34,6 @@ use js::jsapi::{JSObject, JSTracer, Heap};
use js::rust::GCMethods;
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use mitochondria::OnceCell;
use nonzero::NonZero;
use script_layout_interface::TrustedNodeAddress;
use std::cell::{Cell, UnsafeCell};
use std::default::Default;
@ -312,7 +311,7 @@ impl<'root, T: RootedReference<'root> + 'root> RootedReference<'root> for Option
/// This should only be used as a field in other DOM objects.
#[must_root]
pub struct Dom<T> {
ptr: NonZero<*const T>,
ptr: ptr::NonNull<T>,
}
// Dom<T> is similar to Rc<T>, in that it's not always clear how to avoid double-counting.
@ -339,7 +338,7 @@ impl<T: DomObject> Dom<T> {
pub fn from_ref(obj: &T) -> Dom<T> {
debug_assert!(thread_state::get().is_script());
Dom {
ptr: unsafe { NonZero::new_unchecked(&*obj) },
ptr: ptr::NonNull::from(obj),
}
}
}
@ -351,7 +350,7 @@ impl<T: DomObject> Deref for Dom<T> {
debug_assert!(thread_state::get().is_script());
// We can only have &Dom<T> from a rooted thing, so it's safe to deref
// it to &T.
unsafe { &*self.ptr.get() }
unsafe { &*self.ptr.as_ptr() }
}
}
@ -366,7 +365,7 @@ unsafe impl<T: DomObject> JSTraceable for Dom<T> {
trace_reflector(trc,
trace_info,
(*self.ptr.get()).reflector());
(*self.ptr.as_ptr()).reflector());
}
}
@ -374,7 +373,7 @@ unsafe impl<T: DomObject> JSTraceable for Dom<T> {
/// traits must be implemented on this.
#[allow_unrooted_interior]
pub struct LayoutDom<T> {
ptr: NonZero<*const T>,
ptr: ptr::NonNull<T>,
}
impl<T: Castable> LayoutDom<T> {
@ -384,9 +383,9 @@ impl<T: Castable> LayoutDom<T> {
T: DerivedFrom<U>
{
debug_assert!(thread_state::get().is_layout());
let ptr: *const T = self.ptr.get();
let ptr: *mut T = self.ptr.as_ptr();
LayoutDom {
ptr: unsafe { NonZero::new_unchecked(ptr as *const U) },
ptr: unsafe { ptr::NonNull::new_unchecked(ptr as *mut U) },
}
}
@ -397,9 +396,9 @@ impl<T: Castable> LayoutDom<T> {
debug_assert!(thread_state::get().is_layout());
unsafe {
if (*self.unsafe_get()).is::<U>() {
let ptr: *const T = self.ptr.get();
let ptr: *mut T = self.ptr.as_ptr();
Some(LayoutDom {
ptr: NonZero::new_unchecked(ptr as *const U),
ptr: ptr::NonNull::new_unchecked(ptr as *mut U),
})
} else {
None
@ -412,7 +411,7 @@ impl<T: DomObject> LayoutDom<T> {
/// Get the reflector.
pub unsafe fn get_jsobject(&self) -> *mut JSObject {
debug_assert!(thread_state::get().is_layout());
(*self.ptr.get()).reflector().get_jsobject().get()
(*self.ptr.as_ptr()).reflector().get_jsobject().get()
}
}
@ -420,7 +419,7 @@ impl<T> Copy for LayoutDom<T> {}
impl<T> PartialEq for Dom<T> {
fn eq(&self, other: &Dom<T>) -> bool {
self.ptr == other.ptr
self.ptr.as_ptr() == other.ptr.as_ptr()
}
}
@ -428,7 +427,7 @@ impl<T> Eq for Dom<T> {}
impl<T> PartialEq for LayoutDom<T> {
fn eq(&self, other: &LayoutDom<T>) -> bool {
self.ptr == other.ptr
self.ptr.as_ptr() == other.ptr.as_ptr()
}
}
@ -436,13 +435,13 @@ impl<T> Eq for LayoutDom<T> {}
impl<T> Hash for Dom<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.ptr.hash(state)
self.ptr.as_ptr().hash(state)
}
}
impl<T> Hash for LayoutDom<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.ptr.hash(state)
self.ptr.as_ptr().hash(state)
}
}
@ -474,7 +473,7 @@ impl LayoutDom<Node> {
debug_assert!(thread_state::get().is_layout());
let TrustedNodeAddress(addr) = inner;
LayoutDom {
ptr: NonZero::new_unchecked(addr as *const Node),
ptr: ptr::NonNull::new_unchecked(addr as *const Node as *mut Node),
}
}
}
@ -700,7 +699,7 @@ impl<T: DomObject> LayoutDom<T> {
/// this is unsafe is what necessitates the layout wrappers.)
pub unsafe fn unsafe_get(&self) -> *const T {
debug_assert!(thread_state::get().is_layout());
self.ptr.get()
self.ptr.as_ptr()
}
/// Returns a reference to the interior of this JS object. This method is
@ -708,7 +707,7 @@ impl<T: DomObject> LayoutDom<T> {
/// mutate DOM nodes.
pub fn get_for_script(&self) -> &T {
debug_assert!(thread_state::get().is_script());
unsafe { &*self.ptr.get() }
unsafe { &*self.ptr.as_ptr() }
}
}

View file

@ -18,10 +18,10 @@ use js::jsapi::{JSTracer, JS_GetReservedSlot, JS_SetReservedSlot};
use js::jsval::PrivateValue;
use libc::c_void;
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use nonzero::NonZero;
use std::cell::{Cell, UnsafeCell};
use std::mem;
use std::ops::{Deref, DerefMut, Drop};
use std::ptr;
/// The index of the slot wherein a pointer to the weak holder cell is
/// stored for weak-referenceable bindings. We use slot 1 for holding it,
@ -30,9 +30,10 @@ use std::ops::{Deref, DerefMut, Drop};
pub const DOM_WEAK_SLOT: u32 = 1;
/// A weak reference to a JS-managed DOM object.
#[allow(unrooted_must_root)]
#[allow_unrooted_interior]
pub struct WeakRef<T: WeakReferenceable> {
ptr: NonZero<*mut WeakBox<T>>,
ptr: ptr::NonNull<WeakBox<T>>,
}
/// The inner box of weak references, public for the finalization in codegen.
@ -42,7 +43,7 @@ pub struct WeakBox<T: WeakReferenceable> {
/// have already been set to `None`. The pointee contributes one to the count.
pub count: Cell<usize>,
/// The pointer to the JS-managed object, set to None when it is collected.
pub value: Cell<Option<NonZero<*const T>>>,
pub value: Cell<Option<ptr::NonNull<T>>>,
}
/// Trait implemented by weak-referenceable interfaces.
@ -58,7 +59,7 @@ pub trait WeakReferenceable: DomObject + Sized {
trace!("Creating new WeakBox holder for {:p}.", self);
ptr = Box::into_raw(Box::new(WeakBox {
count: Cell::new(1),
value: Cell::new(Some(NonZero::new_unchecked(self))),
value: Cell::new(Some(ptr::NonNull::from(self))),
}));
JS_SetReservedSlot(object, DOM_WEAK_SLOT, PrivateValue(ptr as *const c_void));
}
@ -70,7 +71,7 @@ pub trait WeakReferenceable: DomObject + Sized {
new_count);
box_.count.set(new_count);
WeakRef {
ptr: NonZero::new_unchecked(ptr),
ptr: ptr::NonNull::new_unchecked(ptr),
}
}
}
@ -86,21 +87,21 @@ impl<T: WeakReferenceable> WeakRef<T> {
/// DomRoot a weak reference. Returns `None` if the object was already collected.
pub fn root(&self) -> Option<DomRoot<T>> {
unsafe { &*self.ptr.get() }.value.get().map(|ptr| unsafe {
DomRoot::from_ref(&*ptr.get())
unsafe { &*self.ptr.as_ptr() }.value.get().map(|ptr| unsafe {
DomRoot::from_ref(&*ptr.as_ptr())
})
}
/// Return whether the weakly-referenced object is still alive.
pub fn is_alive(&self) -> bool {
unsafe { &*self.ptr.get() }.value.get().is_some()
unsafe { &*self.ptr.as_ptr() }.value.get().is_some()
}
}
impl<T: WeakReferenceable> Clone for WeakRef<T> {
fn clone(&self) -> WeakRef<T> {
unsafe {
let box_ = &*self.ptr.get();
let box_ = &*self.ptr.as_ptr();
let new_count = box_.count.get() + 1;
box_.count.set(new_count);
WeakRef {
@ -119,7 +120,8 @@ impl<T: WeakReferenceable> MallocSizeOf for WeakRef<T> {
impl<T: WeakReferenceable> PartialEq for WeakRef<T> {
fn eq(&self, other: &Self) -> bool {
unsafe {
(*self.ptr.get()).value.get() == (*other.ptr.get()).value.get()
(*self.ptr.as_ptr()).value.get().map(ptr::NonNull::as_ptr) ==
(*other.ptr.as_ptr()).value.get().map(ptr::NonNull::as_ptr)
}
}
}
@ -127,8 +129,8 @@ impl<T: WeakReferenceable> PartialEq for WeakRef<T> {
impl<T: WeakReferenceable> PartialEq<T> for WeakRef<T> {
fn eq(&self, other: &T) -> bool {
unsafe {
match (*self.ptr.get()).value.get() {
Some(ptr) => ptr.get() == other,
match self.ptr.as_ref().value.get() {
Some(ptr) => ptr::eq(ptr.as_ptr(), other),
None => false,
}
}
@ -145,7 +147,7 @@ impl<T: WeakReferenceable> Drop for WeakRef<T> {
fn drop(&mut self) {
unsafe {
let (count, value) = {
let weak_box = &*self.ptr.get();
let weak_box = &*self.ptr.as_ptr();
assert!(weak_box.count.get() > 0);
let count = weak_box.count.get() - 1;
weak_box.count.set(count);
@ -153,7 +155,7 @@ impl<T: WeakReferenceable> Drop for WeakRef<T> {
};
if count == 0 {
assert!(value.is_none());
mem::drop(Box::from_raw(self.ptr.get()));
mem::drop(Box::from_raw(self.ptr.as_ptr()));
}
}
}

View file

@ -68,7 +68,6 @@ extern crate mitochondria;
extern crate mozjs as js;
extern crate msg;
extern crate net_traits;
extern crate nonzero;
extern crate num_traits;
extern crate offscreen_gl_context;
extern crate open;