Use NonZeroUsize in script_layout_interface

This commit is contained in:
Simon Sapin 2017-10-12 02:00:33 +02:00
parent ff23a8536e
commit 7ebedd02a9
8 changed files with 38 additions and 13 deletions

2
Cargo.lock generated
View file

@ -1567,6 +1567,7 @@ dependencies = [
"metrics 0.0.1", "metrics 0.0.1",
"msg 0.0.1", "msg 0.0.1",
"net_traits 0.0.1", "net_traits 0.0.1",
"nonzero 0.0.1",
"parking_lot 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"profile_traits 0.0.1", "profile_traits 0.0.1",
"range 0.0.1", "range 0.0.1",
@ -2675,6 +2676,7 @@ dependencies = [
"metrics 0.0.1", "metrics 0.0.1",
"msg 0.0.1", "msg 0.0.1",
"net_traits 0.0.1", "net_traits 0.0.1",
"nonzero 0.0.1",
"profile_traits 0.0.1", "profile_traits 0.0.1",
"range 0.0.1", "range 0.0.1",
"script_traits 0.0.1", "script_traits 0.0.1",

View file

@ -10,7 +10,7 @@ name = "layout_thread"
path = "lib.rs" path = "lib.rs"
[features] [features]
unstable = ["parking_lot/nightly"] unstable = ["parking_lot/nightly", "nonzero/unstable"]
[dependencies] [dependencies]
app_units = "0.5" app_units = "0.5"
@ -29,6 +29,7 @@ log = "0.3.5"
metrics = {path = "../metrics"} metrics = {path = "../metrics"}
msg = {path = "../msg"} msg = {path = "../msg"}
net_traits = {path = "../net_traits"} net_traits = {path = "../net_traits"}
nonzero = {path = "../nonzero"}
parking_lot = "0.4" parking_lot = "0.4"
profile_traits = {path = "../profile_traits"} profile_traits = {path = "../profile_traits"}
range = {path = "../range"} range = {path = "../range"}

View file

@ -31,12 +31,12 @@
#![allow(unsafe_code)] #![allow(unsafe_code)]
use atomic_refcell::{AtomicRef, AtomicRefMut, AtomicRefCell}; use atomic_refcell::{AtomicRef, AtomicRefMut, AtomicRefCell};
use core::nonzero::NonZero;
use gfx_traits::ByteIndex; use gfx_traits::ByteIndex;
use html5ever::{LocalName, Namespace}; use html5ever::{LocalName, Namespace};
use layout::data::StyleAndLayoutData; use layout::data::StyleAndLayoutData;
use layout::wrapper::GetRawData; use layout::wrapper::GetRawData;
use msg::constellation_msg::{BrowsingContextId, PipelineId}; use msg::constellation_msg::{BrowsingContextId, PipelineId};
use nonzero::NonZeroUsize;
use range::Range; use range::Range;
use script::layout_exports::{CAN_BE_FRAGMENTED, HAS_DIRTY_DESCENDANTS, IS_IN_DOC}; use script::layout_exports::{CAN_BE_FRAGMENTED, HAS_DIRTY_DESCENDANTS, IS_IN_DOC};
use script::layout_exports::{CharacterDataTypeId, ElementTypeId, HTMLElementTypeId, NodeTypeId}; use script::layout_exports::{CharacterDataTypeId, ElementTypeId, HTMLElementTypeId, NodeTypeId};
@ -79,7 +79,7 @@ use style::shared_lock::{SharedRwLock as StyleSharedRwLock, Locked as StyleLocke
use style::str::is_whitespace; use style::str::is_whitespace;
pub unsafe fn drop_style_and_layout_data(data: OpaqueStyleAndLayoutData) { pub unsafe fn drop_style_and_layout_data(data: OpaqueStyleAndLayoutData) {
let ptr: *mut StyleData = data.ptr.get(); let ptr = data.ptr.get() as *mut StyleData;
let non_opaque: *mut StyleAndLayoutData = ptr as *mut _; let non_opaque: *mut StyleAndLayoutData = ptr as *mut _;
let _ = Box::from_raw(non_opaque); let _ = Box::from_raw(non_opaque);
} }
@ -235,7 +235,8 @@ impl<'ln> LayoutNode for ServoLayoutNode<'ln> {
let ptr: *mut StyleAndLayoutData = let ptr: *mut StyleAndLayoutData =
Box::into_raw(Box::new(StyleAndLayoutData::new())); Box::into_raw(Box::new(StyleAndLayoutData::new()));
let opaque = OpaqueStyleAndLayoutData { let opaque = OpaqueStyleAndLayoutData {
ptr: NonZero::new_unchecked(ptr as *mut StyleData), ptr: NonZeroUsize::new_unchecked(ptr as usize),
phantom: PhantomData,
}; };
self.init_style_and_layout_data(opaque); self.init_style_and_layout_data(opaque);
}; };
@ -471,7 +472,7 @@ impl<'le> TElement for ServoLayoutElement<'le> {
fn get_data(&self) -> Option<&AtomicRefCell<ElementData>> { fn get_data(&self) -> Option<&AtomicRefCell<ElementData>> {
unsafe { unsafe {
self.get_style_and_layout_data().map(|d| { self.get_style_and_layout_data().map(|d| {
&(*d.ptr.get()).element_data &(*(d.ptr.get() as *mut StyleData)).element_data
}) })
} }
} }
@ -583,7 +584,7 @@ impl<'le> ServoLayoutElement<'le> {
fn get_style_data(&self) -> Option<&StyleData> { fn get_style_data(&self) -> Option<&StyleData> {
unsafe { unsafe {
self.get_style_and_layout_data().map(|d| &*d.ptr.get()) self.get_style_and_layout_data().map(|d| &*(d.ptr.get() as *mut StyleData))
} }
} }

View file

@ -6,11 +6,10 @@
//! painted. //! painted.
#![feature(mpsc_select)] #![feature(mpsc_select)]
#![feature(nonzero)] #![cfg_attr(feature = "unstable", feature(nonzero))]
extern crate app_units; extern crate app_units;
extern crate atomic_refcell; extern crate atomic_refcell;
extern crate core;
extern crate euclid; extern crate euclid;
extern crate fnv; extern crate fnv;
extern crate gfx; extern crate gfx;
@ -29,6 +28,7 @@ extern crate log;
extern crate metrics; extern crate metrics;
extern crate msg; extern crate msg;
extern crate net_traits; extern crate net_traits;
extern crate nonzero;
extern crate parking_lot; extern crate parking_lot;
#[macro_use] #[macro_use]
extern crate profile_traits; extern crate profile_traits;

View file

@ -31,6 +31,7 @@ mod imp {
pub struct NonZeroU32(u32); pub struct NonZeroU32(u32);
impl NonZeroU32 { impl NonZeroU32 {
#[inline]
pub fn new(x: u32) -> Option<Self> { pub fn new(x: u32) -> Option<Self> {
if x != 0 { if x != 0 {
Some(NonZeroU32(x)) Some(NonZeroU32(x))
@ -39,10 +40,12 @@ mod imp {
} }
} }
#[inline]
pub unsafe fn new_unchecked(x: u32) -> Self { pub unsafe fn new_unchecked(x: u32) -> Self {
NonZeroU32(x) NonZeroU32(x)
} }
#[inline]
pub fn get(self) -> u32 { pub fn get(self) -> u32 {
self.0 self.0
} }
@ -52,38 +55,49 @@ mod imp {
pub struct NonZeroUsize(&'static ()); pub struct NonZeroUsize(&'static ());
impl NonZeroUsize { impl NonZeroUsize {
#[inline]
pub fn new(x: usize) -> Option<Self> { pub fn new(x: usize) -> Option<Self> {
if x != 0 { if x != 0 {
Some(NonZeroUsize(unsafe { &*(x as *const ()) })) Some(unsafe { Self::new_unchecked(x) })
} else { } else {
None None
} }
} }
#[inline]
pub unsafe fn new_unchecked(x: usize) -> Self {
NonZeroUsize(&*(x as *const ()))
}
#[inline]
pub fn get(self) -> usize { pub fn get(self) -> usize {
self.0 as *const () as usize self.0 as *const () as usize
} }
} }
impl PartialEq for NonZeroUsize { impl PartialEq for NonZeroUsize {
#[inline]
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.get() == other.get() self.get() == other.get()
} }
} }
impl PartialOrd for NonZeroUsize { impl PartialOrd for NonZeroUsize {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
self.get().partial_cmp(&other.get()) self.get().partial_cmp(&other.get())
} }
} }
impl Ord for NonZeroUsize { impl Ord for NonZeroUsize {
#[inline]
fn cmp(&self, other: &Self) -> cmp::Ordering { fn cmp(&self, other: &Self) -> cmp::Ordering {
self.get().cmp(&other.get()) self.get().cmp(&other.get())
} }
} }
impl hash::Hash for NonZeroUsize { impl hash::Hash for NonZeroUsize {
#[inline]
fn hash<H: hash::Hasher>(&self, hasher: &mut H) { fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
self.get().hash(hasher) self.get().hash(hasher)
} }

View file

@ -9,6 +9,9 @@ publish = false
name = "script_layout_interface" name = "script_layout_interface"
path = "lib.rs" path = "lib.rs"
[features]
unstable = ["nonzero/unstable"]
[dependencies] [dependencies]
app_units = "0.5" app_units = "0.5"
atomic_refcell = "0.1" atomic_refcell = "0.1"
@ -25,6 +28,7 @@ log = "0.3.5"
metrics = {path = "../metrics"} metrics = {path = "../metrics"}
msg = {path = "../msg"} msg = {path = "../msg"}
net_traits = {path = "../net_traits"} net_traits = {path = "../net_traits"}
nonzero = {path = "../nonzero"}
profile_traits = {path = "../profile_traits"} profile_traits = {path = "../profile_traits"}
range = {path = "../range"} range = {path = "../range"}
script_traits = {path = "../script_traits"} script_traits = {path = "../script_traits"}

View file

@ -7,12 +7,11 @@
//! to depend on script. //! to depend on script.
#![deny(unsafe_code)] #![deny(unsafe_code)]
#![feature(nonzero)] #![cfg_attr(feature = "unstable", feature(nonzero))]
extern crate app_units; extern crate app_units;
extern crate atomic_refcell; extern crate atomic_refcell;
extern crate canvas_traits; extern crate canvas_traits;
extern crate core;
extern crate cssparser; extern crate cssparser;
extern crate euclid; extern crate euclid;
extern crate gfx_traits; extern crate gfx_traits;
@ -26,6 +25,7 @@ extern crate log;
extern crate metrics; extern crate metrics;
extern crate msg; extern crate msg;
extern crate net_traits; extern crate net_traits;
extern crate nonzero;
extern crate profile_traits; extern crate profile_traits;
extern crate range; extern crate range;
extern crate script_traits; extern crate script_traits;
@ -43,12 +43,13 @@ pub mod wrapper_traits;
use atomic_refcell::AtomicRefCell; use atomic_refcell::AtomicRefCell;
use canvas_traits::canvas::CanvasMsg; use canvas_traits::canvas::CanvasMsg;
use core::nonzero::NonZero;
use ipc_channel::ipc::IpcSender; use ipc_channel::ipc::IpcSender;
use libc::c_void; use libc::c_void;
use net_traits::image_cache::PendingImageId; use net_traits::image_cache::PendingImageId;
use nonzero::NonZeroUsize;
use script_traits::UntrustedNodeAddress; use script_traits::UntrustedNodeAddress;
use servo_url::ServoUrl; use servo_url::ServoUrl;
use std::marker::PhantomData;
use std::sync::atomic::AtomicIsize; use std::sync::atomic::AtomicIsize;
use style::data::ElementData; use style::data::ElementData;
@ -78,7 +79,8 @@ pub struct OpaqueStyleAndLayoutData {
// NB: We really store a `StyleAndLayoutData` here, so be careful! // NB: We really store a `StyleAndLayoutData` here, so be careful!
#[ignore_heap_size_of = "TODO(#6910) Box value that should be counted but \ #[ignore_heap_size_of = "TODO(#6910) Box value that should be counted but \
the type lives in layout"] the type lives in layout"]
pub ptr: NonZero<*mut StyleData> pub ptr: NonZeroUsize,
pub phantom: PhantomData<*mut StyleData>,
} }
#[allow(unsafe_code)] #[allow(unsafe_code)]

View file

@ -27,6 +27,7 @@ unstable = [
"constellation/unstable", "constellation/unstable",
"gfx/unstable", "gfx/unstable",
"profile/unstable", "profile/unstable",
"script_layout_interface/unstable",
] ]
[dependencies] [dependencies]