Derive ComputeSquaredDistance

This commit is contained in:
Anthony Ramine 2017-08-13 00:50:36 +02:00
parent 51b740033b
commit 277351da35
22 changed files with 162 additions and 391 deletions

View file

@ -84,7 +84,7 @@ macro_rules! define_keyword_type {
($name: ident, $css: expr) => {
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, PartialEq, ToCss)]
#[derive(Clone, ComputeSquaredDistance, Copy, PartialEq, ToCss)]
pub struct $name;
impl $crate::properties::animated_properties::Animatable for $name {
@ -117,15 +117,5 @@ macro_rules! define_keyword_type {
#[inline]
fn to_animated_zero(&self) -> Result<Self, ()> { Ok($name) }
}
impl $crate::values::distance::ComputeSquaredDistance for $name {
#[inline]
fn compute_squared_distance(
&self,
_other: &Self
) -> Result<$crate::values::distance::SquaredDistance, ()> {
Ok($crate::values::distance::SquaredDistance::Value(0.))
}
}
};
}

View file

@ -119,8 +119,11 @@
use values::computed::ComputedVecIter;
/// The computed value, effectively a list of single values.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Debug, PartialEq)]
% if need_animatable or animation_value_type == "ComputedValue":
#[derive(ComputeSquaredDistance)]
% endif
pub struct T(
% if allow_empty and allow_empty != "NotInitial":
pub Vec<single_value::T>,
@ -132,7 +135,6 @@
% if need_animatable or animation_value_type == "ComputedValue":
use properties::animated_properties::Animatable;
use values::animated::ToAnimatedZero;
use values::distance::{ComputeSquaredDistance, SquaredDistance};
impl Animatable for T {
fn add_weighted(&self, other: &Self, self_portion: f64, other_portion: f64)
@ -145,13 +147,6 @@
}
}
impl ComputeSquaredDistance for T {
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
self.0.compute_squared_distance(&other.0)
}
}
impl ToAnimatedZero for T {
#[inline]
fn to_animated_zero(&self) -> Result<Self, ()> { Err(()) }

View file

@ -480,24 +480,15 @@ ${helpers.single_keyword_system("font-variant-caps",
}
pub mod computed_value {
use values::distance::{ComputeSquaredDistance, SquaredDistance};
/// As of CSS Fonts Module Level 3, only the following values are
/// valid: 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900
///
/// However, system fonts may provide other values. Pango
/// may provide 350, 380, and 1000 (on top of the existing values), for example.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, ToCss)]
#[derive(Clone, ComputeSquaredDistance, Copy, Debug, PartialEq, Eq, Hash, ToCss)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf, Deserialize, Serialize))]
pub struct T(pub u16);
impl ComputeSquaredDistance for T {
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
self.0.compute_squared_distance(&other.0)
}
}
impl T {
/// Value for normal
pub fn normal() -> Self {

View file

@ -28,11 +28,10 @@ ${helpers.single_keyword("caption-side", "top bottom",
pub mod computed_value {
use properties::animated_properties::Animatable;
use values::animated::{ToAnimatedValue, ToAnimatedZero};
use values::distance::{ComputeSquaredDistance, SquaredDistance};
use values::computed::NonNegativeAu;
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, Debug, PartialEq, ToCss)]
#[derive(Clone, ComputeSquaredDistance, Copy, Debug, PartialEq, ToCss)]
pub struct T {
pub horizontal: NonNegativeAu,
pub vertical: NonNegativeAu,
@ -52,16 +51,6 @@ ${helpers.single_keyword("caption-side", "top bottom",
}
}
impl ComputeSquaredDistance for T {
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
Ok(
self.horizontal.compute_squared_distance(&other.horizontal)? +
self.vertical.compute_squared_distance(&other.vertical)?,
)
}
}
impl ToAnimatedZero for T {
#[inline]
fn to_animated_zero(&self) -> Result<Self, ()> { Err(()) }

View file

@ -227,18 +227,6 @@ impl Animatable for SimpleShadow {
}
}
impl ComputeSquaredDistance for SimpleShadow {
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
Ok(
self.color.compute_squared_distance(&other.color)? +
self.horizontal.compute_squared_distance(&other.horizontal)? +
self.vertical.compute_squared_distance(&other.vertical)? +
self.blur.compute_squared_distance(&other.blur)?
)
}
}
impl ToAnimatedZero for SimpleShadow {
#[inline]
fn to_animated_zero(&self) -> Result<Self, ()> {

View file

@ -8,7 +8,6 @@ use properties::animated_properties::{Animatable, RepeatableListAnimatable};
use properties::longhands::background_size::computed_value::T as BackgroundSizeList;
use values::animated::{ToAnimatedValue, ToAnimatedZero};
use values::computed::length::LengthOrPercentageOrAuto;
use values::distance::{ComputeSquaredDistance, SquaredDistance};
use values::generics::background::BackgroundSize as GenericBackgroundSize;
/// A computed value for the `background-size` property.
@ -33,24 +32,6 @@ impl Animatable for BackgroundSize {
}
}
impl ComputeSquaredDistance for BackgroundSize {
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
match (self, other) {
(
&GenericBackgroundSize::Explicit { width: self_width, height: self_height },
&GenericBackgroundSize::Explicit { width: other_width, height: other_height },
) => {
Ok(
self_width.compute_squared_distance(&other_width)? +
self_height.compute_squared_distance(&other_height)?
)
}
_ => Err(()),
}
}
}
impl ToAnimatedZero for BackgroundSize {
#[inline]
fn to_animated_zero(&self) -> Result<Self, ()> { Err(()) }

View file

@ -543,9 +543,9 @@ pub type LengthOrPercentageOrNumber = Either<Number, LengthOrPercentage>;
/// NonNegativeLengthOrPercentage | NonNegativeNumber
pub type NonNegativeLengthOrPercentageOrNumber = Either<NonNegativeNumber, NonNegativeLengthOrPercentage>;
#[derive(Clone, PartialEq, Eq, Copy, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, ComputeSquaredDistance, Copy, Debug, Eq, PartialEq)]
/// A computed cliprect for clip and image-region
pub struct ClipRect {
pub top: Option<Au>,
@ -554,18 +554,6 @@ pub struct ClipRect {
pub left: Option<Au>,
}
impl ComputeSquaredDistance for ClipRect {
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
Ok(
self.top.compute_squared_distance(&other.top)? +
self.right.compute_squared_distance(&other.right)? +
self.bottom.compute_squared_distance(&other.bottom)? +
self.left.compute_squared_distance(&other.left)?,
)
}
}
impl ToCss for ClipRect {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
dest.write_str("rect(")?;
@ -671,17 +659,10 @@ impl From<Au> for NonNegativeAu {
}
/// A computed `<percentage>` value.
#[derive(Clone, Copy, Debug, Default, PartialEq, HasViewportPercentage)]
#[derive(Clone, ComputeSquaredDistance, Copy, Debug, Default, HasViewportPercentage, PartialEq)]
#[cfg_attr(feature = "servo", derive(Deserialize, HeapSizeOf, Serialize))]
pub struct Percentage(pub CSSFloat);
impl ComputeSquaredDistance for Percentage {
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
self.0.compute_squared_distance(&other.0)
}
}
impl Percentage {
/// 0%
#[inline]

View file

@ -9,7 +9,6 @@ use values::{CSSInteger, CSSFloat};
use values::animated::ToAnimatedZero;
use values::computed::{NonNegativeAu, NonNegativeNumber};
use values::computed::length::{Length, LengthOrPercentage};
use values::distance::{ComputeSquaredDistance, SquaredDistance};
use values::generics::text::InitialLetter as GenericInitialLetter;
use values::generics::text::LineHeight as GenericLineHeight;
use values::generics::text::Spacing;
@ -48,28 +47,6 @@ impl Animatable for LineHeight {
}
}
impl ComputeSquaredDistance for LineHeight {
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
match (self, other) {
(&GenericLineHeight::Length(ref this), &GenericLineHeight::Length(ref other)) => {
this.compute_squared_distance(other)
},
(&GenericLineHeight::Number(ref this), &GenericLineHeight::Number(ref other)) => {
this.compute_squared_distance(other)
},
(&GenericLineHeight::Normal, &GenericLineHeight::Normal) => {
Ok(SquaredDistance::Value(0.))
},
#[cfg(feature = "gecko")]
(&GenericLineHeight::MozBlockHeight, &GenericLineHeight::MozBlockHeight) => {
Ok(SquaredDistance::Value(0.))
},
_ => Err(()),
}
}
}
impl ToAnimatedZero for LineHeight {
#[inline]
fn to_animated_zero(&self) -> Result<Self, ()> { Err(()) }

View file

@ -7,7 +7,6 @@
use properties::animated_properties::Animatable;
use values::animated::ToAnimatedZero;
use values::computed::{Length, LengthOrPercentage, Number, Percentage};
use values::distance::{ComputeSquaredDistance, SquaredDistance};
use values::generics::transform::TimingFunction as GenericTimingFunction;
use values::generics::transform::TransformOrigin as GenericTransformOrigin;
@ -40,17 +39,6 @@ impl Animatable for TransformOrigin {
}
}
impl ComputeSquaredDistance for TransformOrigin {
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
Ok(
self.horizontal.compute_squared_distance(&other.horizontal)? +
self.vertical.compute_squared_distance(&other.vertical)? +
self.depth.compute_squared_distance(&other.depth)?
)
}
}
impl ToAnimatedZero for TransformOrigin {
#[inline]
fn to_animated_zero(&self) -> Result<Self, ()> {

View file

@ -5,7 +5,7 @@
//! Generic types for CSS values related to backgrounds.
/// A generic value for the `background-size` property.
#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue, ToCss)]
#[derive(Clone, ComputeSquaredDistance, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue, ToCss)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub enum BackgroundSize<LengthOrPercentageOrAuto> {
/// `<width> <height>`

View file

@ -55,7 +55,7 @@ pub enum ShapeSource<BasicShape, ReferenceBox, Url> {
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Debug, PartialEq, ToComputedValue, ToCss)]
#[derive(Clone, ComputeSquaredDistance, Debug, PartialEq, ToComputedValue, ToCss)]
pub enum BasicShape<H, V, LengthOrPercentage> {
Inset(InsetRect<LengthOrPercentage>),
Circle(Circle<H, V, LengthOrPercentage>),
@ -66,7 +66,7 @@ pub enum BasicShape<H, V, LengthOrPercentage> {
/// https://drafts.csswg.org/css-shapes/#funcdef-inset
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Debug, PartialEq, ToComputedValue)]
#[derive(Clone, ComputeSquaredDistance, Debug, PartialEq, ToComputedValue)]
pub struct InsetRect<LengthOrPercentage> {
pub rect: Rect<LengthOrPercentage>,
pub round: Option<BorderRadius<LengthOrPercentage>>,
@ -75,7 +75,7 @@ pub struct InsetRect<LengthOrPercentage> {
/// https://drafts.csswg.org/css-shapes/#funcdef-circle
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, Debug, PartialEq, ToComputedValue)]
#[derive(Clone, ComputeSquaredDistance, Copy, Debug, PartialEq, ToComputedValue)]
pub struct Circle<H, V, LengthOrPercentage> {
pub position: Position<H, V>,
pub radius: ShapeRadius<LengthOrPercentage>,
@ -84,7 +84,7 @@ pub struct Circle<H, V, LengthOrPercentage> {
/// https://drafts.csswg.org/css-shapes/#funcdef-ellipse
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, Debug, PartialEq, ToComputedValue)]
#[derive(Clone, ComputeSquaredDistance, Copy, Debug, PartialEq, ToComputedValue)]
pub struct Ellipse<H, V, LengthOrPercentage> {
pub position: Position<H, V>,
pub semiaxis_x: ShapeRadius<LengthOrPercentage>,
@ -94,7 +94,7 @@ pub struct Ellipse<H, V, LengthOrPercentage> {
/// https://drafts.csswg.org/css-shapes/#typedef-shape-radius
#[allow(missing_docs)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, Debug, PartialEq, ToComputedValue, ToCss)]
#[derive(Clone, ComputeSquaredDistance, Copy, Debug, PartialEq, ToComputedValue, ToCss)]
pub enum ShapeRadius<LengthOrPercentage> {
Length(LengthOrPercentage),
ClosestSide,
@ -148,7 +148,7 @@ where
}
// FIXME(nox): Implement ComputeSquaredDistance for T types and stop
// using PartialEq here.
// using PartialEq here, this will let us derive this impl.
impl<B, T, U> ComputeSquaredDistance for ShapeSource<B, T, U>
where
B: ComputeSquaredDistance,
@ -208,31 +208,6 @@ where
}
}
impl<H, V, L> ComputeSquaredDistance for BasicShape<H, V, L>
where
H: ComputeSquaredDistance,
V: ComputeSquaredDistance,
L: ComputeSquaredDistance + Copy,
{
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
match (self, other) {
(&BasicShape::Circle(ref this), &BasicShape::Circle(ref other)) => {
this.compute_squared_distance(other)
},
(&BasicShape::Ellipse(ref this), &BasicShape::Ellipse(ref other)) => {
this.compute_squared_distance(other)
},
(&BasicShape::Inset(ref this), &BasicShape::Inset(ref other)) => {
this.compute_squared_distance(other)
},
(&BasicShape::Polygon(ref this), &BasicShape::Polygon(ref other)) => {
this.compute_squared_distance(other)
},
_ => Err(()),
}
}
}
impl<L> Animatable for InsetRect<L>
where
L: Animatable + Copy,
@ -249,18 +224,6 @@ where
}
}
impl<L> ComputeSquaredDistance for InsetRect<L>
where
L: ComputeSquaredDistance + Copy,
{
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
Ok(
self.rect.compute_squared_distance(&other.rect)? +
self.round.compute_squared_distance(&other.round)?,
)
}
}
impl<L> ToCss for InsetRect<L>
where L: ToCss + PartialEq
{
@ -293,20 +256,6 @@ where
}
}
impl<H, V, L> ComputeSquaredDistance for Circle<H, V, L>
where
H: ComputeSquaredDistance,
V: ComputeSquaredDistance,
L: ComputeSquaredDistance,
{
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
Ok(
self.position.compute_squared_distance(&other.position)? +
self.radius.compute_squared_distance(&other.radius)?,
)
}
}
impl<H, V, L> Animatable for Ellipse<H, V, L>
where
H: Animatable,
@ -326,21 +275,6 @@ where
}
}
impl<H, V, L> ComputeSquaredDistance for Ellipse<H, V, L>
where
H: ComputeSquaredDistance,
V: ComputeSquaredDistance,
L: ComputeSquaredDistance,
{
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
Ok(
self.position.compute_squared_distance(&other.position)? +
self.semiaxis_x.compute_squared_distance(&other.semiaxis_x)? +
self.semiaxis_y.compute_squared_distance(&other.semiaxis_y)?,
)
}
}
impl<L> Animatable for ShapeRadius<L>
where
L: Animatable,
@ -360,20 +294,6 @@ where
}
}
impl<L> ComputeSquaredDistance for ShapeRadius<L>
where
L: ComputeSquaredDistance,
{
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
match (self, other) {
(&ShapeRadius::Length(ref this), &ShapeRadius::Length(ref other)) => {
this.compute_squared_distance(other)
},
_ => Err(()),
}
}
}
impl<L> Default for ShapeRadius<L> {
#[inline]
fn default() -> Self { ShapeRadius::ClosestSide }

View file

@ -8,7 +8,6 @@ use euclid::Size2D;
use properties::animated_properties::Animatable;
use std::fmt;
use style_traits::ToCss;
use values::distance::{ComputeSquaredDistance, SquaredDistance};
use values::generics::rect::Rect;
/// A generic value for a single side of a `border-image-width` property.
@ -37,7 +36,7 @@ pub struct BorderImageSlice<NumberOrPercentage> {
///
/// https://drafts.csswg.org/css-backgrounds-3/#border-radius
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue)]
#[derive(Clone, ComputeSquaredDistance, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue)]
pub struct BorderRadius<LengthOrPercentage> {
/// The top left radius.
pub top_left: BorderCornerRadius<LengthOrPercentage>,
@ -49,9 +48,9 @@ pub struct BorderRadius<LengthOrPercentage> {
pub bottom_left: BorderCornerRadius<LengthOrPercentage>,
}
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue)]
/// A generic value for `border-*-radius` longhand properties.
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, ComputeSquaredDistance, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue)]
pub struct BorderCornerRadius<L>(pub Size2D<L>);
impl<N> From<N> for BorderImageSlice<N>
@ -132,20 +131,6 @@ where
}
}
impl<L> ComputeSquaredDistance for BorderRadius<L>
where
L: ComputeSquaredDistance + Copy,
{
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
Ok(
self.top_left.compute_squared_distance(&other.top_left)? +
self.top_right.compute_squared_distance(&other.top_right)? +
self.bottom_right.compute_squared_distance(&other.bottom_right)? +
self.bottom_left.compute_squared_distance(&other.bottom_left)?,
)
}
}
impl<L> ToCss for BorderRadius<L>
where L: PartialEq + ToCss
{
@ -193,16 +178,6 @@ where
}
}
impl<L> ComputeSquaredDistance for BorderCornerRadius<L>
where
L: ComputeSquaredDistance + Copy,
{
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
self.0.compute_squared_distance(&other.0)
}
}
impl<L> ToCss for BorderCornerRadius<L>
where L: ToCss,
{

View file

@ -65,7 +65,7 @@ pub enum Filter<Angle, Factor, Length, DropShadow> {
/// Contrary to the canonical order from the spec, the color is serialised
/// first, like in Gecko and Webkit.
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Debug, HasViewportPercentage, PartialEq, ToAnimatedValue, ToCss)]
#[derive(Clone, ComputeSquaredDistance, Debug, HasViewportPercentage, PartialEq, ToAnimatedValue, ToCss)]
pub struct SimpleShadow<Color, SizeLength, ShapeLength> {
/// Color.
pub color: Color,

View file

@ -11,7 +11,6 @@ use parser::{Parse, ParserContext};
use std::fmt;
use style_traits::{Comma, OneOrMoreSeparated, ParseError, StyleParseError, ToCss};
use super::CustomIdent;
use values::distance::{ComputeSquaredDistance, SquaredDistance};
pub mod background;
pub mod basic_shape;
@ -268,31 +267,13 @@ impl ToCss for FontSettingTagFloat {
}
/// A wrapper of Non-negative values.
#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, PartialOrd, ToComputedValue, ToCss)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf, Deserialize, Serialize))]
#[cfg_attr(feature = "servo", derive(Deserialize, HeapSizeOf, Serialize))]
#[derive(Clone, ComputeSquaredDistance, Copy, Debug, HasViewportPercentage)]
#[derive(PartialEq, PartialOrd, ToComputedValue, ToCss)]
pub struct NonNegative<T>(pub T);
impl<T> ComputeSquaredDistance for NonNegative<T>
where
T: ComputeSquaredDistance,
{
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
self.0.compute_squared_distance(&other.0)
}
}
/// A wrapper of greater-than-or-equal-to-one values.
#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, PartialOrd, ToComputedValue, ToCss)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf, Deserialize, Serialize))]
#[cfg_attr(feature = "servo", derive(Deserialize, HeapSizeOf, Serialize))]
#[derive(Clone, ComputeSquaredDistance, Copy, Debug, HasViewportPercentage)]
#[derive(PartialEq, PartialOrd, ToComputedValue, ToCss)]
pub struct GreaterThanOrEqualToOne<T>(pub T);
impl<T> ComputeSquaredDistance for GreaterThanOrEqualToOne<T>
where
T: ComputeSquaredDistance,
{
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
self.0.compute_squared_distance(&other.0)
}
}

