Add unsafe blocks to make_unique

See #6376
This commit is contained in:
Manish Goregaokar 2015-06-14 20:52:27 +05:30
parent 3cdc412a4c
commit 94fa868d2b
4 changed files with 84 additions and 68 deletions

View file

@ -68,6 +68,7 @@ pub fn process_new_animations(rw_data: &mut LayoutTaskData, pipeline_id: Pipelin
/// Recalculates style for an animation. This does *not* run with the DOM lock held. /// Recalculates style for an animation. This does *not* run with the DOM lock held.
pub fn recalc_style_for_animation(flow: &mut Flow, animation: &Animation) { pub fn recalc_style_for_animation(flow: &mut Flow, animation: &Animation) {
#![allow(unsafe_code)] // #6376
let mut damage = RestyleDamage::empty(); let mut damage = RestyleDamage::empty();
flow.mutate_fragments(&mut |fragment| { flow.mutate_fragments(&mut |fragment| {
if fragment.node.id() != animation.node { if fragment.node.id() != animation.node {
@ -84,7 +85,7 @@ pub fn recalc_style_for_animation(flow: &mut Flow, animation: &Animation) {
} }
let mut new_style = fragment.style.clone(); let mut new_style = fragment.style.clone();
animation.property_animation.update(&mut *new_style.make_unique(), progress); animation.property_animation.update(&mut *unsafe { new_style.make_unique() }, progress);
damage.insert(incremental::compute_damage(&Some(fragment.style.clone()), &new_style)); damage.insert(incremental::compute_damage(&Some(fragment.style.clone()), &new_style));
fragment.style = new_style fragment.style = new_style
}); });

View file

@ -950,6 +950,7 @@ impl InlineFlow {
fn justify_inline_fragments(fragments: &mut InlineFragments, fn justify_inline_fragments(fragments: &mut InlineFragments,
line: &Line, line: &Line,
slack_inline_size: Au) { slack_inline_size: Au) {
#![allow(unsafe_code)] // #6376
// Fast path. // Fast path.
if slack_inline_size == Au(0) { if slack_inline_size == Au(0) {
return return
@ -984,9 +985,9 @@ impl InlineFlow {
// FIXME(pcwalton): This is an awful lot of uniqueness making. I don't see any easy way // FIXME(pcwalton): This is an awful lot of uniqueness making. I don't see any easy way
// to get rid of it without regressing the performance of the non-justified case, // to get rid of it without regressing the performance of the non-justified case,
// though. // though.
let run = scanned_text_fragment_info.run.make_unique(); let run = unsafe { scanned_text_fragment_info.run.make_unique() };
{ {
let glyph_runs = run.glyphs.make_unique(); let glyph_runs = unsafe { run.glyphs.make_unique() };
for mut glyph_run in glyph_runs.iter_mut() { for mut glyph_run in glyph_runs.iter_mut() {
let mut range = glyph_run.range.intersect(&fragment_range); let mut range = glyph_run.range.intersect(&fragment_range);
if range.is_empty() { if range.is_empty() {
@ -994,7 +995,7 @@ impl InlineFlow {
} }
range.shift_by(-glyph_run.range.begin()); range.shift_by(-glyph_run.range.begin());
let glyph_store = glyph_run.glyph_store.make_unique(); let glyph_store = unsafe { glyph_run.glyph_store.make_unique() };
glyph_store.distribute_extra_space_in_range(&range, glyph_store.distribute_extra_space_in_range(&range,
space_per_expansion_opportunity); space_per_expansion_opportunity);
} }

View file

@ -627,13 +627,14 @@ impl<'a> ElementHelpers<'a> for JSRef<'a, Element> {
} }
fn remove_inline_style_property(self, property: DOMString) { fn remove_inline_style_property(self, property: DOMString) {
#![allow(unsafe_code)] // #6376
let mut inline_declarations = self.style_attribute.borrow_mut(); let mut inline_declarations = self.style_attribute.borrow_mut();
if let &mut Some(ref mut declarations) = &mut *inline_declarations { if let &mut Some(ref mut declarations) = &mut *inline_declarations {
let index = declarations.normal let index = declarations.normal
.iter() .iter()
.position(|decl| decl.name() == property); .position(|decl| decl.name() == property);
if let Some(index) = index { if let Some(index) = index {
declarations.normal.make_unique().remove(index); unsafe { declarations.normal.make_unique().remove(index); }
return; return;
} }
@ -641,19 +642,20 @@ impl<'a> ElementHelpers<'a> for JSRef<'a, Element> {
.iter() .iter()
.position(|decl| decl.name() == property); .position(|decl| decl.name() == property);
if let Some(index) = index { if let Some(index) = index {
declarations.important.make_unique().remove(index); unsafe { declarations.important.make_unique().remove(index); }
return; return;
} }
} }
} }
fn update_inline_style(self, property_decl: PropertyDeclaration, style_priority: StylePriority) { fn update_inline_style(self, property_decl: PropertyDeclaration, style_priority: StylePriority) {
#![allow(unsafe_code)] // #6376
let mut inline_declarations = self.style_attribute().borrow_mut(); let mut inline_declarations = self.style_attribute().borrow_mut();
if let &mut Some(ref mut declarations) = &mut *inline_declarations { if let &mut Some(ref mut declarations) = &mut *inline_declarations {
let existing_declarations = if style_priority == StylePriority::Important { let existing_declarations = if style_priority == StylePriority::Important {
declarations.important.make_unique() unsafe { declarations.important.make_unique() }
} else { } else {
declarations.normal.make_unique() unsafe { declarations.normal.make_unique() }
}; };
for declaration in existing_declarations.iter_mut() { for declaration in existing_declarations.iter_mut() {

View file

@ -4,6 +4,10 @@
// This file is a Mako template: http://www.makotemplates.org/ // This file is a Mako template: http://www.makotemplates.org/
// XXXManishearth remove all unsafe blocks when
// https://github.com/rust-lang/rust/issues/24880 is fixed
// See #6376
use std::ascii::AsciiExt; use std::ascii::AsciiExt;
use std::borrow::ToOwned; use std::borrow::ToOwned;
use std::default::Default; use std::default::Default;
@ -5328,7 +5332,7 @@ impl ComputedValues {
#[inline] #[inline]
pub fn mutate_${style_struct.name.lower()} pub fn mutate_${style_struct.name.lower()}
<'a>(&'a mut self) -> &'a mut style_structs::${style_struct.name} { <'a>(&'a mut self) -> &'a mut style_structs::${style_struct.name} {
&mut *self.${style_struct.ident}.make_unique() unsafe{ &mut *self.${style_struct.ident}.make_unique() }
} }
% endfor % endfor
} }
@ -5439,8 +5443,8 @@ fn cascade_with_cached_declarations(
.clone() .clone()
} }
}; };
style_${style_struct.ident}.make_unique() unsafe { style_${style_struct.ident}.make_unique()
.${property.ident} = computed_value; .${property.ident} = computed_value; }
% endif % endif
% if property.name in DERIVED_LONGHANDS: % if property.name in DERIVED_LONGHANDS:
@ -5450,13 +5454,13 @@ fn cascade_with_cached_declarations(
.${property.ident}.clone(); .${property.ident}.clone();
% endif % endif
% for derived in DERIVED_LONGHANDS[property.name]: % for derived in DERIVED_LONGHANDS[property.name]:
style_${derived.style_struct.ident} unsafe { style_${derived.style_struct.ident}
.make_unique() .make_unique()
.${derived.ident} = .${derived.ident} =
longhands::${derived.ident} longhands::${derived.ident}
::derive_from_${property.ident}( ::derive_from_${property.ident}(
computed_value, computed_value,
context); context); }
% endfor % endfor
% endif % endif
} }
@ -5473,7 +5477,7 @@ fn cascade_with_cached_declarations(
if seen.get_font_style() || seen.get_font_weight() || seen.get_font_stretch() || if seen.get_font_style() || seen.get_font_weight() || seen.get_font_stretch() ||
seen.get_font_family() { seen.get_font_family() {
compute_font_hash(&mut *style_font.make_unique()) unsafe { compute_font_hash(&mut *style_font.make_unique()) }
} }
ComputedValues { ComputedValues {
@ -5685,18 +5689,18 @@ pub fn cascade(viewport_size: Size2D<Au>,
.clone() .clone()
} }
}; };
style_${style_struct.ident}.make_unique() unsafe { style_${style_struct.ident}.make_unique()
.${property.ident} = computed_value; .${property.ident} = computed_value; }
% if property.name in DERIVED_LONGHANDS: % if property.name in DERIVED_LONGHANDS:
% for derived in DERIVED_LONGHANDS[property.name]: % for derived in DERIVED_LONGHANDS[property.name]:
style_${derived.style_struct.ident} unsafe { style_${derived.style_struct.ident}
.make_unique() .make_unique()
.${derived.ident} = .${derived.ident} =
longhands::${derived.ident} longhands::${derived.ident}
::derive_from_${property.ident}( ::derive_from_${property.ident}(
computed_value, computed_value,
&context); &context); }
% endfor % endfor
% endif % endif
} }
@ -5713,7 +5717,7 @@ pub fn cascade(viewport_size: Size2D<Au>,
// The initial value of border-*-width may be changed at computed value time. // The initial value of border-*-width may be changed at computed value time.
{ {
let border = style_border.make_unique(); let border = unsafe { style_border.make_unique() };
% for side in ["top", "right", "bottom", "left"]: % for side in ["top", "right", "bottom", "left"]:
// Like calling to_computed_value, which wouldn't type check. // Like calling to_computed_value, which wouldn't type check.
if !context.border_${side}_present { if !context.border_${side}_present {
@ -5724,7 +5728,7 @@ pub fn cascade(viewport_size: Size2D<Au>,
// The initial value of display may be changed at computed value time. // The initial value of display may be changed at computed value time.
if !seen.get_display() { if !seen.get_display() {
let box_ = style_box_.make_unique(); let box_ = unsafe { style_box_.make_unique() };
box_.display = box_.display.to_computed_value(&context); box_.display = box_.display.to_computed_value(&context);
} }
@ -5734,7 +5738,7 @@ pub fn cascade(viewport_size: Size2D<Au>,
if seen.get_font_style() || seen.get_font_weight() || seen.get_font_stretch() || if seen.get_font_style() || seen.get_font_weight() || seen.get_font_stretch() ||
seen.get_font_family() { seen.get_font_family() {
compute_font_hash(&mut *style_font.make_unique()) unsafe { compute_font_hash(&mut *style_font.make_unique()) }
} }
(ComputedValues { (ComputedValues {
@ -5769,7 +5773,7 @@ pub fn cascade_anonymous(parent_style: &ComputedValues) -> ComputedValues {
root_font_size: parent_style.root_font_size, root_font_size: parent_style.root_font_size,
}; };
{ {
let border = result.border.make_unique(); let border = unsafe { result.border.make_unique() };
% for side in ["top", "right", "bottom", "left"]: % for side in ["top", "right", "bottom", "left"]:
// Like calling to_computed_value, which wouldn't type check. // Like calling to_computed_value, which wouldn't type check.
border.border_${side}_width = Au(0); border.border_${side}_width = Au(0);
@ -5789,16 +5793,20 @@ pub fn cascade_anonymous(parent_style: &ComputedValues) -> ComputedValues {
pub fn modify_style_for_replaced_content(style: &mut Arc<ComputedValues>) { pub fn modify_style_for_replaced_content(style: &mut Arc<ComputedValues>) {
// Reset `position` to handle cases like `<div style="position: absolute">foo bar baz</div>`. // Reset `position` to handle cases like `<div style="position: absolute">foo bar baz</div>`.
if style.box_.display != longhands::display::computed_value::T::inline { if style.box_.display != longhands::display::computed_value::T::inline {
let mut style = style.make_unique(); unsafe {
style.box_.make_unique().display = longhands::display::computed_value::T::inline; let mut style = style.make_unique();
style.box_.make_unique().position = longhands::position::computed_value::T::static_; style.box_.make_unique().display = longhands::display::computed_value::T::inline;
style.box_.make_unique().position = longhands::position::computed_value::T::static_;
}
} }
// Reset `vertical-align` to handle cases like `<sup>foo</sup>`. // Reset `vertical-align` to handle cases like `<sup>foo</sup>`.
if style.box_.vertical_align != longhands::vertical_align::computed_value::T::baseline { if style.box_.vertical_align != longhands::vertical_align::computed_value::T::baseline {
let mut style = style.make_unique(); unsafe {
style.box_.make_unique().vertical_align = let mut style = style.make_unique();
longhands::vertical_align::computed_value::T::baseline style.box_.make_unique().vertical_align =
longhands::vertical_align::computed_value::T::baseline
}
} }
// Reset margins. // Reset margins.
@ -5806,12 +5814,14 @@ pub fn modify_style_for_replaced_content(style: &mut Arc<ComputedValues>) {
style.margin.margin_left != computed::LengthOrPercentageOrAuto::Length(Au(0)) || style.margin.margin_left != computed::LengthOrPercentageOrAuto::Length(Au(0)) ||
style.margin.margin_bottom != computed::LengthOrPercentageOrAuto::Length(Au(0)) || style.margin.margin_bottom != computed::LengthOrPercentageOrAuto::Length(Au(0)) ||
style.margin.margin_right != computed::LengthOrPercentageOrAuto::Length(Au(0)) { style.margin.margin_right != computed::LengthOrPercentageOrAuto::Length(Au(0)) {
let mut style = style.make_unique(); unsafe {
let margin = style.margin.make_unique(); let mut style = style.make_unique();
margin.margin_top = computed::LengthOrPercentageOrAuto::Length(Au(0)); let margin = style.margin.make_unique();
margin.margin_left = computed::LengthOrPercentageOrAuto::Length(Au(0)); margin.margin_top = computed::LengthOrPercentageOrAuto::Length(Au(0));
margin.margin_bottom = computed::LengthOrPercentageOrAuto::Length(Au(0)); margin.margin_left = computed::LengthOrPercentageOrAuto::Length(Au(0));
margin.margin_right = computed::LengthOrPercentageOrAuto::Length(Au(0)); margin.margin_bottom = computed::LengthOrPercentageOrAuto::Length(Au(0));
margin.margin_right = computed::LengthOrPercentageOrAuto::Length(Au(0));
}
} }
} }
@ -5825,40 +5835,42 @@ pub fn modify_style_for_inline_sides(style: &mut Arc<ComputedValues>,
is_first_fragment_of_element: bool, is_first_fragment_of_element: bool,
is_last_fragment_of_element: bool) { is_last_fragment_of_element: bool) {
fn modify_side(style: &mut Arc<ComputedValues>, side: PhysicalSide) { fn modify_side(style: &mut Arc<ComputedValues>, side: PhysicalSide) {
let mut style = style.make_unique(); unsafe {
let border = style.border.make_unique(); let mut style = style.make_unique();
match side { let border = style.border.make_unique();
PhysicalSide::Left => { match side {
border.border_left_width = Au(0); PhysicalSide::Left => {
border.border_left_style = BorderStyle::none; border.border_left_width = Au(0);
style.padding.make_unique().padding_left = border.border_left_style = BorderStyle::none;
computed::LengthOrPercentage::Length(Au(0)); style.padding.make_unique().padding_left =
style.margin.make_unique().margin_left = computed::LengthOrPercentage::Length(Au(0));
computed::LengthOrPercentageOrAuto::Length(Au(0)) style.margin.make_unique().margin_left =
} computed::LengthOrPercentageOrAuto::Length(Au(0))
PhysicalSide::Right => { }
border.border_right_width = Au(0); PhysicalSide::Right => {
border.border_right_style = BorderStyle::none; border.border_right_width = Au(0);
style.padding.make_unique().padding_right = border.border_right_style = BorderStyle::none;
computed::LengthOrPercentage::Length(Au(0)); style.padding.make_unique().padding_right =
style.margin.make_unique().margin_right = computed::LengthOrPercentage::Length(Au(0));
computed::LengthOrPercentageOrAuto::Length(Au(0)) style.margin.make_unique().margin_right =
} computed::LengthOrPercentageOrAuto::Length(Au(0))
PhysicalSide::Bottom => { }
border.border_bottom_width = Au(0); PhysicalSide::Bottom => {
border.border_bottom_style = BorderStyle::none; border.border_bottom_width = Au(0);
style.padding.make_unique().padding_bottom = border.border_bottom_style = BorderStyle::none;
computed::LengthOrPercentage::Length(Au(0)); style.padding.make_unique().padding_bottom =
style.margin.make_unique().margin_bottom = computed::LengthOrPercentage::Length(Au(0));
computed::LengthOrPercentageOrAuto::Length(Au(0)) style.margin.make_unique().margin_bottom =
} computed::LengthOrPercentageOrAuto::Length(Au(0))
PhysicalSide::Top => { }
border.border_top_width = Au(0); PhysicalSide::Top => {
border.border_top_style = BorderStyle::none; border.border_top_width = Au(0);
style.padding.make_unique().padding_top = border.border_top_style = BorderStyle::none;
computed::LengthOrPercentage::Length(Au(0)); style.padding.make_unique().padding_top =
style.margin.make_unique().margin_top = computed::LengthOrPercentage::Length(Au(0));
computed::LengthOrPercentageOrAuto::Length(Au(0)) style.margin.make_unique().margin_top =
computed::LengthOrPercentageOrAuto::Length(Au(0))
}
} }
} }
} }