From 6ac3fcbfeb6e36ced0b20d6e6483f07513d842c6 Mon Sep 17 00:00:00 2001 From: Ravi Shankar Date: Thu, 17 Nov 2016 20:16:34 +0530 Subject: [PATCH] Prefer Either for LengthOrNumber --- components/style/gecko/values.rs | 36 ++++++-------- .../style/properties/longhand/border.mako.rs | 8 ++-- components/style/values/computed/length.rs | 48 +------------------ components/style/values/specified/length.rs | 38 +-------------- 4 files changed, 20 insertions(+), 110 deletions(-) diff --git a/components/style/gecko/values.rs b/components/style/gecko/values.rs index 5537cdac6e4d..3abb746added 100644 --- a/components/style/gecko/values.rs +++ b/components/style/gecko/values.rs @@ -11,8 +11,8 @@ use gecko_bindings::structs::nsStyleCoord; use gecko_bindings::sugar::ns_style_coord::{CoordData, CoordDataMut, CoordDataValue}; use std::cmp::max; use values::Either; -use values::computed::{LengthOrNumber, LengthOrPercentage, LengthOrPercentageOrAuto}; -use values::computed::{LengthOrPercentageOrNone, Angle}; +use values::computed::{Angle, LengthOrPercentageOrNone, Number}; +use values::computed::{LengthOrPercentage, LengthOrPercentageOrAuto}; use values::computed::basic_shape::ShapeRadius; pub trait StyleCoordHelpers { @@ -26,7 +26,6 @@ impl StyleCoordHelpers for nsStyleCoord { } } - pub trait GeckoStyleCoordConvertible : Sized { fn to_gecko_style_coord(&self, coord: &mut T); fn from_gecko_style_coord(coord: &T) -> Option; @@ -47,6 +46,19 @@ impl GeckoStyleCoo } } +impl GeckoStyleCoordConvertible for Number { + fn to_gecko_style_coord(&self, coord: &mut T) { + coord.set_value(CoordDataValue::Factor(*self)); + } + + fn from_gecko_style_coord(coord: &T) -> Option { + match coord.as_value() { + CoordDataValue::Factor(f) => Some(f), + _ => None, + } + } +} + impl GeckoStyleCoordConvertible for LengthOrPercentage { fn to_gecko_style_coord(&self, coord: &mut T) { let value = match *self { @@ -124,24 +136,6 @@ impl GeckoStyleCoordConvertible for LengthOrPercentageOrNone { } } -impl GeckoStyleCoordConvertible for LengthOrNumber { - fn to_gecko_style_coord(&self, coord: &mut T) { - let value = match *self { - LengthOrNumber::Length(au) => CoordDataValue::Coord(au.0), - LengthOrNumber::Number(number) => CoordDataValue::Factor(number), - }; - coord.set_value(value); - } - - fn from_gecko_style_coord(coord: &T) -> Option { - match coord.as_value() { - CoordDataValue::Coord(coord) => Some(LengthOrNumber::Length(Au(coord))), - CoordDataValue::Factor(f) => Some(LengthOrNumber::Number(f)), - _ => None, - } - } -} - impl GeckoStyleCoordConvertible for ShapeRadius { fn to_gecko_style_coord(&self, coord: &mut T) { match *self { diff --git a/components/style/properties/longhand/border.mako.rs b/components/style/properties/longhand/border.mako.rs index 10aa2dd26989..57bd1e82f890 100644 --- a/components/style/properties/longhand/border.mako.rs +++ b/components/style/properties/longhand/border.mako.rs @@ -195,15 +195,13 @@ ${helpers.single_keyword("-moz-float-edge", "content-box margin-box", #[inline] pub fn get_initial_value() -> computed_value::T { - computed_value::T(computed::LengthOrNumber::Number(0.0), - computed::LengthOrNumber::Number(0.0), - computed::LengthOrNumber::Number(0.0), - computed::LengthOrNumber::Number(0.0)) + computed_value::T(Either::Second(0.0), Either::Second(0.0), + Either::Second(0.0), Either::Second(0.0)) } #[inline] pub fn get_initial_specified_value() -> SpecifiedValue { - SpecifiedValue(vec![LengthOrNumber::Number(Number(0.0))]) + SpecifiedValue(vec![Either::Second(Number(0.0))]) } impl ToComputedValue for SpecifiedValue { diff --git a/components/style/values/computed/length.rs b/components/style/values/computed/length.rs index 4536041fdd46..2d32e9c4614a 100644 --- a/components/style/values/computed/length.rs +++ b/components/style/values/computed/length.rs @@ -471,52 +471,6 @@ impl ToCss for LengthOrPercentageOrNone { pub type LengthOrNone = Either; -#[derive(Clone, PartialEq)] -#[cfg_attr(feature = "servo", derive(HeapSizeOf))] -pub enum LengthOrNumber { - Length(Length), - Number(Number), -} - -impl fmt::Debug for LengthOrNumber { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - LengthOrNumber::Length(length) => write!(f, "{:?}", length), - LengthOrNumber::Number(number) => write!(f, "{:?}", number), - } - } -} - -impl ToComputedValue for specified::LengthOrNumber { - type ComputedValue = LengthOrNumber; - - #[inline] - fn to_computed_value(&self, context: &Context) -> LengthOrNumber { - match *self { - specified::LengthOrNumber::Length(len) => - LengthOrNumber::Length(len.to_computed_value(context)), - specified::LengthOrNumber::Number(number) => - LengthOrNumber::Number(number.to_computed_value(context)), - } - } - #[inline] - fn from_computed_value(computed: &LengthOrNumber) -> Self { - match *computed { - LengthOrNumber::Length(len) => - specified::LengthOrNumber::Length(ToComputedValue::from_computed_value(&len)), - LengthOrNumber::Number(number) => - specified::LengthOrNumber::Number(ToComputedValue::from_computed_value(&number)), - } - } -} - -impl ToCss for LengthOrNumber { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { - match *self { - LengthOrNumber::Length(len) => len.to_css(dest), - LengthOrNumber::Number(number) => number.to_css(dest), - } - } -} +pub type LengthOrNumber = Either; pub type Length = Au; diff --git a/components/style/values/specified/length.rs b/components/style/values/specified/length.rs index a96271647167..26eb45e9201e 100644 --- a/components/style/values/specified/length.rs +++ b/components/style/values/specified/length.rs @@ -1008,40 +1008,4 @@ impl Parse for LengthOrPercentageOrAutoOrContent { } } -#[derive(Debug, Clone, PartialEq)] -#[cfg_attr(feature = "servo", derive(HeapSizeOf))] -pub enum LengthOrNumber { - Length(Length), - Number(Number), -} - -impl HasViewportPercentage for LengthOrNumber { - fn has_viewport_percentage(&self) -> bool { - match *self { - LengthOrNumber::Length(length) => length.has_viewport_percentage(), - _ => false - } - } -} - -impl ToCss for LengthOrNumber { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { - match *self { - LengthOrNumber::Length(len) => len.to_css(dest), - LengthOrNumber::Number(number) => number.to_css(dest), - } - } -} - -impl Parse for LengthOrNumber { - fn parse(input: &mut Parser) -> Result { - let length = input.try(Length::parse); - if let Ok(len) = length { - return Ok(LengthOrNumber::Length(len)); - } - - let num = try!(Number::parse_non_negative(input)); - Ok(LengthOrNumber::Number(num)) - } -} - +pub type LengthOrNumber = Either;