Use Arc<PropertyDeclarationBlock> everwhere it’s appropriate.

This commit is contained in:
Simon Sapin 2016-08-31 01:06:45 +02:00
parent c50e6add4a
commit acc38aa8c2
13 changed files with 85 additions and 68 deletions

View file

@ -356,7 +356,7 @@ pub extern "C" fn Servo_StyleSet_Drop(data: *mut RawServoStyleSet) -> () {
}
pub struct GeckoDeclarationBlock {
pub declarations: Option<PropertyDeclarationBlock>,
pub declarations: Option<Arc<PropertyDeclarationBlock>>,
// XXX The following two fields are made atomic to work around the
// ownership system so that they can be changed inside a shared
// instance. It wouldn't provide safety as Rust usually promises,
@ -377,7 +377,7 @@ pub extern "C" fn Servo_ParseStyleAttribute(bytes: *const u8, length: u32,
-> ServoDeclarationBlockStrong {
let value = unsafe { from_utf8_unchecked(slice::from_raw_parts(bytes, length as usize)) };
GeckoDeclarationBlock::from_arc(Arc::new(GeckoDeclarationBlock {
declarations: GeckoElement::parse_style_attribute(value),
declarations: GeckoElement::parse_style_attribute(value).map(Arc::new),
cache: AtomicPtr::new(cache),
immutable: AtomicBool::new(false),
}))

View file

@ -35,7 +35,6 @@ use snapshot::GeckoElementSnapshot;
use snapshot_helpers;
use std::fmt;
use std::marker::PhantomData;
use std::mem::transmute;
use std::ops::BitOr;
use std::ptr;
use std::sync::Arc;
@ -460,8 +459,6 @@ lazy_static! {
};
}
static NO_STYLE_ATTRIBUTE: Option<PropertyDeclarationBlock> = None;
impl<'le> TElement for GeckoElement<'le> {
type ConcreteNode = GeckoNode<'le>;
type ConcreteDocument = GeckoDocument<'le>;
@ -470,14 +467,16 @@ impl<'le> TElement for GeckoElement<'le> {
unsafe { GeckoNode::from_raw(self.element as *mut RawGeckoNode) }
}
fn style_attribute(&self) -> &Option<PropertyDeclarationBlock> {
fn style_attribute(&self) -> Option<&Arc<PropertyDeclarationBlock>> {
let declarations = unsafe { Gecko_GetServoDeclarationBlock(self.element) };
if declarations.is_null() {
&NO_STYLE_ATTRIBUTE
None
} else {
GeckoDeclarationBlock::with(declarations, |declarations| {
unsafe { transmute(&declarations.declarations) }
})
let opt_ptr = GeckoDeclarationBlock::with(declarations, |declarations| {
// Use a raw poointer to extend the lifetime
declarations.declarations.as_ref().map(|r| r as *const Arc<_>)
});
opt_ptr.map(|ptr| unsafe { &*ptr })
}
}