Skip to content

Commit

Permalink
Replace NonZero<*{const,mut} _> with std::ptr::NonNull
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Jan 22, 2018
1 parent 52eda60 commit 10ec5a2
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 50 deletions.
3 changes: 0 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/layout/wrapper.rs
Expand Up @@ -67,7 +67,7 @@ pub trait GetRawData {
impl<T: GetLayoutData> GetRawData for T {
fn get_raw_data(&self) -> Option<&StyleAndLayoutData> {
self.get_style_and_layout_data().map(|opaque| {
let container = opaque.ptr.get() as *mut StyleAndLayoutData;
let container = opaque.ptr.as_ptr() as *mut StyleAndLayoutData;
unsafe { &*container }
})
}
Expand Down
3 changes: 1 addition & 2 deletions components/layout_thread/Cargo.toml
Expand Up @@ -10,7 +10,7 @@ name = "layout_thread"
path = "lib.rs"

[features]
unstable = ["parking_lot/nightly", "nonzero/unstable"]
unstable = ["parking_lot/nightly"]

[dependencies]
app_units = "0.6"
Expand All @@ -30,7 +30,6 @@ malloc_size_of = { path = "../malloc_size_of" }
metrics = {path = "../metrics"}
msg = {path = "../msg"}
net_traits = {path = "../net_traits"}
nonzero = {path = "../nonzero"}
parking_lot = "0.4"
profile_traits = {path = "../profile_traits"}
range = {path = "../range"}
Expand Down
10 changes: 5 additions & 5 deletions components/layout_thread/dom_wrapper.rs
Expand Up @@ -36,7 +36,6 @@ use html5ever::{LocalName, Namespace};
use layout::data::StyleAndLayoutData;
use layout::wrapper::GetRawData;
use msg::constellation_msg::{BrowsingContextId, PipelineId};
use nonzero::NonZero;
use range::Range;
use script::layout_exports::{CharacterDataTypeId, ElementTypeId, HTMLElementTypeId, NodeTypeId};
use script::layout_exports::{Document, Element, Node, Text};
Expand All @@ -59,6 +58,7 @@ use std::fmt;
use std::fmt::Debug;
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use std::ptr::NonNull;
use std::sync::atomic::Ordering;
use style::CaseSensitivityExt;
use style::applicable_declarations::ApplicableDeclarationBlock;
Expand All @@ -76,7 +76,7 @@ use style::shared_lock::{SharedRwLock as StyleSharedRwLock, Locked as StyleLocke
use style::str::is_whitespace;

pub unsafe fn drop_style_and_layout_data(data: OpaqueStyleAndLayoutData) {
let ptr = data.ptr.get() as *mut StyleData;
let ptr = data.ptr.as_ptr() as *mut StyleData;
let non_opaque: *mut StyleAndLayoutData = ptr as *mut _;
let _ = Box::from_raw(non_opaque);
}
Expand Down Expand Up @@ -229,7 +229,7 @@ impl<'ln> LayoutNode for ServoLayoutNode<'ln> {
let ptr: *mut StyleAndLayoutData =
Box::into_raw(Box::new(StyleAndLayoutData::new()));
let opaque = OpaqueStyleAndLayoutData {
ptr: NonZero::new_unchecked(ptr as *mut StyleData),
ptr: NonNull::new_unchecked(ptr as *mut StyleData),
};
self.init_style_and_layout_data(opaque);
};
Expand Down Expand Up @@ -450,7 +450,7 @@ impl<'le> TElement for ServoLayoutElement<'le> {
fn get_data(&self) -> Option<&AtomicRefCell<ElementData>> {
unsafe {
self.get_style_and_layout_data().map(|d| {
&(*(d.ptr.get() as *mut StyleData)).element_data
&(*(d.ptr.as_ptr() as *mut StyleData)).element_data
})
}
}
Expand Down Expand Up @@ -575,7 +575,7 @@ impl<'le> ServoLayoutElement<'le> {

fn get_style_data(&self) -> Option<&StyleData> {
unsafe {
self.get_style_and_layout_data().map(|d| &*(d.ptr.get() as *mut StyleData))
self.get_style_and_layout_data().map(|d| &*(d.ptr.as_ptr() as *mut StyleData))
}
}

Expand Down
1 change: 0 additions & 1 deletion components/layout_thread/lib.rs
Expand Up @@ -28,7 +28,6 @@ extern crate malloc_size_of;
extern crate metrics;
extern crate msg;
extern crate net_traits;
extern crate nonzero;
extern crate parking_lot;
#[macro_use]
extern crate profile_traits;
Expand Down
1 change: 0 additions & 1 deletion components/script/Cargo.toml
Expand Up @@ -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"
Expand Down
35 changes: 17 additions & 18 deletions components/script/dom/bindings/root.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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),
}
}
}
Expand All @@ -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() }
}
}

Expand All @@ -366,15 +365,15 @@ unsafe impl<T: DomObject> JSTraceable for Dom<T> {

trace_reflector(trc,
trace_info,
(*self.ptr.get()).reflector());
(*self.ptr.as_ptr()).reflector());
}
}

/// An unrooted reference to a DOM object for use in layout. `Layout*Helpers`
/// 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> {
Expand All @@ -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) },
}
}

Expand All @@ -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
Expand All @@ -412,37 +411,37 @@ 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()
}
}

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()
}
}

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()
}
}

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

Expand Down Expand Up @@ -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),
}
}
}
Expand Down Expand Up @@ -700,15 +699,15 @@ 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
/// safe to call because it originates from the layout thread, and it cannot
/// 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() }
}
}

Expand Down
30 changes: 16 additions & 14 deletions components/script/dom/bindings/weakref.rs
Expand Up @@ -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,
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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));
}
Expand All @@ -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),
}
}
}
Expand All @@ -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 {
Expand All @@ -119,16 +120,17 @@ 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)
}
}
}

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,
}
}
Expand All @@ -145,15 +147,15 @@ 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);
(count, weak_box.value.get())
};
if count == 0 {
assert!(value.is_none());
mem::drop(Box::from_raw(self.ptr.get()));
mem::drop(Box::from_raw(self.ptr.as_ptr()));
}
}
}
Expand Down
1 change: 0 additions & 1 deletion components/script/lib.rs
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion components/script_layout_interface/Cargo.toml
Expand Up @@ -25,7 +25,6 @@ malloc_size_of_derive = { path = "../malloc_size_of_derive" }
metrics = {path = "../metrics"}
msg = {path = "../msg"}
net_traits = {path = "../net_traits"}
nonzero = {path = "../nonzero"}
profile_traits = {path = "../profile_traits"}
range = {path = "../range"}
script_traits = {path = "../script_traits"}
Expand Down

0 comments on commit 10ec5a2

Please sign in to comment.