View file

@ -5,11 +5,9 @@
//! Generic types for CSS handling of specified and computed values of
//! [`position`](https://drafts.csswg.org/css-backgrounds-3/#position)
use values::distance::{ComputeSquaredDistance, SquaredDistance};
#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
/// A generic type for representing a CSS [position](https://drafts.csswg.org/css-values/#position).
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, ComputeSquaredDistance, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue)]
pub struct Position<H, V> {
/// The horizontal component of position.
pub horizontal: H,
@ -26,17 +24,3 @@ impl<H, V> Position<H, V> {
}
}
}
impl<H, V> ComputeSquaredDistance for Position<H, V>
where
H: ComputeSquaredDistance,
V: ComputeSquaredDistance,
{
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
Ok(
self.horizontal.compute_squared_distance(&other.horizontal)? +
self.vertical.compute_squared_distance(&other.vertical)?,
)
}
}

View file

@ -9,11 +9,10 @@ use parser::{Parse, ParserContext};
use properties::animated_properties::Animatable;
use std::fmt;
use style_traits::{ToCss, ParseError};
use values::distance::{ComputeSquaredDistance, SquaredDistance};
/// 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)]
#[derive(Clone, ComputeSquaredDistance, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct Rect<T>(pub T, pub T, pub T, pub T);
@ -71,21 +70,6 @@ where
}
}
impl<L> ComputeSquaredDistance for Rect<L>
where
L: ComputeSquaredDistance,
{
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
Ok(
self.0.compute_squared_distance(&other.0)? +
self.1.compute_squared_distance(&other.1)? +
self.2.compute_squared_distance(&other.2)? +
self.3.compute_squared_distance(&other.3)?,
)
}
}
impl<T> From<T> for Rect<T>
where T: Clone
{

View file

@ -6,10 +6,8 @@
use cssparser::Parser;
use parser::{Parse, ParserContext};
use properties::animated_properties::RepeatableListAnimatable;
use std::fmt;
use style_traits::{ParseError, StyleParseError, ToCss};
use values::distance::{ComputeSquaredDistance, SquaredDistance};
/// An SVG paint value
///
@ -99,7 +97,8 @@ impl<ColorType: Parse, UrlPaintServer: Parse> Parse for SVGPaint<ColorType, UrlP
/// An SVG length value supports `context-value` in addition to length.
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, Debug, PartialEq, HasViewportPercentage, ToAnimatedValue, ToComputedValue, ToCss)]
#[derive(Clone, ComputeSquaredDistance, Copy, Debug, PartialEq)]
#[derive(HasViewportPercentage, ToAnimatedValue, ToComputedValue, ToCss)]
pub enum SVGLength<LengthType> {
/// `<length> | <percentage> | <number>`
Length(LengthType),
@ -107,28 +106,9 @@ pub enum SVGLength<LengthType> {
ContextValue,
}
impl<L> ComputeSquaredDistance for SVGLength<L>
where
L: ComputeSquaredDistance,
{
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
match (self, other) {
(&SVGLength::Length(ref this), &SVGLength::Length(ref other)) => {
this.compute_squared_distance(other)
},
_ => {
// FIXME(nox): Should this return `Ok(SquaredDistance::Value(0.))`
// if `self` and `other` are the same keyword value?
Err(())
},
}
}
}
/// Generic value for stroke-dasharray.
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Debug, PartialEq, HasViewportPercentage, ToAnimatedValue, ToComputedValue)]
#[derive(Clone, ComputeSquaredDistance, Debug, PartialEq, HasViewportPercentage, ToAnimatedValue, ToComputedValue)]
pub enum SVGStrokeDashArray<LengthType> {
/// `[ <length> | <percentage> | <number> ]#`
Values(Vec<LengthType>),
@ -136,25 +116,6 @@ pub enum SVGStrokeDashArray<LengthType> {
ContextValue,
}
impl<L> ComputeSquaredDistance for SVGStrokeDashArray<L>
where
L: ComputeSquaredDistance + RepeatableListAnimatable,
{
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
match (self, other) {
(&SVGStrokeDashArray::Values(ref this), &SVGStrokeDashArray::Values(ref other)) => {
this.compute_squared_distance(other)
},
_ => {
// FIXME(nox): Should this return `Ok(SquaredDistance::Value(0.))`
// if `self` and `other` are the same keyword value?
Err(())
},
}
}
}
impl<LengthType> ToCss for SVGStrokeDashArray<LengthType> where LengthType: ToCss {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match self {
@ -181,7 +142,7 @@ impl<LengthType> ToCss for SVGStrokeDashArray<LengthType> where LengthType: ToCs
/// An SVG opacity value accepts `context-{fill,stroke}-opacity` in
/// addition to opacity value.
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, Debug, PartialEq, HasViewportPercentage, ToComputedValue, ToCss)]
#[derive(Clone, ComputeSquaredDistance, Copy, Debug, PartialEq, HasViewportPercentage, ToComputedValue, ToCss)]
pub enum SVGOpacity<OpacityType> {
/// `<opacity-value>`
Opacity(OpacityType),
@ -190,22 +151,3 @@ pub enum SVGOpacity<OpacityType> {
/// `context-stroke-opacity`
ContextStrokeOpacity,
}
impl<L> ComputeSquaredDistance for SVGOpacity<L>
where
L: ComputeSquaredDistance,
{
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
match (self, other) {
(&SVGOpacity::Opacity(ref this), &SVGOpacity::Opacity(ref other)) => {
this.compute_squared_distance(other)
}
_ => {
// FIXME(nox): Should this return `Ok(SquaredDistance::Value(0.))`
// if `self` and `other` are the same keyword value?
Err(())
},
}
}
}

