diff --git a/components/style/properties/data.py b/components/style/properties/data.py index 8db09e6fe81..eb1706e691e 100644 --- a/components/style/properties/data.py +++ b/components/style/properties/data.py @@ -140,7 +140,7 @@ class Longhand(object): 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', 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 if not spec: raise TypeError("Spec should be specified for %s" % name) @@ -167,6 +167,7 @@ class Longhand(object): self.flags = flags.split() if flags else [] self.allowed_in_page_rule = arg_to_bool(allowed_in_page_rule) self.allow_quirks = allow_quirks + self.is_vector = vector # https://drafts.csswg.org/css-animations/#keyframes # > The inside of accepts any CSS property diff --git a/components/style/properties/gecko.mako.rs b/components/style/properties/gecko.mako.rs index c453bfae688..f1c80ff31b0 100644 --- a/components/style/properties/gecko.mako.rs +++ b/components/style/properties/gecko.mako.rs @@ -1697,14 +1697,18 @@ fn static_assert() { <%def name="impl_animation_or_transition_time_value(type, ident, gecko_ffi_name)"> #[allow(non_snake_case)] - pub fn set_${type}_${ident}(&mut self, v: longhands::${type}_${ident}::computed_value::T) { - debug_assert!(!v.0.is_empty()); - let input_len = v.0.len(); + pub fn set_${type}_${ident}(&mut self, v: I) + where I: IntoIterator, + 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) }; 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() { - gecko.m${gecko_ffi_name} = v.0[i % input_len].seconds() * 1000.; + for (gecko, servo) in self.gecko.m${type.capitalize()}s.iter_mut().zip(v.cycle()) { + gecko.m${gecko_ffi_name} = servo.seconds() * 1000.; } } #[allow(non_snake_case)] @@ -1718,14 +1722,18 @@ fn static_assert() { <%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) { - debug_assert!(!v.0.is_empty()); - let input_len = v.0.len(); + pub fn set_${type}_timing_function(&mut self, v: I) + where I: IntoIterator, + 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) }; self.gecko.m${type.capitalize()}TimingFunctionCount = input_len as u32; - for (i, gecko) in self.gecko.m${type.capitalize()}s.iter_mut().enumerate() { - gecko.mTimingFunction = v.0[i % input_len].into(); + for (gecko, servo) in self.gecko.m${type.capitalize()}s.iter_mut().zip(v.cycle()) { + gecko.mTimingFunction = servo.into(); } } ${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')"> #[allow(non_snake_case)] - pub fn set_animation_${ident}(&mut self, v: longhands::animation_${ident}::computed_value::T) { + pub fn set_animation_${ident}(&mut self, v: I) + where I: IntoIterator, + I::IntoIter: ExactSizeIterator + Clone + { use properties::longhands::animation_${ident}::single_value::computed_value::T as Keyword; use gecko_bindings::structs; - debug_assert!(!v.0.is_empty()); - let input_len = v.0.len(); + let v = v.into_iter(); + + debug_assert!(v.len() != 0); + let input_len = v.len(); unsafe { self.gecko.mAnimations.ensure_len(input_len) }; self.gecko.mAnimation${gecko_ffi_name}Count = input_len as u32; - for (i, gecko) in self.gecko.mAnimations.iter_mut().enumerate() { - let result = match v.0[i % input_len] { + for (gecko, servo) in self.gecko.mAnimations.iter_mut().zip(v.cycle()) { + let result = match servo { % for value in keyword.gecko_values(): Keyword::${to_rust_ident(value)} => 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')} - pub fn set_scroll_snap_coordinate(&mut self, v: longhands::scroll_snap_coordinate::computed_value::T) { - unsafe { self.gecko.mScrollSnapCoordinate.set_len_pod(v.0.len() as u32); } + pub fn set_scroll_snap_coordinate(&mut self, v: I) + where I: IntoIterator, + 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 .iter_mut() - .zip(v.0.iter()) { + .zip(v) { gecko.mXPosition = servo.horizontal.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 } - pub fn set_transition_property(&mut self, v: longhands::transition_property::computed_value::T) { + pub fn set_transition_property(&mut self, v: I) + where I: IntoIterator, + I::IntoIter: ExactSizeIterator + { use gecko_bindings::structs::nsCSSPropertyID::eCSSPropertyExtra_no_properties; - if !v.0.is_empty() { - unsafe { self.gecko.mTransitions.ensure_len(v.0.len()) }; - self.gecko.mTransitionPropertyCount = v.0.len() as u32; - for (servo, gecko) in v.0.into_iter().zip(self.gecko.mTransitions.iter_mut()) { + let v = v.into_iter(); + + if v.len() != 0 { + 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 { TransitionProperty::Unsupported(ref atom) => unsafe { Gecko_StyleTransition_SetUnsupportedProperty(gecko, atom.as_ptr()) @@ -2246,12 +2269,17 @@ fn static_assert() { unsafe { bindings::Gecko_StyleAnimationsEquals(&self.gecko.mAnimations, &other.gecko.mAnimations) } } - pub fn set_animation_name(&mut self, v: longhands::animation_name::computed_value::T) { - debug_assert!(!v.0.is_empty()); - unsafe { self.gecko.mAnimations.ensure_len(v.0.len()) }; + pub fn set_animation_name(&mut self, v: I) + where I: IntoIterator, + I::IntoIter: ExactSizeIterator + { - self.gecko.mAnimationNameCount = v.0.len() as u32; - for (servo, gecko) in v.0.into_iter().zip(self.gecko.mAnimations.iter_mut()) { + let v = v.into_iter(); + 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. gecko.mName.assign(match servo.0 { Some(ref name) => name.as_atom().as_slice(), @@ -2294,17 +2322,22 @@ fn static_assert() { ${impl_animation_keyword('play_state', 'PlayState', 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(&mut self, v: I) + where I: IntoIterator, + I::IntoIter: ExactSizeIterator + Clone + { use std::f32; use properties::longhands::animation_iteration_count::single_value::SpecifiedValue as AnimationIterationCount; - debug_assert!(!v.0.is_empty()); - let input_len = v.0.len(); + let v = v.into_iter(); + + debug_assert!(v.len() != 0); + let input_len = v.len(); unsafe { self.gecko.mAnimations.ensure_len(input_len) }; self.gecko.mAnimationIterationCountCount = input_len as u32; - for (i, gecko) in self.gecko.mAnimations.iter_mut().enumerate() { - match v.0[i % input_len] { + for (gecko, servo) in self.gecko.mAnimations.iter_mut().zip(v.cycle()) { + match servo { AnimationIterationCount::Number(n) => gecko.mIterationCount = n, AnimationIterationCount::Infinite => gecko.mIterationCount = f32::INFINITY, } @@ -2558,18 +2591,21 @@ fn static_assert() { 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}(&mut self, v: I) + where I: IntoIterator, + I::IntoIter: ExactSizeIterator + { use gecko_bindings::structs::nsStyleImageLayers_LayerType as LayerType; + let v = v.into_iter(); 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()}); } - self.gecko.${image_layers_field}.${field_name}Count = v.0.len() as u32; - for (servo, geckolayer) in v.0.into_iter() - .zip(self.gecko.${image_layers_field}.mLayers.iter_mut()) { + self.gecko.${image_layers_field}.${field_name}Count = v.len() as u32; + for (servo, geckolayer) in v.zip(self.gecko.${image_layers_field}.mLayers.iter_mut()) { geckolayer.${field_name} = { ${caller.body()} }; @@ -2675,17 +2711,23 @@ fn static_assert() { ) } - pub fn set_${shorthand}_position_${orientation[0]}(&mut self, - v: longhands::${shorthand}_position_${orientation[0]}::computed_value::T) { + pub fn set_${shorthand}_position_${orientation[0]}(&mut self, + v: I) + where I: IntoIterator, + I::IntoIter: ExactSizeIterator + { use gecko_bindings::structs::nsStyleImageLayers_LayerType as LayerType; + let v = v.into_iter(); + 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()}); } - self.gecko.${image_layers_field}.mPosition${orientation[0].upper()}Count = v.0.len() as u32; - for (servo, geckolayer) in v.0.into_iter().zip(self.gecko.${image_layers_field} + self.gecko.${image_layers_field}.mPosition${orientation[0].upper()}Count = v.len() as u32; + for (servo, geckolayer) in v.zip(self.gecko.${image_layers_field} .mLayers.iter_mut()) { geckolayer.mPosition.m${orientation[0].upper()}Position = servo.0.into(); } @@ -2772,25 +2814,28 @@ fn static_assert() { } #[allow(unused_variables)] - pub fn set_${shorthand}_image(&mut self, - images: longhands::${shorthand}_image::computed_value::T, - cacheable: &mut bool) { + pub fn set_${shorthand}_image(&mut self, images: I, cacheable: &mut bool) + where I: IntoIterator, + I::IntoIter: ExactSizeIterator + { use gecko_bindings::structs::nsStyleImageLayers_LayerType as LayerType; + let images = images.into_iter(); + unsafe { // Prevent leaking of the last elements we did set for image in &mut self.gecko.${image_layers_field}.mLayers { Gecko_SetNullImageValue(&mut image.mImage) } // 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()}); } - 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} - .mLayers.iter_mut()) { + for (image, geckoimage) in images.zip(self.gecko.${image_layers_field} + .mLayers.iter_mut()) { if let Some(image) = image.0 { geckoimage.mImage.set(image, cacheable) } @@ -2986,12 +3031,15 @@ fn static_assert() { <%self:impl_trait style_struct_name="Effects" 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(&mut self, v: I) + where I: IntoIterator, + 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() - .zip(self.gecko.mBoxShadow.iter_mut()) { + for (servo, gecko_shadow) in v.zip(self.gecko.mBoxShadow.iter_mut()) { gecko_shadow.mXOffset = servo.offset_x.0; gecko_shadow.mYOffset = servo.offset_y.0; @@ -3790,12 +3838,16 @@ clip-path ${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(&mut self, v: I) + where I: IntoIterator, + I::IntoIter: ExactSizeIterator + { + let v = v.into_iter(); 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 { Either::First(number) => gecko.set_value(CoordDataValue::Factor(number)), Either::Second(lop) => gecko.set(lop), diff --git a/components/style/properties/helpers.mako.rs b/components/style/properties/helpers.mako.rs index b69057f916c..9f89abda857 100644 --- a/components/style/properties/helpers.mako.rs +++ b/components/style/properties/helpers.mako.rs @@ -74,7 +74,7 @@ <%def name="vector_longhand(name, gecko_only=False, allow_empty=False, delegate_animate=False, space_separated_allowed=False, **kwargs)"> - <%call expr="longhand(name, **kwargs)"> + <%call expr="longhand(name, vector=True, **kwargs)"> % if not gecko_only: use smallvec::SmallVec; use std::fmt; @@ -102,7 +102,9 @@ pub mod computed_value { pub use super::single_value::computed_value as single_value; 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. #[derive(Debug, Clone, PartialEq)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] @@ -129,6 +131,16 @@ } } % 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 { @@ -212,12 +224,19 @@ 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 { type ComputedValue = computed_value::T; #[inline] 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] fn from_computed_value(computed: &computed_value::T) -> Self { @@ -294,7 +313,10 @@ % if property.logical: let wm = context.style.writing_mode; % 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 { DeclaredValue::Value(ref specified_value) => { % if property.ident in SYSTEM_FONT_LONGHANDS and product == "gecko": @@ -302,19 +324,30 @@ longhands::system_font::resolve_system_font(sf, context); } % endif - let computed = specified_value.to_computed_value(context); - % if property.ident == "font_size": - longhands::font_size::cascade_specified_font_size(context, - specified_value, - computed, - inherited_style.get_font()); + % 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: - % if property.has_uncacheable_values == "True": - context.mutate_style().mutate_${data.current_style_struct.name_lower}() - .set_${property.ident}(computed, cacheable ${maybe_wm}); + let computed = specified_value.to_computed_value(context); + % if property.ident == "font_size": + longhands::font_size::cascade_specified_font_size(context, + specified_value, + computed, + inherited_style.get_font()); % else: - context.mutate_style().mutate_${data.current_style_struct.name_lower}() - .set_${property.ident}(computed ${maybe_wm}); + context.mutate_style().mutate_${data.current_style_struct.name_lower}() + .set_${property.ident}(computed ${maybe_cacheable} ${maybe_wm}); % endif % endif } diff --git a/components/style/properties/properties.mako.rs b/components/style/properties/properties.mako.rs index b2a122adcbe..566263252fb 100644 --- a/components/style/properties/properties.mako.rs +++ b/components/style/properties/properties.mako.rs @@ -14,7 +14,7 @@ use std::borrow::Cow; use std::collections::HashSet; use std::fmt; use std::ops::Deref; -use stylearc::Arc; +use stylearc::{Arc, UniqueArc}; use app_units::Au; #[cfg(feature = "servo")] use cssparser::{Color as CSSParserColor, RGBA}; @@ -1509,12 +1509,26 @@ pub mod style_structs { % if longhand.logical: ${helpers.logical_setter(name=longhand.name)} % else: - /// Set ${longhand.name}. - #[allow(non_snake_case)] - #[inline] - pub fn set_${longhand.ident}(&mut self, v: longhands::${longhand.ident}::computed_value::T) { - self.${longhand.ident} = v; - } + % if longhand.is_vector: + /// Set ${longhand.name}. + #[allow(non_snake_case)] + #[inline] + pub fn set_${longhand.ident}(&mut self, v: I) + where I: IntoIterator, + I::IntoIter: ExactSizeIterator + { + self.${longhand.ident} = longhands::${longhand.ident}::computed_value + ::T(v.into_iter().collect()); + } + % else: + /// Set ${longhand.name}. + #[allow(non_snake_case)] + #[inline] + pub fn set_${longhand.ident}(&mut self, v: longhands::${longhand.ident}::computed_value::T) { + self.${longhand.ident} = v; + } + % endif /// Set ${longhand.name} from other struct. #[allow(non_snake_case)] #[inline] @@ -2057,7 +2071,9 @@ pub enum StyleStructRef<'a, T: 'a> { /// A borrowed struct from the parent, for example, for inheriting style. Borrowed(&'a Arc), /// An owned struct, that we've already mutated. - Owned(Arc), + Owned(UniqueArc), + /// Temporarily vacated, will panic if accessed + Vacated, } 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. pub fn mutate(&mut self) -> &mut T { if let StyleStructRef::Borrowed(v) = *self { - *self = StyleStructRef::Owned(Arc::new((**v).clone())); + *self = StyleStructRef::Owned(UniqueArc::new((**v).clone())); } match *self { - StyleStructRef::Owned(ref mut v) => Arc::get_mut(v).unwrap(), + StyleStructRef::Owned(ref mut v) => v, 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 { + 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) { + debug_assert!(matches!(*self, StyleStructRef::Vacated)); + *self = StyleStructRef::Owned(arc); + } + /// Get a mutable reference to the owned struct, or `None` if the struct /// hasn't been mutated. pub fn get_if_mutated(&mut self) -> Option<<&mut T> { match *self { - StyleStructRef::Owned(ref mut v) => Some(Arc::get_mut(v).unwrap()), + StyleStructRef::Owned(ref mut v) => Some(v), StyleStructRef::Borrowed(..) => None, + StyleStructRef::Vacated => panic!("Accessed vacated style struct") } } @@ -2089,8 +2129,9 @@ impl<'a, T: 'a> StyleStructRef<'a, T> /// appropriate. pub fn build(self) -> Arc { match self { - StyleStructRef::Owned(v) => v, + StyleStructRef::Owned(v) => v.shareable(), 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 { StyleStructRef::Owned(ref 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() } + /// Gets a mutable view of the current `${style_struct.name}` style. + pub fn take_${style_struct.name_lower}(&mut self) -> UniqueArc { + 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) { + self.${style_struct.ident}.put(s) + } + /// Gets a mutable view of the current `${style_struct.name}` style, /// only if it's been mutated before. pub fn get_${style_struct.name_lower}_if_mutated(&mut self) diff --git a/components/style/stylearc.rs b/components/style/stylearc.rs index 01885dc481b..9b355e7d211 100644 --- a/components/style/stylearc.rs +++ b/components/style/stylearc.rs @@ -31,7 +31,7 @@ use std::convert::From; use std::fmt; use std::hash::{Hash, Hasher}; use std::mem; -use std::ops::Deref; +use std::ops::{Deref, DerefMut}; use std::sync::atomic; use std::sync::atomic::Ordering::{Acquire, Relaxed, Release}; @@ -71,6 +71,40 @@ pub struct Arc { ptr: *mut ArcInner, } +/// 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(Arc); + +impl UniqueArc { + #[inline] + /// Construct a new UniqueArc + pub fn new(data: T) -> Self { + UniqueArc(Arc::new(data)) + } + + #[inline] + /// Convert to a shareable Arc once we're done using it + pub fn shareable(self) -> Arc { + self.0 + } +} + +impl Deref for UniqueArc { + type Target = T; + fn deref(&self) -> &T { + &*self.0 + } +} + +impl DerefMut for UniqueArc { + fn deref_mut(&mut self) -> &mut T { + // We know this to be uniquely owned + unsafe { &mut (*self.0.ptr).data } + } +} + unsafe impl Send for Arc {} unsafe impl Sync for Arc {} diff --git a/components/style/values/computed/mod.rs b/components/style/values/computed/mod.rs index 19cc22edf27..f1721e552cd 100644 --- a/components/style/values/computed/mod.rs +++ b/components/style/values/computed/mod.rs @@ -100,6 +100,42 @@ impl<'a> Context<'a> { 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 { + 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. pub trait ToComputedValue { /// The computed value type we're going to be converted to.