Skip to content

Commit

Permalink
Prefer Either<A, B> for LengthOrNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
wafflespeanut committed Nov 18, 2016
1 parent 73eabad commit 6ac3fcb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 110 deletions.
36 changes: 15 additions & 21 deletions components/style/gecko/values.rs
Expand Up @@ -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 {
Expand All @@ -26,7 +26,6 @@ impl StyleCoordHelpers for nsStyleCoord {
}
}


pub trait GeckoStyleCoordConvertible : Sized {
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T);
fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self>;
Expand All @@ -47,6 +46,19 @@ impl<A: GeckoStyleCoordConvertible, B: GeckoStyleCoordConvertible> GeckoStyleCoo
}
}

impl GeckoStyleCoordConvertible for Number {
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
coord.set_value(CoordDataValue::Factor(*self));
}

fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self> {
match coord.as_value() {
CoordDataValue::Factor(f) => Some(f),
_ => None,
}
}
}

impl GeckoStyleCoordConvertible for LengthOrPercentage {
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
let value = match *self {
Expand Down Expand Up @@ -124,24 +136,6 @@ impl GeckoStyleCoordConvertible for LengthOrPercentageOrNone {
}
}

impl GeckoStyleCoordConvertible for LengthOrNumber {
fn to_gecko_style_coord<T: CoordDataMut>(&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<T: CoordData>(coord: &T) -> Option<Self> {
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<T: CoordDataMut>(&self, coord: &mut T) {
match *self {
Expand Down
8 changes: 3 additions & 5 deletions components/style/properties/longhand/border.mako.rs
Expand Up @@ -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 {
Expand Down
48 changes: 1 addition & 47 deletions components/style/values/computed/length.rs
Expand Up @@ -471,52 +471,6 @@ impl ToCss for LengthOrPercentageOrNone {

pub type LengthOrNone = Either<Length, None_>;

#[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<W>(&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<Length, Number>;

pub type Length = Au;
38 changes: 1 addition & 37 deletions components/style/values/specified/length.rs
Expand Up @@ -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<W>(&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<Self, ()> {
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<Length, Number>;

0 comments on commit 6ac3fcb

Please sign in to comment.