Auto merge of #16721 - Manishearth:vector-iter, r=emilio

Set vector properties via iterator in stylo

https://bugzilla.mozilla.org/show_bug.cgi?id=1360882

Avoids transient allocations

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/16721)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2017-05-04 11:23:05 -05:00 committed by GitHub
commit e23c8f1f3c
6 changed files with 295 additions and 87 deletions

View file

@ -140,7 +140,7 @@ class Longhand(object):
need_clone=False, need_index=False, gecko_ffi_name=None, depend_on_viewport_size=False, need_clone=False, need_index=False, gecko_ffi_name=None, depend_on_viewport_size=False,
allowed_in_keyframe_block=True, complex_color=False, cast_type='u8', allowed_in_keyframe_block=True, complex_color=False, cast_type='u8',
has_uncacheable_values=False, logical=False, alias=None, extra_prefixes=None, boxed=False, has_uncacheable_values=False, logical=False, alias=None, extra_prefixes=None, boxed=False,
flags=None, allowed_in_page_rule=False, allow_quirks=False): flags=None, allowed_in_page_rule=False, allow_quirks=False, vector=False):
self.name = name self.name = name
if not spec: if not spec:
raise TypeError("Spec should be specified for %s" % name) raise TypeError("Spec should be specified for %s" % name)
@ -167,6 +167,7 @@ class Longhand(object):
self.flags = flags.split() if flags else [] self.flags = flags.split() if flags else []
self.allowed_in_page_rule = arg_to_bool(allowed_in_page_rule) self.allowed_in_page_rule = arg_to_bool(allowed_in_page_rule)
self.allow_quirks = allow_quirks self.allow_quirks = allow_quirks
self.is_vector = vector
# https://drafts.csswg.org/css-animations/#keyframes # https://drafts.csswg.org/css-animations/#keyframes
# > The <declaration-list> inside of <keyframe-block> accepts any CSS property # > The <declaration-list> inside of <keyframe-block> accepts any CSS property

View file

