Make Rect<T> a struct tuple

It makes no sense to have named fields in some cases, notably to reuse
Rect<T> in BorderRadius<T>.
This commit is contained in:
Anthony Ramine 2017-05-27 10:32:49 +02:00
parent 4ec2e8b4c5
commit 4144dc74db
5 changed files with 41 additions and 55 deletions

View file

@ -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),

View file

@ -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 {

View file

@ -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
})
}

View file

@ -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<W>(&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)
}
}
</%helpers:shorthand>

View file

@ -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<T> {
/// Top
pub top: T,
/// Right.
pub right: T,
/// Bottom.
pub bottom: T,
/// Left.
pub left: T,
}
pub struct Rect<T>(pub T, pub T, pub T, pub T);
impl<T> Rect<T> {
/// Returns a new `Rect<T>` 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<T> Rect<T>
-> Result<Self, ()>
where Parse: Fn(&ParserContext, &mut Parser) -> Result<T, ()>
{
let top = parse(context, input)?;
let right = if let Ok(right) = input.try(|i| parse(context, i)) { right } else {
// <top>
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 {
// <first>
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 {
// <top> <right>
return Ok(Self::new(top.clone(), right.clone(), top, right));
let third = if let Ok(third) = input.try(|i| parse(context, i)) { third } else {
// <first> <second>
return Ok(Self::new(first.clone(), second.clone(), first, second));
};
let left = if let Ok(left) = input.try(|i| parse(context, i)) { left } else {
// <top> <right> <bottom>
return Ok(Self::new(top, right.clone(), bottom, right));
let fourth = if let Ok(fourth) = input.try(|i| parse(context, i)) { fourth } else {
// <first> <second> <third>
return Ok(Self::new(first, second.clone(), third, second));
};
// <top> <right> <bottom> <left>
Ok(Self::new(top, right, bottom, left))
// <first> <second> <third> <fourth>
Ok(Self::new(first, second, third, fourth))
}
}
@ -88,23 +75,23 @@ impl<T> ToCss for Rect<T>
fn to_css<W>(&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)
}
}