View file

@ -110,7 +110,7 @@ where
/// A generic value for the `line-height` property.
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, ToAnimatedValue, ToCss)]
#[derive(Clone, ComputeSquaredDistance, Copy, Debug, HasViewportPercentage, PartialEq, ToAnimatedValue, ToCss)]
pub enum LineHeight<Number, LengthOrPercentage> {
/// `normal`
Normal,

View file

@ -24,7 +24,7 @@ pub struct Matrix<T, U = T> {
/// A generic transform origin.
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue, ToCss)]
#[derive(Clone, ComputeSquaredDistance, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue, ToCss)]
pub struct TransformOrigin<H, V, Depth> {
/// The horizontal origin.
pub horizontal: H,

View file

@ -16,7 +16,6 @@ use std::ascii::AsciiExt;
use std::fmt::{self, Debug};
use std::hash;
use style_traits::{ToCss, ParseError, StyleParseError};
use values::distance::{ComputeSquaredDistance, SquaredDistance};
pub mod animated;
pub mod computed;
@ -53,7 +52,8 @@ impl Parse for Impossible {
/// A struct representing one of two kinds of values.
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, HasViewportPercentage, PartialEq, ToAnimatedValue, ToComputedValue, ToCss)]
#[derive(Clone, ComputeSquaredDistance, Copy, HasViewportPercentage, PartialEq)]
#[derive(ToAnimatedValue, ToComputedValue, ToCss)]
pub enum Either<A, B> {
/// The first value.
First(A),
@ -61,25 +61,6 @@ pub enum Either<A, B> {
Second(B),
}
impl<A, B> ComputeSquaredDistance for Either<A, B>
where
A: ComputeSquaredDistance,
B: ComputeSquaredDistance,
{
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
match (self, other) {
(&Either::First(ref this), &Either::First(ref other)) => {
this.compute_squared_distance(other)
},
(&Either::Second(ref this), &Either::Second(ref other)) => {
this.compute_squared_distance(other)
},
_ => Err(())
}
}
}
impl<A: Debug, B: Debug> Debug for Either<A, B> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {

View file

@ -0,0 +1,117 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use quote;
use std::borrow::Cow;
use syn;
use synstructure;
pub fn derive(input: syn::DeriveInput) -> quote::Tokens {
let name = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let mut where_clause = where_clause.clone();
for param in &input.generics.ty_params {
where_clause.predicates.push(where_predicate(syn::Ty::Path(None, param.ident.clone().into())))
}
let variants = variants(&input);
let mut match_body = quote!();
match_body.append_all(variants.iter().map(|variant| {
let name = match input.body {
syn::Body::Struct(_) => Cow::Borrowed(&input.ident),
syn::Body::Enum(_) => {
Cow::Owned(syn::Ident::from(format!("{}::{}", input.ident, variant.ident)))
},
};
let (this_pattern, this_info) = synstructure::match_pattern(
&name,
&variant.data,
&synstructure::BindOpts::with_prefix(
synstructure::BindStyle::Ref,
"this".to_owned(),
),
);
let (other_pattern, other_info) = synstructure::match_pattern(
&name,
&variant.data,
&synstructure::BindOpts::with_prefix(
synstructure::BindStyle::Ref,
"other".to_owned(),
),
);
let sum = if this_info.is_empty() {
quote! { ::values::distance::SquaredDistance::Value(0.) }
} else {
let mut sum = quote!();
sum.append_separated(this_info.iter().zip(&other_info).map(|(this, other)| {
where_clause.predicates.push(where_predicate(this.field.ty.clone()));
quote! {
::values::distance::ComputeSquaredDistance::compute_squared_distance(#this, #other)?
}
}), "+");
sum
};
quote! {
(&#this_pattern, &#other_pattern) => {
Ok(#sum)
}
}
}));
if variants.len() > 1 {
match_body = quote! { #match_body, _ => Err(()), };
}
quote! {
impl #impl_generics ::values::distance::ComputeSquaredDistance for #name #ty_generics #where_clause {
#[allow(unused_variables, unused_imports)]
#[inline]
fn compute_squared_distance(
&self,
other: &Self,
) -> Result<::values::distance::SquaredDistance, ()> {
match (self, other) {
#match_body
}
}
}
}
}
fn variants(input: &syn::DeriveInput) -> Cow<[syn::Variant]> {
match input.body {
syn::Body::Enum(ref variants) => (&**variants).into(),
syn::Body::Struct(ref data) => {
vec![syn::Variant {
ident: input.ident.clone(),
attrs: input.attrs.clone(),
data: data.clone(),
discriminant: None,
}].into()
},
}
}
fn where_predicate(ty: syn::Ty) -> syn::WherePredicate {
syn::WherePredicate::BoundPredicate(
syn::WhereBoundPredicate {
bound_lifetimes: vec![],
bounded_ty: ty,
bounds: vec![syn::TyParamBound::Trait(
syn::PolyTraitRef {
bound_lifetimes: vec![],
trait_ref: syn::Path {
global: true,
segments: vec![
"values".into(),
"distance".into(),
"ComputeSquaredDistance".into(),
],
},
},
syn::TraitBoundModifier::None,
)],
},
)
}

View file

@ -9,11 +9,18 @@ extern crate synstructure;
use proc_macro::TokenStream;
mod compute_squared_distance;
mod has_viewport_percentage;
mod to_animated_value;
mod to_computed_value;
mod to_css;
#[proc_macro_derive(ComputeSquaredDistance)]
pub fn derive_compute_squared_distance(stream: TokenStream) -> TokenStream {
let input = syn::parse_derive_input(&stream.to_string()).unwrap();
compute_squared_distance::derive(input).to_string().parse().unwrap()
}
#[proc_macro_derive(HasViewportPercentage)]
pub fn derive_has_viewport_percentage(stream: TokenStream) -> TokenStream {
let input = syn::parse_derive_input(&stream.to_string()).unwrap();