Make OpaqueLayoutAndStyleData just a bit less opaque

It now stores a NonNull<dyn Any>.
This commit is contained in:
Anthony Ramine 2020-04-02 13:17:44 +02:00
parent e47e884cc7
commit 4c61baee30
11 changed files with 83 additions and 65 deletions

View file

@ -7,6 +7,7 @@
//! to depend on script.
#![deny(unsafe_code)]
#![feature(box_into_raw_non_null)]
#[macro_use]
extern crate html5ever;
@ -24,6 +25,7 @@ use libc::c_void;
use net_traits::image_cache::PendingImageId;
use script_traits::UntrustedNodeAddress;
use servo_url::{ImmutableOrigin, ServoUrl};
use std::any::Any;
use std::ptr::NonNull;
use std::sync::atomic::AtomicIsize;
use style::data::ElementData;
@ -54,7 +56,34 @@ pub struct OpaqueStyleAndLayoutData {
// NB: We really store a `StyleAndLayoutData` here, so be careful!
#[ignore_malloc_size_of = "TODO(#6910) Box value that should be counted but \
the type lives in layout"]
pub ptr: NonNull<StyleData>,
ptr: NonNull<dyn Any + Send + Sync>,
}
impl OpaqueStyleAndLayoutData {
#[inline]
pub fn new<T>(value: T) -> Self
where
T: Any + Send + Sync,
{
Self {
ptr: Box::into_raw_non_null(Box::new(value) as Box<dyn Any + Send + Sync>),
}
}
#[inline]
pub fn as_ptr(&self) -> *mut (dyn Any + Send + Sync) {
self.ptr.as_ptr()
}
/// Extremely cursed.
#[allow(unsafe_code)]
#[inline]
pub unsafe fn downcast_ref<'extended, T>(&self) -> Option<&'extended T>
where
T: Any + Send + Sync,
{
(*self.ptr.as_ptr()).downcast_ref()
}
}
#[allow(unsafe_code)]