Skip to content

Commit

Permalink
Make use of Either<A, B> for LengthOrNone
Browse files Browse the repository at this point in the history
  • Loading branch information
wafflespeanut committed Nov 9, 2016
1 parent 5df250b commit c4fc49c
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 116 deletions.
11 changes: 5 additions & 6 deletions components/layout/display_list_builder.rs
Expand Up @@ -52,9 +52,8 @@ use style::logical_geometry::{LogicalPoint, LogicalRect, LogicalSize, WritingMod
use style::properties::{self, ServoComputedValues};
use style::properties::style_structs;
use style::servo::restyle_damage::REPAINT;
use style::values::RGBA;
use style::values::computed;
use style::values::computed::{Gradient, GradientKind, LengthOrNone, LengthOrPercentage, LengthOrPercentageOrAuto};
use style::values::{self, Either, RGBA, computed};
use style::values::computed::{Gradient, GradientKind, LengthOrPercentage, LengthOrPercentageOrAuto};
use style::values::specified::{AngleOrCorner, HorizontalDirection, VerticalDirection};
use style_traits::cursor::Cursor;
use table_cell::CollapsedBordersForCell;
Expand Down Expand Up @@ -1563,7 +1562,7 @@ impl FragmentDisplayListBuilding for Fragment {

let transform = self.transform_matrix(&border_box);
let perspective = match self.style().get_effects().perspective {
LengthOrNone::Length(d) => {
Either::First(length) => {
let perspective_origin = self.style().get_effects().perspective_origin;
let perspective_origin =
Point2D::new(model::specified(perspective_origin.horizontal,
Expand All @@ -1578,11 +1577,11 @@ impl FragmentDisplayListBuilding for Fragment {
-perspective_origin.y,
0.0);

let perspective_matrix = create_perspective_matrix(d);
let perspective_matrix = create_perspective_matrix(length);

pre_transform.pre_mul(&perspective_matrix).pre_mul(&post_transform)
}
LengthOrNone::None => {
Either::Second(values::None_) => {
Matrix4D::identity()
}
};
Expand Down
5 changes: 3 additions & 2 deletions components/layout/fragment.rs
Expand Up @@ -49,7 +49,8 @@ use style::properties::ServoComputedValues;
use style::selector_impl::RestyleDamage;
use style::servo::restyle_damage::RECONSTRUCT_FLOW;
use style::str::char_is_whitespace;
use style::values::computed::{LengthOrNone, LengthOrPercentage, LengthOrPercentageOrAuto};
use style::values::Either;
use style::values::computed::{LengthOrPercentage, LengthOrPercentageOrAuto};
use style::values::computed::LengthOrPercentageOrNone;
use text;
use text::TextRunScanner;
Expand Down Expand Up @@ -2579,7 +2580,7 @@ impl Fragment {

// TODO(mrobinson): Determine if this is necessary, since blocks with
// transformations already create stacking contexts.
if self.style().get_effects().perspective != LengthOrNone::None {
if let Either::First(ref _length) = self.style().get_effects().perspective {
return true
}

Expand Down
2 changes: 1 addition & 1 deletion components/style/properties/helpers.mako.rs
Expand Up @@ -178,7 +178,7 @@
use parser::{ParserContext, ParserContextExtraData};
use properties::{CSSWideKeyword, DeclaredValue, Shorthand};
% endif
#[allow(unused_imports)]
use values::{Auto, Either, None_, Normal};
use cascade_info::CascadeInfo;
use error_reporting::ParseErrorReporter;
use parser::Parse;
Expand Down
Expand Up @@ -22,6 +22,7 @@ use std::cmp;
use std::fmt;
use style_traits::ToCss;
use super::ComputedValues;
use values::Either;
use values::computed::{Angle, LengthOrPercentageOrAuto, LengthOrPercentageOrNone};
use values::computed::{BorderRadiusSize, LengthOrNone};
use values::computed::{CalcLengthOrPercentage, LengthOrPercentage};
Expand Down Expand Up @@ -682,8 +683,8 @@ impl Interpolate for BoxShadow {
impl Interpolate for LengthOrNone {
fn interpolate(&self, other: &Self, progress: f64) -> Result<Self, ()> {
match (*self, *other) {
(LengthOrNone::Length(ref len), LengthOrNone::Length(ref other)) =>
len.interpolate(&other, progress).map(LengthOrNone::Length),
(Either::First(ref length), Either::First(ref other)) =>
length.interpolate(&other, progress).map(Either::First),
_ => Err(()),
}
}
Expand Down
2 changes: 1 addition & 1 deletion components/style/properties/longhand/effects.mako.rs
Expand Up @@ -1395,7 +1395,7 @@ ${helpers.single_keyword("transform-style",

${helpers.predefined_type("perspective",
"LengthOrNone",
"computed::LengthOrNone::None",
"Either::Second(None_)",
products="servo",
animatable=True)}

Expand Down
3 changes: 2 additions & 1 deletion components/style/properties/properties.mako.rs
Expand Up @@ -30,6 +30,7 @@ use logical_geometry::WritingMode;
use parser::{ParserContext, ParserContextExtraData};
use style_traits::ToCss;
use stylesheets::Origin;
#[cfg(feature = "servo")] use values::Either;
use values::{HasViewportPercentage, computed};
use cascade_info::CascadeInfo;
use rule_tree::StrongRuleNode;
Expand Down Expand Up @@ -1249,7 +1250,7 @@ impl ComputedValues {
if effects.transform.0.is_some() {
return transform_style::T::flat;
}
if effects.perspective != computed::LengthOrNone::None {
if let Either::First(ref _length) = effects.perspective {
return transform_style::T::flat;
}
}
Expand Down
58 changes: 2 additions & 56 deletions components/style/values/computed/length.rs
Expand Up @@ -7,7 +7,7 @@ use ordered_float::NotNaN;
use std::fmt;
use style_traits::ToCss;
use super::{Number, ToComputedValue, Context};
use values::{CSSFloat, specified};
use values::{CSSFloat, Either, None_, specified};

pub use cssparser::Color as CSSColor;
pub use super::image::{EndingShape as GradientShape, Gradient, GradientKind, Image};
Expand Down Expand Up @@ -452,61 +452,7 @@ impl ToCss for LengthOrPercentageOrNone {
}
}

#[derive(PartialEq, Clone, Copy)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub enum LengthOrNone {
Length(Au),
None,
}

impl fmt::Debug for LengthOrNone {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
LengthOrNone::Length(length) => write!(f, "{:?}", length),
LengthOrNone::None => write!(f, "none"),
}
}
}

impl ToComputedValue for specified::LengthOrNone {
type ComputedValue = LengthOrNone;

#[inline]
fn to_computed_value(&self, context: &Context) -> LengthOrNone {
match *self {
specified::LengthOrNone::Length(specified::Length::Calc(calc, range)) => {
LengthOrNone::Length(range.clamp(calc.to_computed_value(context).length()))
}
specified::LengthOrNone::Length(value) => {
LengthOrNone::Length(value.to_computed_value(context))
}
specified::LengthOrNone::None => {
LengthOrNone::None
}
}
}

#[inline]
fn from_computed_value(computed: &LengthOrNone) -> Self {
match *computed {
LengthOrNone::Length(au) => {
specified::LengthOrNone::Length(ToComputedValue::from_computed_value(&au))
}
LengthOrNone::None => {
specified::LengthOrNone::None
}
}
}
}

impl ToCss for LengthOrNone {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
LengthOrNone::Length(length) => length.to_css(dest),
LengthOrNone::None => dest.write_str("none"),
}
}
}
pub type LengthOrNone = Either<Length, None_>;

#[derive(Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
Expand Down
1 change: 1 addition & 0 deletions components/style/values/computed/mod.rs
Expand Up @@ -12,6 +12,7 @@ use super::{CSSFloat, specified};
pub use cssparser::Color as CSSColor;
pub use self::image::{EndingShape as GradientShape, Gradient, GradientKind, Image};
pub use self::image::{LengthOrKeyword, LengthOrPercentageOrKeyword};
pub use super::{Either, None_};
pub use super::specified::{Angle, BorderStyle, Time, UrlExtraData, UrlOrNone};
pub use self::length::{CalcLengthOrPercentage, Length, LengthOrNumber, LengthOrPercentage, LengthOrPercentageOrAuto};
pub use self::length::{LengthOrPercentageOrAutoOrContent, LengthOrPercentageOrNone, LengthOrNone};
Expand Down
49 changes: 3 additions & 46 deletions components/style/values/specified/length.rs
Expand Up @@ -13,7 +13,7 @@ use std::ops::Mul;
use style_traits::ToCss;
use style_traits::values::specified::AllowedNumericType;
use super::{Angle, Number, SimplifiedValueNode, SimplifiedSumNode, Time};
use values::{CSSFloat, FONT_MEDIUM_PX, HasViewportPercentage, computed};
use values::{CSSFloat, Either, FONT_MEDIUM_PX, HasViewportPercentage, None_, computed};

pub use super::image::{AngleOrCorner, ColorStop, EndingShape as GradientEndingShape, Gradient};
pub use super::image::{GradientKind, HorizontalDirection, Image, LengthOrKeyword, LengthOrPercentageOrKeyword};
Expand Down Expand Up @@ -908,55 +908,12 @@ impl LengthOrPercentageOrNone {
}
}

#[derive(Clone, PartialEq, Copy, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub enum LengthOrNone {
Length(Length),
None,
}

impl HasViewportPercentage for LengthOrNone {
fn has_viewport_percentage(&self) -> bool {
match *self {
LengthOrNone::Length(ref length) => length.has_viewport_percentage(),
_ => false
}
}
}
pub type LengthOrNone = Either<Length, None_>;

impl ToCss for LengthOrNone {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
LengthOrNone::Length(length) => length.to_css(dest),
LengthOrNone::None => dest.write_str("none"),
}
}
}
impl LengthOrNone {
fn parse_internal(input: &mut Parser, context: AllowedNumericType)
-> Result<LengthOrNone, ()>
{
match try!(input.next()) {
Token::Dimension(ref value, ref unit) if context.is_ok(value.value) =>
Length::parse_dimension(value.value, unit).map(LengthOrNone::Length),
Token::Number(ref value) if value.value == 0. =>
Ok(LengthOrNone::Length(Length::Absolute(Au(0)))),
Token::Function(ref name) if name.eq_ignore_ascii_case("calc") =>
input.parse_nested_block(|input| {
CalcLengthOrPercentage::parse_length(input, context)
}).map(LengthOrNone::Length),
Token::Ident(ref value) if value.eq_ignore_ascii_case("none") =>
Ok(LengthOrNone::None),
_ => Err(())
}
}
#[inline]
pub fn parse(input: &mut Parser) -> Result<LengthOrNone, ()> {
LengthOrNone::parse_internal(input, AllowedNumericType::All)
}
#[inline]
pub fn parse_non_negative(input: &mut Parser) -> Result<LengthOrNone, ()> {
LengthOrNone::parse_internal(input, AllowedNumericType::NonNegative)
Length::parse_internal(input, AllowedNumericType::NonNegative).map(Either::First)
}
}

Expand Down
1 change: 0 additions & 1 deletion components/style/values/specified/mod.rs
Expand Up @@ -83,7 +83,6 @@ impl ToCss for CSSRGBA {
}
}


#[derive(Clone, Debug)]
pub struct SimplifiedSumNode {
values: Vec<SimplifiedValueNode>,
Expand Down

0 comments on commit c4fc49c

Please sign in to comment.