From 4144dc74db8e082991b1ef6a6ed6e665f1ea6fd8 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Sat, 27 May 2017 10:32:49 +0200 Subject: [PATCH] Make Rect a struct tuple It makes no sense to have named fields in some cases, notably to reuse Rect in BorderRadius. --- components/layout/display_list_builder.rs | 8 +-- components/style/properties/gecko.mako.rs | 6 +- components/style/properties/helpers.mako.rs | 4 +- .../style/properties/shorthand/border.mako.rs | 17 +++--- components/style/values/generics/rect.rs | 61 ++++++++----------- 5 files changed, 41 insertions(+), 55 deletions(-) diff --git a/components/layout/display_list_builder.rs b/components/layout/display_list_builder.rs index b0549cde28a..4ecbbd98cc2 100644 --- a/components/layout/display_list_builder.rs +++ b/components/layout/display_list_builder.rs @@ -1424,10 +1424,10 @@ impl FragmentDisplayListBuilding for Fragment { details: BorderDetails::Image(ImageBorder { image: webrender_image, fill: border_style_struct.border_image_slice.fill, - slice: SideOffsets2D::new(corners.top.resolve(webrender_image.height), - corners.right.resolve(webrender_image.width), - corners.bottom.resolve(webrender_image.height), - corners.left.resolve(webrender_image.width)), + slice: SideOffsets2D::new(corners.0.resolve(webrender_image.height), + corners.1.resolve(webrender_image.width), + corners.2.resolve(webrender_image.height), + corners.3.resolve(webrender_image.width)), // TODO(gw): Support border-image-outset outset: SideOffsets2D::zero(), repeat_horizontal: convert_repeat_mode(border_style_struct.border_image_repeat.0), diff --git a/components/style/properties/gecko.mako.rs b/components/style/properties/gecko.mako.rs index aeacf2bbefc..c946d8c3033 100644 --- a/components/style/properties/gecko.mako.rs +++ b/components/style/properties/gecko.mako.rs @@ -964,7 +964,7 @@ fn static_assert() { pub fn set_border_image_outset(&mut self, v: longhands::border_image_outset::computed_value::T) { % for side in SIDES: - v.${side.ident}.to_gecko_style_coord(&mut self.gecko.mBorderImageOutset.data_at_mut(${side.index})); + v.${side.index}.to_gecko_style_coord(&mut self.gecko.mBorderImageOutset.data_at_mut(${side.index})); % endfor } @@ -1001,7 +1001,7 @@ fn static_assert() { use values::generics::border::BorderImageWidthSide; % for side in SIDES: - match v.${side.ident} { + match v.${side.index} { BorderImageWidthSide::Auto => { self.gecko.mBorderImageWidth.data_at_mut(${side.index}).set_value(CoordDataValue::Auto) }, @@ -1026,7 +1026,7 @@ fn static_assert() { use gecko_bindings::structs::{NS_STYLE_BORDER_IMAGE_SLICE_NOFILL, NS_STYLE_BORDER_IMAGE_SLICE_FILL}; % for side in SIDES: - v.offsets.${side.ident}.to_gecko_style_coord(&mut self.gecko.mBorderImageSlice.data_at_mut(${side.index})); + v.offsets.${side.index}.to_gecko_style_coord(&mut self.gecko.mBorderImageSlice.data_at_mut(${side.index})); % endfor let fill = if v.fill { diff --git a/components/style/properties/helpers.mako.rs b/components/style/properties/helpers.mako.rs index 0c629df19f5..d161b60129e 100644 --- a/components/style/properties/helpers.mako.rs +++ b/components/style/properties/helpers.mako.rs @@ -901,8 +901,8 @@ % endif })?; Ok(expanded! { - % for side in ["top", "right", "bottom", "left"]: - ${to_rust_ident(sub_property_pattern % side)}: rect.${side}, + % for index, side in enumerate(["top", "right", "bottom", "left"]): + ${to_rust_ident(sub_property_pattern % side)}: rect.${index}, % endfor }) } diff --git a/components/style/properties/shorthand/border.mako.rs b/components/style/properties/shorthand/border.mako.rs index 67e807b824b..51645ea97ed 100644 --- a/components/style/properties/shorthand/border.mako.rs +++ b/components/style/properties/shorthand/border.mako.rs @@ -25,20 +25,19 @@ ${helpers.four_sides_shorthand("border-style", "border-%s-style", BorderWidth::parse_quirky(context, i, AllowQuirks::Yes) })?; Ok(expanded! { - % for side in PHYSICAL_SIDES: - ${to_rust_ident('border-%s-width' % side)}: rect.${side}, - % endfor + border_top_width: rect.0, + border_right_width: rect.1, + border_bottom_width: rect.2, + border_left_width: rect.3, }) } impl<'a> ToCss for LonghandsToSerialize<'a> { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { - let rect = Rect { - % for side in PHYSICAL_SIDES: - ${side}: &self.border_${side}_width, - % endfor - }; - rect.to_css(dest) + % for side in PHYSICAL_SIDES: + let ${side} = &self.border_${side}_width; + % endfor + Rect::new(top, right, bottom, left).to_css(dest) } } diff --git a/components/style/values/generics/rect.rs b/components/style/values/generics/rect.rs index 767ce2223e4..a0a63b7144e 100644 --- a/components/style/values/generics/rect.rs +++ b/components/style/values/generics/rect.rs @@ -9,29 +9,16 @@ use parser::{Parse, ParserContext}; use std::fmt; use style_traits::ToCss; -/// A CSS value made of four sides: top, right, bottom, and left. +/// A CSS value made of four components, where its `ToCss` impl will try to +/// serialize as few components as possible, like for example in `border-width`. #[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] -pub struct Rect { - /// Top - pub top: T, - /// Right. - pub right: T, - /// Bottom. - pub bottom: T, - /// Left. - pub left: T, -} +pub struct Rect(pub T, pub T, pub T, pub T); impl Rect { /// Returns a new `Rect` value. - pub fn new(top: T, right: T, bottom: T, left: T) -> Self { - Rect { - top: top, - right: right, - bottom: bottom, - left: left, - } + pub fn new(first: T, second: T, third: T, fourth: T) -> Self { + Rect(first, second, third, fourth) } } @@ -46,21 +33,21 @@ impl Rect -> Result where Parse: Fn(&ParserContext, &mut Parser) -> Result { - let top = parse(context, input)?; - let right = if let Ok(right) = input.try(|i| parse(context, i)) { right } else { - // - return Ok(Self::new(top.clone(), top.clone(), top.clone(), top)); + let first = parse(context, input)?; + let second = if let Ok(second) = input.try(|i| parse(context, i)) { second } else { + // + return Ok(Self::new(first.clone(), first.clone(), first.clone(), first)); }; - let bottom = if let Ok(bottom) = input.try(|i| parse(context, i)) { bottom } else { - // - return Ok(Self::new(top.clone(), right.clone(), top, right)); + let third = if let Ok(third) = input.try(|i| parse(context, i)) { third } else { + // + return Ok(Self::new(first.clone(), second.clone(), first, second)); }; - let left = if let Ok(left) = input.try(|i| parse(context, i)) { left } else { - // - return Ok(Self::new(top, right.clone(), bottom, right)); + let fourth = if let Ok(fourth) = input.try(|i| parse(context, i)) { fourth } else { + // + return Ok(Self::new(first, second.clone(), third, second)); }; - // - Ok(Self::new(top, right, bottom, left)) + // + Ok(Self::new(first, second, third, fourth)) } } @@ -88,23 +75,23 @@ impl ToCss for Rect fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write, { - self.top.to_css(dest)?; - let same_vertical = self.top == self.bottom; - let same_horizontal = self.right == self.left; - if same_vertical && same_horizontal && self.top == self.right { + self.0.to_css(dest)?; + let same_vertical = self.0 == self.2; + let same_horizontal = self.1 == self.3; + if same_vertical && same_horizontal && self.0 == self.1 { return Ok(()); } dest.write_str(" ")?; - self.right.to_css(dest)?; + self.1.to_css(dest)?; if same_vertical && same_horizontal { return Ok(()); } dest.write_str(" ")?; - self.bottom.to_css(dest)?; + self.2.to_css(dest)?; if same_horizontal { return Ok(()); } dest.write_str(" ")?; - self.left.to_css(dest) + self.3.to_css(dest) } }