@ -1697,14 +1697,18 @@ fn static_assert() {
<%def name="impl_animation_or_transition_time_value(type, ident, gecko_ffi_name)"> <%def name="impl_animation_or_transition_time_value(type, ident, gecko_ffi_name)">
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub fn set_${type}_${ident}(&mut self, v: longhands::${type}_${ident}::computed_value::T) { pub fn set_${type}_${ident}<I>(&mut self, v: I)
debug_assert!(!v.0.is_empty()); where I: IntoIterator<Item = longhands::${type}_${ident}::computed_value::single_value::T>,
let input_len = v.0.len(); I::IntoIter: ExactSizeIterator + Clone
{
let v = v.into_iter();
debug_assert!(v.len() != 0);
let input_len = v.len();
unsafe { self.gecko.m${type.capitalize()}s.ensure_len(input_len) }; unsafe { self.gecko.m${type.capitalize()}s.ensure_len(input_len) };
self.gecko.m${type.capitalize()}${gecko_ffi_name}Count = input_len as u32; self.gecko.m${type.capitalize()}${gecko_ffi_name}Count = input_len as u32;
for (i, gecko) in self.gecko.m${type.capitalize()}s.iter_mut().enumerate() { for (gecko, servo) in self.gecko.m${type.capitalize()}s.iter_mut().zip(v.cycle()) {
gecko.m${gecko_ffi_name} = v.0[i % input_len].seconds() * 1000.; gecko.m${gecko_ffi_name} = servo.seconds() * 1000.;
} }
} }
#[allow(non_snake_case)] #[allow(non_snake_case)]
@ -1718,14 +1722,18 @@ fn static_assert() {
</%def> </%def>
<%def name="impl_animation_or_transition_timing_function(type)"> <%def name="impl_animation_or_transition_timing_function(type)">
pub fn set_${type}_timing_function(&mut self, v: longhands::${type}_timing_function::computed_value::T) { pub fn set_${type}_timing_function<I>(&mut self, v: I)
debug_assert!(!v.0.is_empty()); where I: IntoIterator<Item = longhands::${type}_timing_function::computed_value::single_value::T>,
let input_len = v.0.len(); I::IntoIter: ExactSizeIterator + Clone
{
let v = v.into_iter();
debug_assert!(v.len() != 0);
let input_len = v.len();
unsafe { self.gecko.m${type.capitalize()}s.ensure_len(input_len) }; unsafe { self.gecko.m${type.capitalize()}s.ensure_len(input_len) };
self.gecko.m${type.capitalize()}TimingFunctionCount = input_len as u32; self.gecko.m${type.capitalize()}TimingFunctionCount = input_len as u32;
for (i, gecko) in self.gecko.m${type.capitalize()}s.iter_mut().enumerate() { for (gecko, servo) in self.gecko.m${type.capitalize()}s.iter_mut().zip(v.cycle()) {
gecko.mTimingFunction = v.0[i % input_len].into(); gecko.mTimingFunction = servo.into();
} }
} }
${impl_animation_or_transition_count(type, 'timing_function', 'TimingFunction')} ${impl_animation_or_transition_count(type, 'timing_function', 'TimingFunction')}
@ -1766,18 +1774,23 @@ fn static_assert() {
<%def name="impl_animation_keyword(ident, gecko_ffi_name, keyword, cast_type='u8')"> <%def name="impl_animation_keyword(ident, gecko_ffi_name, keyword, cast_type='u8')">
#[allow(non_snake_case)] #[allow(non_snake_case)]
pub fn set_animation_${ident}(&mut self, v: longhands::animation_${ident}::computed_value::T) { pub fn set_animation_${ident}<I>(&mut self, v: I)
where I: IntoIterator<Item = longhands::animation_${ident}::computed_value::single_value::T>,
I::IntoIter: ExactSizeIterator + Clone
{
use properties::longhands::animation_${ident}::single_value::computed_value::T as Keyword; use properties::longhands::animation_${ident}::single_value::computed_value::T as Keyword;
use gecko_bindings::structs; use gecko_bindings::structs;
debug_assert!(!v.0.is_empty()); let v = v.into_iter();
let input_len = v.0.len();
debug_assert!(v.len() != 0);
let input_len = v.len();
unsafe { self.gecko.mAnimations.ensure_len(input_len) }; unsafe { self.gecko.mAnimations.ensure_len(input_len) };
self.gecko.mAnimation${gecko_ffi_name}Count = input_len as u32; self.gecko.mAnimation${gecko_ffi_name}Count = input_len as u32;
for (i, gecko) in self.gecko.mAnimations.iter_mut().enumerate() { for (gecko, servo) in self.gecko.mAnimations.iter_mut().zip(v.cycle()) {
let result = match v.0[i % input_len] { let result = match servo {
% for value in keyword.gecko_values(): % for value in keyword.gecko_values():
Keyword::${to_rust_ident(value)} => Keyword::${to_rust_ident(value)} =>
structs::${keyword.gecko_constant(value)} ${keyword.maybe_cast(cast_type)}, structs::${keyword.gecko_constant(value)} ${keyword.maybe_cast(cast_type)},
@ -1970,11 +1983,16 @@ fn static_assert() {
${impl_coord_copy('scroll_snap_points_y', 'mScrollSnapPointsY')} ${impl_coord_copy('scroll_snap_points_y', 'mScrollSnapPointsY')}
pub fn set_scroll_snap_coordinate(&mut self, v: longhands::scroll_snap_coordinate::computed_value::T) { pub fn set_scroll_snap_coordinate<I>(&mut self, v: I)
unsafe { self.gecko.mScrollSnapCoordinate.set_len_pod(v.0.len() as u32); } where I: IntoIterator<Item = longhands::scroll_snap_coordinate::computed_value::single_value::T>,
I::IntoIter: ExactSizeIterator
{
let v = v.into_iter();
unsafe { self.gecko.mScrollSnapCoordinate.set_len_pod(v.len() as u32); }
for (gecko, servo) in self.gecko.mScrollSnapCoordinate for (gecko, servo) in self.gecko.mScrollSnapCoordinate
.iter_mut() .iter_mut()
.zip(v.0.iter()) { .zip(v) {
gecko.mXPosition = servo.horizontal.0.into(); gecko.mXPosition = servo.horizontal.0.into();
gecko.mYPosition = servo.vertical.0.into(); gecko.mYPosition = servo.vertical.0.into();
} }
@ -2165,13 +2183,18 @@ fn static_assert() {
self.gecko.mTransitions[index].mDuration.max(0.0) + self.gecko.mTransitions[index].mDelay self.gecko.mTransitions[index].mDuration.max(0.0) + self.gecko.mTransitions[index].mDelay
} }
pub fn set_transition_property(&mut self, v: longhands::transition_property::computed_value::T) { pub fn set_transition_property<I>(&mut self, v: I)
where I: IntoIterator<Item = longhands::transition_property::computed_value::single_value::T>,
I::IntoIter: ExactSizeIterator
{
use gecko_bindings::structs::nsCSSPropertyID::eCSSPropertyExtra_no_properties; use gecko_bindings::structs::nsCSSPropertyID::eCSSPropertyExtra_no_properties;
if !v.0.is_empty() { let v = v.into_iter();
unsafe { self.gecko.mTransitions.ensure_len(v.0.len()) };
self.gecko.mTransitionPropertyCount = v.0.len() as u32; if v.len() != 0 {
for (servo, gecko) in v.0.into_iter().zip(self.gecko.mTransitions.iter_mut()) { unsafe { self.gecko.mTransitions.ensure_len(v.len()) };
self.gecko.mTransitionPropertyCount = v.len() as u32;
for (servo, gecko) in v.zip(self.gecko.mTransitions.iter_mut()) {
match servo { match servo {
TransitionProperty::Unsupported(ref atom) => unsafe { TransitionProperty::Unsupported(ref atom) => unsafe {
Gecko_StyleTransition_SetUnsupportedProperty(gecko, atom.as_ptr()) Gecko_StyleTransition_SetUnsupportedProperty(gecko, atom.as_ptr())
@ -2246,12 +2269,17 @@ fn static_assert() {
unsafe { bindings::Gecko_StyleAnimationsEquals(&self.gecko.mAnimations, &other.gecko.mAnimations) } unsafe { bindings::Gecko_StyleAnimationsEquals(&self.gecko.mAnimations, &other.gecko.mAnimations) }
} }
pub fn set_animation_name(&mut self, v: longhands::animation_name::computed_value::T) { pub fn set_animation_name<I>(&mut self, v: I)
debug_assert!(!v.0.is_empty()); where I: IntoIterator<Item = longhands::animation_name::computed_value::single_value::T>,
unsafe { self.gecko.mAnimations.ensure_len(v.0.len()) }; I::IntoIter: ExactSizeIterator
{
self.gecko.mAnimationNameCount = v.0.len() as u32; let v = v.into_iter();
for (servo, gecko) in v.0.into_iter().zip(self.gecko.mAnimations.iter_mut()) { debug_assert!(v.len() != 0);
unsafe { self.gecko.mAnimations.ensure_len(v.len()) };
self.gecko.mAnimationNameCount = v.len() as u32;
for (servo, gecko) in v.zip(self.gecko.mAnimations.iter_mut()) {
// TODO This is inefficient. We should fix this in bug 1329169. // TODO This is inefficient. We should fix this in bug 1329169.
gecko.mName.assign(match servo.0 { gecko.mName.assign(match servo.0 {
Some(ref name) => name.as_atom().as_slice(), Some(ref name) => name.as_atom().as_slice(),
@ -2294,17 +2322,22 @@ fn static_assert() {
${impl_animation_keyword('play_state', 'PlayState', ${impl_animation_keyword('play_state', 'PlayState',
data.longhands_by_name["animation-play-state"].keyword)} data.longhands_by_name["animation-play-state"].keyword)}
pub fn set_animation_iteration_count(&mut self, v: longhands::animation_iteration_count::computed_value::T) { pub fn set_animation_iteration_count<I>(&mut self, v: I)
where I: IntoIterator<Item = longhands::animation_iteration_count::computed_value::single_value::T>,
I::IntoIter: ExactSizeIterator + Clone
{
use std::f32; use std::f32;
use properties::longhands::animation_iteration_count::single_value::SpecifiedValue as AnimationIterationCount; use properties::longhands::animation_iteration_count::single_value::SpecifiedValue as AnimationIterationCount;
debug_assert!(!v.0.is_empty()); let v = v.into_iter();
let input_len = v.0.len();
debug_assert!(v.len() != 0);
let input_len = v.len();
unsafe { self.gecko.mAnimations.ensure_len(input_len) }; unsafe { self.gecko.mAnimations.ensure_len(input_len) };
self.gecko.mAnimationIterationCountCount = input_len as u32; self.gecko.mAnimationIterationCountCount = input_len as u32;
for (i, gecko) in self.gecko.mAnimations.iter_mut().enumerate() { for (gecko, servo) in self.gecko.mAnimations.iter_mut().zip(v.cycle()) {
match v.0[i % input_len] { match servo {
AnimationIterationCount::Number(n) => gecko.mIterationCount = n, AnimationIterationCount::Number(n) => gecko.mIterationCount = n,
AnimationIterationCount::Infinite => gecko.mIterationCount = f32::INFINITY, AnimationIterationCount::Infinite => gecko.mIterationCount = f32::INFINITY,
} }
@ -2558,18 +2591,21 @@ fn static_assert() {
other.gecko.${image_layers_field}.${field_name}Count; other.gecko.${image_layers_field}.${field_name}Count;
} }
pub fn set_${shorthand}_${name}(&mut self,
v: longhands::${shorthand}_${name}::computed_value::T) { pub fn set_${shorthand}_${name}<I>(&mut self, v: I)
where I: IntoIterator<Item=longhands::${shorthand}_${name}::computed_value::single_value::T>,
I::IntoIter: ExactSizeIterator
{
use gecko_bindings::structs::nsStyleImageLayers_LayerType as LayerType; use gecko_bindings::structs::nsStyleImageLayers_LayerType as LayerType;
let v = v.into_iter();
unsafe { unsafe {
Gecko_EnsureImageLayersLength(&mut self.gecko.${image_layers_field}, v.0.len(), Gecko_EnsureImageLayersLength(&mut self.gecko.${image_layers_field}, v.len(),
LayerType::${shorthand.title()}); LayerType::${shorthand.title()});
} }
self.gecko.${image_layers_field}.${field_name}Count = v.0.len() as u32; self.gecko.${image_layers_field}.${field_name}Count = v.len() as u32;
for (servo, geckolayer) in v.0.into_iter() for (servo, geckolayer) in v.zip(self.gecko.${image_layers_field}.mLayers.iter_mut()) {
.zip(self.gecko.${image_layers_field}.mLayers.iter_mut()) {
geckolayer.${field_name} = { geckolayer.${field_name} = {
${caller.body()} ${caller.body()}
}; };
@ -2675,17 +2711,23 @@ fn static_assert() {
) )
} }
pub fn set_${shorthand}_position_${orientation[0]}(&mut self, pub fn set_${shorthand}_position_${orientation[0]}<I>(&mut self,
v: longhands::${shorthand}_position_${orientation[0]}::computed_value::T) { v: I)
where I: IntoIterator<Item = longhands::${shorthand}_position_${orientation[0]}
::computed_value::single_value::T>,
I::IntoIter: ExactSizeIterator
{
use gecko_bindings::structs::nsStyleImageLayers_LayerType as LayerType; use gecko_bindings::structs::nsStyleImageLayers_LayerType as LayerType;
let v = v.into_iter();
unsafe { unsafe {
Gecko_EnsureImageLayersLength(&mut self.gecko.${image_layers_field}, v.0.len(), Gecko_EnsureImageLayersLength(&mut self.gecko.${image_layers_field}, v.len(),
LayerType::${shorthand.capitalize()}); LayerType::${shorthand.capitalize()});
} }
self.gecko.${image_layers_field}.mPosition${orientation[0].upper()}Count = v.0.len() as u32; self.gecko.${image_layers_field}.mPosition${orientation[0].upper()}Count = v.len() as u32;
for (servo, geckolayer) in v.0.into_iter().zip(self.gecko.${image_layers_field} for (servo, geckolayer) in v.zip(self.gecko.${image_layers_field}
.mLayers.iter_mut()) { .mLayers.iter_mut()) {
geckolayer.mPosition.m${orientation[0].upper()}Position = servo.0.into(); geckolayer.mPosition.m${orientation[0].upper()}Position = servo.0.into();
} }
@ -2772,24 +2814,27 @@ fn static_assert() {
} }
#[allow(unused_variables)] #[allow(unused_variables)]
pub fn set_${shorthand}_image(&mut self, pub fn set_${shorthand}_image<I>(&mut self, images: I, cacheable: &mut bool)
images: longhands::${shorthand}_image::computed_value::T, where I: IntoIterator<Item = longhands::${shorthand}_image::computed_value::single_value::T>,
cacheable: &mut bool) { I::IntoIter: ExactSizeIterator
{
use gecko_bindings::structs::nsStyleImageLayers_LayerType as LayerType; use gecko_bindings::structs::nsStyleImageLayers_LayerType as LayerType;
let images = images.into_iter();
unsafe { unsafe {
// Prevent leaking of the last elements we did set // Prevent leaking of the last elements we did set
for image in &mut self.gecko.${image_layers_field}.mLayers { for image in &mut self.gecko.${image_layers_field}.mLayers {
Gecko_SetNullImageValue(&mut image.mImage) Gecko_SetNullImageValue(&mut image.mImage)
} }
// XXXManishearth clear mSourceURI for masks // XXXManishearth clear mSourceURI for masks
Gecko_EnsureImageLayersLength(&mut self.gecko.${image_layers_field}, images.0.len(), Gecko_EnsureImageLayersLength(&mut self.gecko.${image_layers_field}, images.len(),
LayerType::${shorthand.title()}); LayerType::${shorthand.title()});
} }
self.gecko.${image_layers_field}.mImageCount = images.0.len() as u32; self.gecko.${image_layers_field}.mImageCount = images.len() as u32;
for (image, geckoimage) in images.0.into_iter().zip(self.gecko.${image_layers_field} for (image, geckoimage) in images.zip(self.gecko.${image_layers_field}
.mLayers.iter_mut()) { .mLayers.iter_mut()) {
if let Some(image) = image.0 { if let Some(image) = image.0 {
geckoimage.mImage.set(image, cacheable) geckoimage.mImage.set(image, cacheable)
@ -2986,12 +3031,15 @@ fn static_assert() {
<%self:impl_trait style_struct_name="Effects" <%self:impl_trait style_struct_name="Effects"
skip_longhands="box-shadow clip filter"> skip_longhands="box-shadow clip filter">
pub fn set_box_shadow(&mut self, v: longhands::box_shadow::computed_value::T) { pub fn set_box_shadow<I>(&mut self, v: I)
where I: IntoIterator<Item = longhands::box_shadow::computed_value::single_value::T>,
I::IntoIter: ExactSizeIterator
{
let v = v.into_iter();
self.gecko.mBoxShadow.replace_with_new(v.0.len() as u32); self.gecko.mBoxShadow.replace_with_new(v.len() as u32);
for (servo, gecko_shadow) in v.0.into_iter() for (servo, gecko_shadow) in v.zip(self.gecko.mBoxShadow.iter_mut()) {
.zip(self.gecko.mBoxShadow.iter_mut()) {
gecko_shadow.mXOffset = servo.offset_x.0; gecko_shadow.mXOffset = servo.offset_x.0;
gecko_shadow.mYOffset = servo.offset_y.0; gecko_shadow.mYOffset = servo.offset_y.0;
@ -3790,12 +3838,16 @@ clip-path
${impl_simple_copy('paint_order', 'mPaintOrder')} ${impl_simple_copy('paint_order', 'mPaintOrder')}
pub fn set_stroke_dasharray(&mut self, v: longhands::stroke_dasharray::computed_value::T) { pub fn set_stroke_dasharray<I>(&mut self, v: I)
where I: IntoIterator<Item = longhands::stroke_dasharray::computed_value::single_value::T>,
I::IntoIter: ExactSizeIterator
{
let v = v.into_iter();
unsafe { unsafe {
bindings::Gecko_nsStyleSVG_SetDashArrayLength(&mut self.gecko, v.0.len() as u32); bindings::Gecko_nsStyleSVG_SetDashArrayLength(&mut self.gecko, v.len() as u32);
} }
for (mut gecko, servo) in self.gecko.mStrokeDasharray.iter_mut().zip(v.0.into_iter()) { for (mut gecko, servo) in self.gecko.mStrokeDasharray.iter_mut().zip(v) {
match servo { match servo {
Either::First(number) => gecko.set_value(CoordDataValue::Factor(number)), Either::First(number) => gecko.set_value(CoordDataValue::Factor(number)),
Either::Second(lop) => gecko.set(lop), Either::Second(lop) => gecko.set(lop),

View file

@ -74,7 +74,7 @@
</%doc> </%doc>
<%def name="vector_longhand(name, gecko_only=False, allow_empty=False, <%def name="vector_longhand(name, gecko_only=False, allow_empty=False,
delegate_animate=False, space_separated_allowed=False, **kwargs)"> delegate_animate=False, space_separated_allowed=False, **kwargs)">
<%call expr="longhand(name, **kwargs)"> <%call expr="longhand(name, vector=True, **kwargs)">
% if not gecko_only: % if not gecko_only:
use smallvec::SmallVec; use smallvec::SmallVec;
use std::fmt; use std::fmt;
@ -102,7 +102,9 @@
pub mod computed_value { pub mod computed_value {
pub use super::single_value::computed_value as single_value; pub use super::single_value::computed_value as single_value;
pub use self::single_value::T as SingleComputedValue; pub use self::single_value::T as SingleComputedValue;
use smallvec::SmallVec; use smallvec::{IntoIter, SmallVec};
use values::computed::ComputedVecIter;
/// The computed value, effectively a list of single values. /// The computed value, effectively a list of single values.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
@ -129,6 +131,16 @@
} }
} }
% endif % endif
pub type Iter<'a, 'cx, 'cx_a> = ComputedVecIter<'a, 'cx, 'cx_a, super::single_value::SpecifiedValue>;
impl IntoIterator for T {
type Item = single_value::T;
type IntoIter = IntoIter<[single_value::T; 1]>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
} }
impl ToCss for computed_value::T { impl ToCss for computed_value::T {
@ -212,12 +224,19 @@
pub use self::single_value::SpecifiedValue as SingleSpecifiedValue; pub use self::single_value::SpecifiedValue as SingleSpecifiedValue;
impl SpecifiedValue {
pub fn compute_iter<'a, 'cx, 'cx_a>(&'a self, context: &'cx Context<'cx_a>)
-> computed_value::Iter<'a, 'cx, 'cx_a> {
computed_value::Iter::new(context, &self.0)
}
}
impl ToComputedValue for SpecifiedValue { impl ToComputedValue for SpecifiedValue {
type ComputedValue = computed_value::T; type ComputedValue = computed_value::T;
#[inline] #[inline]
fn to_computed_value(&self, context: &Context) -> computed_value::T { fn to_computed_value(&self, context: &Context) -> computed_value::T {
computed_value::T(self.0.iter().map(|x| x.to_computed_value(context)).collect()) computed_value::T(self.compute_iter(context).collect())
} }
#[inline] #[inline]
fn from_computed_value(computed: &computed_value::T) -> Self { fn from_computed_value(computed: &computed_value::T) -> Self {
@ -294,7 +313,10 @@
% if property.logical: % if property.logical:
let wm = context.style.writing_mode; let wm = context.style.writing_mode;
% endif % endif
<% maybe_wm = ", wm" if property.logical else "" %> <%
maybe_wm = ", wm" if property.logical else ""
maybe_cacheable = ", cacheable" if property.has_uncacheable_values == "True" else ""
%>
match *value { match *value {
DeclaredValue::Value(ref specified_value) => { DeclaredValue::Value(ref specified_value) => {
% if property.ident in SYSTEM_FONT_LONGHANDS and product == "gecko": % if property.ident in SYSTEM_FONT_LONGHANDS and product == "gecko":
@ -302,6 +324,21 @@
longhands::system_font::resolve_system_font(sf, context); longhands::system_font::resolve_system_font(sf, context);
} }
% endif % endif
% if property.is_vector:
// In the case of a vector property we want to pass down
// an iterator so that this can be computed without allocation
//
// However, computing requires a context, but the style struct
// being mutated is on the context. We temporarily remove it,
// mutate it, and then put it back. Vector longhands cannot
// touch their own style struct whilst computing, else this will panic.
let mut s = context.mutate_style().take_${data.current_style_struct.name_lower}();
{
let iter = specified_value.compute_iter(context);
s.set_${property.ident}(iter ${maybe_cacheable});
}
context.mutate_style().put_${data.current_style_struct.name_lower}(s);
% else:
let computed = specified_value.to_computed_value(context); let computed = specified_value.to_computed_value(context);
% if property.ident == "font_size": % if property.ident == "font_size":
longhands::font_size::cascade_specified_font_size(context, longhands::font_size::cascade_specified_font_size(context,
@ -309,12 +346,8 @@
computed, computed,
inherited_style.get_font()); inherited_style.get_font());
% else: % else:
% if property.has_uncacheable_values == "True":
context.mutate_style().mutate_${data.current_style_struct.name_lower}() context.mutate_style().mutate_${data.current_style_struct.name_lower}()
.set_${property.ident}(computed, cacheable ${maybe_wm}); .set_${property.ident}(computed ${maybe_cacheable} ${maybe_wm});
% else:
context.mutate_style().mutate_${data.current_style_struct.name_lower}()
.set_${property.ident}(computed ${maybe_wm});
% endif % endif
% endif % endif
} }

View file

@ -14,7 +14,7 @@ use std::borrow::Cow;
use std::collections::HashSet; use std::collections::HashSet;
use std::fmt; use std::fmt;
use std::ops::Deref; use std::ops::Deref;
use stylearc::Arc; use stylearc::{Arc, UniqueArc};
use app_units::Au; use app_units::Au;
#[cfg(feature = "servo")] use cssparser::{Color as CSSParserColor, RGBA}; #[cfg(feature = "servo")] use cssparser::{Color as CSSParserColor, RGBA};
@ -1508,6 +1508,19 @@ pub mod style_structs {
% for longhand in style_struct.longhands: % for longhand in style_struct.longhands:
% if longhand.logical: % if longhand.logical:
${helpers.logical_setter(name=longhand.name)} ${helpers.logical_setter(name=longhand.name)}
% else:
% if longhand.is_vector:
/// Set ${longhand.name}.
#[allow(non_snake_case)]
#[inline]
pub fn set_${longhand.ident}<I>(&mut self, v: I)
where I: IntoIterator<Item = longhands::${longhand.ident}
::computed_value::single_value::T>,
I::IntoIter: ExactSizeIterator
{
self.${longhand.ident} = longhands::${longhand.ident}::computed_value
::T(v.into_iter().collect());
}
% else: % else:
/// Set ${longhand.name}. /// Set ${longhand.name}.
#[allow(non_snake_case)] #[allow(non_snake_case)]
@ -1515,6 +1528,7 @@ pub mod style_structs {
pub fn set_${longhand.ident}(&mut self, v: longhands::${longhand.ident}::computed_value::T) { pub fn set_${longhand.ident}(&mut self, v: longhands::${longhand.ident}::computed_value::T) {
self.${longhand.ident} = v; self.${longhand.ident} = v;
} }
% endif
/// Set ${longhand.name} from other struct. /// Set ${longhand.name} from other struct.
#[allow(non_snake_case)] #[allow(non_snake_case)]
#[inline] #[inline]
@ -2057,7 +2071,9 @@ pub enum StyleStructRef<'a, T: 'a> {
/// A borrowed struct from the parent, for example, for inheriting style. /// A borrowed struct from the parent, for example, for inheriting style.
Borrowed(&'a Arc<T>), Borrowed(&'a Arc<T>),
/// An owned struct, that we've already mutated. /// An owned struct, that we've already mutated.
Owned(Arc<T>), Owned(UniqueArc<T>),
/// Temporarily vacated, will panic if accessed
Vacated,
} }
impl<'a, T: 'a> StyleStructRef<'a, T> impl<'a, T: 'a> StyleStructRef<'a, T>
@ -2067,21 +2083,45 @@ impl<'a, T: 'a> StyleStructRef<'a, T>
/// borrowed value, or returning the owned one. /// borrowed value, or returning the owned one.
pub fn mutate(&mut self) -> &mut T { pub fn mutate(&mut self) -> &mut T {
if let StyleStructRef::Borrowed(v) = *self { if let StyleStructRef::Borrowed(v) = *self {
*self = StyleStructRef::Owned(Arc::new((**v).clone())); *self = StyleStructRef::Owned(UniqueArc::new((**v).clone()));
} }
match *self { match *self {
StyleStructRef::Owned(ref mut v) => Arc::get_mut(v).unwrap(), StyleStructRef::Owned(ref mut v) => v,
StyleStructRef::Borrowed(..) => unreachable!(), StyleStructRef::Borrowed(..) => unreachable!(),
StyleStructRef::Vacated => panic!("Accessed vacated style struct")
} }
} }
/// Extract a unique Arc from this struct, vacating it.
///
/// The vacated state is a transient one, please put the Arc back
/// when done via `put()`. This function is to be used to separate
/// the struct being mutated from the computed context
pub fn take(&mut self) -> UniqueArc<T> {
use std::mem::replace;
let inner = replace(self, StyleStructRef::Vacated);
match inner {
StyleStructRef::Owned(arc) => arc,
StyleStructRef::Borrowed(arc) => UniqueArc::new((**arc).clone()),
StyleStructRef::Vacated => panic!("Accessed vacated style struct"),
}
}
/// Replace vacated ref with an arc
pub fn put(&mut self, arc: UniqueArc<T>) {
debug_assert!(matches!(*self, StyleStructRef::Vacated));
*self = StyleStructRef::Owned(arc);
}
/// Get a mutable reference to the owned struct, or `None` if the struct /// Get a mutable reference to the owned struct, or `None` if the struct
/// hasn't been mutated. /// hasn't been mutated.
pub fn get_if_mutated(&mut self) -> Option<<&mut T> { pub fn get_if_mutated(&mut self) -> Option<<&mut T> {
match *self { match *self {
StyleStructRef::Owned(ref mut v) => Some(Arc::get_mut(v).unwrap()), StyleStructRef::Owned(ref mut v) => Some(v),
StyleStructRef::Borrowed(..) => None, StyleStructRef::Borrowed(..) => None,
StyleStructRef::Vacated => panic!("Accessed vacated style struct")
} }
} }
@ -2089,8 +2129,9 @@ impl<'a, T: 'a> StyleStructRef<'a, T>
/// appropriate. /// appropriate.
pub fn build(self) -> Arc<T> { pub fn build(self) -> Arc<T> {
match self { match self {
StyleStructRef::Owned(v) => v, StyleStructRef::Owned(v) => v.shareable(),
StyleStructRef::Borrowed(v) => v.clone(), StyleStructRef::Borrowed(v) => v.clone(),
StyleStructRef::Vacated => panic!("Accessed vacated style struct")
} }
} }
} }
@ -2102,6 +2143,7 @@ impl<'a, T: 'a> Deref for StyleStructRef<'a, T> {
match *self { match *self {
StyleStructRef::Owned(ref v) => &**v, StyleStructRef::Owned(ref v) => &**v,
StyleStructRef::Borrowed(v) => &**v, StyleStructRef::Borrowed(v) => &**v,
StyleStructRef::Vacated => panic!("Accessed vacated style struct")
} }
} }
} }
@ -2183,6 +2225,16 @@ impl<'a> StyleBuilder<'a> {
self.${style_struct.ident}.mutate() self.${style_struct.ident}.mutate()
} }
/// Gets a mutable view of the current `${style_struct.name}` style.
pub fn take_${style_struct.name_lower}(&mut self) -> UniqueArc<style_structs::${style_struct.name}> {
self.${style_struct.ident}.take()
}
/// Gets a mutable view of the current `${style_struct.name}` style.
pub fn put_${style_struct.name_lower}(&mut self, s: UniqueArc<style_structs::${style_struct.name}>) {
self.${style_struct.ident}.put(s)
}
/// Gets a mutable view of the current `${style_struct.name}` style, /// Gets a mutable view of the current `${style_struct.name}` style,
/// only if it's been mutated before. /// only if it's been mutated before.
pub fn get_${style_struct.name_lower}_if_mutated(&mut self) pub fn get_${style_struct.name_lower}_if_mutated(&mut self)

View file

@ -31,7 +31,7 @@ use std::convert::From;
use std::fmt; use std::fmt;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::mem; use std::mem;
use std::ops::Deref; use std::ops::{Deref, DerefMut};
use std::sync::atomic; use std::sync::atomic;
use std::sync::atomic::Ordering::{Acquire, Relaxed, Release}; use std::sync::atomic::Ordering::{Acquire, Relaxed, Release};
@ -71,6 +71,40 @@ pub struct Arc<T: ?Sized> {
ptr: *mut ArcInner<T>, ptr: *mut ArcInner<T>,
} }
/// An Arc that is known to be uniquely owned
///
/// This lets us build arcs that we can mutate before
/// freezing, without needing to change the allocation
pub struct UniqueArc<T: ?Sized>(Arc<T>);
impl<T> UniqueArc<T> {
#[inline]
/// Construct a new UniqueArc
pub fn new(data: T) -> Self {
UniqueArc(Arc::new(data))
}
#[inline]
/// Convert to a shareable Arc<T> once we're done using it
pub fn shareable(self) -> Arc<T> {
self.0
}
}
impl<T> Deref for UniqueArc<T> {
type Target = T;
fn deref(&self) -> &T {
&*self.0
}
}
impl<T> DerefMut for UniqueArc<T> {
fn deref_mut(&mut self) -> &mut T {
// We know this to be uniquely owned
unsafe { &mut (*self.0.ptr).data }
}
}
unsafe impl<T: ?Sized + Sync + Send> Send for Arc<T> {} unsafe impl<T: ?Sized + Sync + Send> Send for Arc<T> {}
unsafe impl<T: ?Sized + Sync + Send> Sync for Arc<T> {} unsafe impl<T: ?Sized + Sync + Send> Sync for Arc<T> {}

View file

@ -100,6 +100,42 @@ impl<'a> Context<'a> {
pub fn mutate_style(&mut self) -> &mut StyleBuilder<'a> { &mut self.style } pub fn mutate_style(&mut self) -> &mut StyleBuilder<'a> { &mut self.style }
} }
/// An iterator over a slice of computed values
#[derive(Clone)]
pub struct ComputedVecIter<'a, 'cx, 'cx_a: 'cx, S: ToComputedValue + 'a> {
cx: &'cx Context<'cx_a>,
values: &'a [S],
}
impl<'a, 'cx, 'cx_a: 'cx, S: ToComputedValue + 'a> ComputedVecIter<'a, 'cx, 'cx_a, S> {
/// Construct an iterator from a slice of specified values and a context
pub fn new(cx: &'cx Context<'cx_a>, values: &'a [S]) -> Self {
ComputedVecIter {
cx: cx,
values: values,
}
}
}
impl<'a, 'cx, 'cx_a: 'cx, S: ToComputedValue + 'a> ExactSizeIterator for ComputedVecIter<'a, 'cx, 'cx_a, S> {
fn len(&self) -> usize {
self.values.len()
}
}
impl<'a, 'cx, 'cx_a: 'cx, S: ToComputedValue + 'a> Iterator for ComputedVecIter<'a, 'cx, 'cx_a, S> {
type Item = S::ComputedValue;
fn next(&mut self) -> Option<Self::Item> {
if let Some((next, rest)) = self.values.split_first() {
let ret = next.to_computed_value(self.cx);
self.values = rest;
Some(ret)
} else {
None
}
}
}
/// A trait to represent the conversion between computed and specified values. /// A trait to represent the conversion between computed and specified values.
pub trait ToComputedValue { pub trait ToComputedValue {
/// The computed value type we're going to be converted to. /// The computed value type we're going to be converted to.