diff options
-rw-r--r-- | components/style/macros.rs | 7 | ||||
-rw-r--r-- | components/style/properties/helpers/animated_properties.mako.rs | 102 | ||||
-rw-r--r-- | components/style/values/animated/color.rs | 9 | ||||
-rw-r--r-- | components/style/values/animated/effects.rs | 12 | ||||
-rw-r--r-- | components/style/values/computed/angle.rs | 16 | ||||
-rw-r--r-- | components/style/values/computed/length.rs | 2 | ||||
-rw-r--r-- | components/style/values/computed/percentage.rs | 12 | ||||
-rw-r--r-- | components/style/values/computed/transform.rs | 13 | ||||
-rw-r--r-- | components/style/values/generics/effects.rs | 3 | ||||
-rw-r--r-- | components/style/values/generics/mod.rs | 4 | ||||
-rw-r--r-- | components/style/values/generics/position.rs | 3 | ||||
-rw-r--r-- | components/style/values/generics/svg.rs | 14 | ||||
-rw-r--r-- | components/style/values/generics/transform.rs | 3 | ||||
-rw-r--r-- | components/style_derive/lib.rs | 7 | ||||
-rw-r--r-- | components/style_derive/to_animated_zero.rs | 79 |
15 files changed, 112 insertions, 174 deletions
diff --git a/components/style/macros.rs b/components/style/macros.rs index b4a27415162..e07aa8f99ab 100644 --- a/components/style/macros.rs +++ b/components/style/macros.rs @@ -84,7 +84,7 @@ macro_rules! define_keyword_type { ($name: ident, $css: expr) => { #[allow(missing_docs)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] - #[derive(Clone, ComputeSquaredDistance, Copy, PartialEq, ToCss)] + #[derive(Clone, ComputeSquaredDistance, Copy, PartialEq, ToAnimatedZero, ToCss)] pub struct $name; impl $crate::values::animated::Animate for $name { @@ -115,10 +115,5 @@ macro_rules! define_keyword_type { impl $crate::values::computed::ComputedValueAsSpecified for $name {} impl $crate::values::animated::AnimatedValueAsComputed for $name {} no_viewport_percentage!($name); - - impl $crate::values::animated::ToAnimatedZero for $name { - #[inline] - fn to_animated_zero(&self) -> Result<Self, ()> { Ok($name) } - } }; } diff --git a/components/style/properties/helpers/animated_properties.mako.rs b/components/style/properties/helpers/animated_properties.mako.rs index b30bb25623d..ec0eed5a66d 100644 --- a/components/style/properties/helpers/animated_properties.mako.rs +++ b/components/style/properties/helpers/animated_properties.mako.rs @@ -879,23 +879,6 @@ impl Animate for LengthOrPercentage { } } -impl ToAnimatedZero for LengthOrPercentage { - #[inline] - fn to_animated_zero(&self) -> Result<Self, ()> { - match *self { - LengthOrPercentage::Length(ref length) => { - Ok(LengthOrPercentage::Length(length.to_animated_zero()?)) - }, - LengthOrPercentage::Percentage(ref percentage) => { - Ok(LengthOrPercentage::Percentage(percentage.to_animated_zero()?)) - }, - LengthOrPercentage::Calc(ref calc) => { - Ok(LengthOrPercentage::Calc(calc.to_animated_zero()?)) - }, - } - } -} - /// https://drafts.csswg.org/css-transitions/#animtype-lpcalc impl Animate for LengthOrPercentageOrAuto { #[inline] @@ -1134,20 +1117,6 @@ where } } -impl<H, V> ToAnimatedZero for generic_position::Position<H, V> -where - H: ToAnimatedZero, - V: ToAnimatedZero, -{ - #[inline] - fn to_animated_zero(&self) -> Result<Self, ()> { - Ok(generic_position::Position { - horizontal: self.horizontal.to_animated_zero()?, - vertical: self.vertical.to_animated_zero()?, - }) - } -} - impl<H, V> RepeatableListAnimatable for generic_position::Position<H, V> where H: RepeatableListAnimatable, V: RepeatableListAnimatable {} @@ -2689,28 +2658,6 @@ where } } -impl<L, N> ToAnimatedZero for SvgLengthOrPercentageOrNumber<L, N> -where - L: ToAnimatedZero, - N: ToAnimatedZero, -{ - #[inline] - fn to_animated_zero(&self) -> Result<Self, ()> { - match *self { - SvgLengthOrPercentageOrNumber::LengthOrPercentage(ref lop) => { - Ok(SvgLengthOrPercentageOrNumber::LengthOrPercentage( - lop.to_animated_zero()?, - )) - }, - SvgLengthOrPercentageOrNumber::Number(ref num) => { - Ok(SvgLengthOrPercentageOrNumber::Number( - num.to_animated_zero()?, - )) - }, - } - } -} - impl<L> Animate for SVGLength<L> where L: Animate + Clone, @@ -2732,21 +2679,6 @@ where } } -impl<L> ToAnimatedZero for SVGLength<L> -where - L: ToAnimatedZero, -{ - #[inline] - fn to_animated_zero(&self) -> Result<Self, ()> { - match *self { - SVGLength::Length(ref length) => { - Ok(SVGLength::Length(length.to_animated_zero()?)) - }, - SVGLength::ContextValue => Ok(SVGLength::ContextValue), - } - } -} - /// https://www.w3.org/TR/SVG11/painting.html#StrokeDasharrayProperty impl<L> Animate for SVGStrokeDashArray<L> where @@ -2772,7 +2704,7 @@ where impl<L> ToAnimatedZero for SVGStrokeDashArray<L> where - L: Clone + ToAnimatedZero + L: ToAnimatedZero, { #[inline] fn to_animated_zero(&self) -> Result<Self, ()> { @@ -2808,19 +2740,6 @@ where } } -impl<OpacityType> ToAnimatedZero for SVGOpacity<OpacityType> - where OpacityType: ToAnimatedZero + Clone -{ - #[inline] - fn to_animated_zero(&self) -> Result<Self, ()> { - match self { - &SVGOpacity::Opacity(ref opacity) => - opacity.to_animated_zero().map(SVGOpacity::Opacity), - other => Ok(other.clone()), - } - } -} - <% FILTER_FUNCTIONS = [ 'Blur', 'Brightness', 'Contrast', 'Grayscale', 'HueRotate', 'Invert', 'Opacity', 'Saturate', @@ -2877,7 +2796,6 @@ impl ToAnimatedZero for AnimatedFilter { } } - // FIXME(nox): This should be derived. impl ComputeSquaredDistance for AnimatedFilter { fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> { @@ -3004,15 +2922,6 @@ where } } -impl<T> ToAnimatedZero for NonNegative<T> - where T: ToAnimatedZero -{ - #[inline] - fn to_animated_zero(&self) -> Result<Self, ()> { - self.0.to_animated_zero().map(NonNegative::<T>) - } -} - impl<T> Animate for GreaterThanOrEqualToOne<T> where T: Animate, @@ -3022,12 +2931,3 @@ where Ok(GreaterThanOrEqualToOne(self.0.animate(&other.0, procedure)?)) } } - -impl<T> ToAnimatedZero for GreaterThanOrEqualToOne<T> - where T: ToAnimatedZero -{ - #[inline] - fn to_animated_zero(&self) -> Result<Self, ()> { - self.0.to_animated_zero().map(GreaterThanOrEqualToOne::<T>) - } -} diff --git a/components/style/values/animated/color.rs b/components/style/values/animated/color.rs index ad77feb10dd..5c891709374 100644 --- a/components/style/values/animated/color.rs +++ b/components/style/values/animated/color.rs @@ -12,7 +12,7 @@ use values::distance::{ComputeSquaredDistance, SquaredDistance}; /// Unlike in computed values, each component value may exceed the /// range `[0.0, 1.0]`. #[cfg_attr(feature = "servo", derive(HeapSizeOf))] -#[derive(Clone, Copy, Debug, PartialEq)] +#[derive(Clone, Copy, Debug, PartialEq, ToAnimatedZero)] pub struct RGBA { /// The red component. pub red: f32, @@ -69,13 +69,6 @@ impl ComputeSquaredDistance for RGBA { } } -impl ToAnimatedZero for RGBA { - #[inline] - fn to_animated_zero(&self) -> Result<Self, ()> { - Ok(RGBA::transparent()) - } -} - #[allow(missing_docs)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[derive(Clone, Copy, Debug, PartialEq)] diff --git a/components/style/values/animated/effects.rs b/components/style/values/animated/effects.rs index e4419ec9304..0d649138b70 100644 --- a/components/style/values/animated/effects.rs +++ b/components/style/values/animated/effects.rs @@ -223,15 +223,3 @@ impl Animate for SimpleShadow { }) } } - -impl ToAnimatedZero for SimpleShadow { - #[inline] - fn to_animated_zero(&self) -> Result<Self, ()> { - Ok(SimpleShadow { - color: self.color.to_animated_zero()?, - horizontal: self.horizontal.to_animated_zero()?, - vertical: self.vertical.to_animated_zero()?, - blur: self.blur.to_animated_zero()?, - }) - } -} diff --git a/components/style/values/computed/angle.rs b/components/style/values/computed/angle.rs index 3478553666f..d5461d1cbf9 100644 --- a/components/style/values/computed/angle.rs +++ b/components/style/values/computed/angle.rs @@ -8,12 +8,12 @@ use std::{f32, f64, fmt}; use std::f64::consts::PI; use style_traits::ToCss; use values::CSSFloat; -use values::animated::{Animate, Procedure, ToAnimatedZero}; +use values::animated::{Animate, Procedure}; use values::distance::{ComputeSquaredDistance, SquaredDistance}; /// A computed angle. #[cfg_attr(feature = "servo", derive(HeapSizeOf, Deserialize, Serialize))] -#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, PartialOrd)] +#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, PartialOrd, ToAnimatedZero)] pub enum Angle { /// An angle with degree unit. Degree(CSSFloat), @@ -85,18 +85,6 @@ impl Animate for Angle { } } -impl ToAnimatedZero for Angle { - #[inline] - fn to_animated_zero(&self) -> Result<Angle, ()> { - match *self { - Angle::Degree(ref this) => Ok(Angle::Degree(this.to_animated_zero()?)), - Angle::Gradian(ref this) => Ok(Angle::Gradian(this.to_animated_zero()?)), - Angle::Radian(ref this) => Ok(Angle::Radian(this.to_animated_zero()?)), - Angle::Turn(ref this) => Ok(Angle::Turn(this.to_animated_zero()?)), - } - } -} - impl ComputeSquaredDistance for Angle { #[inline] fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> { diff --git a/components/style/values/computed/length.rs b/components/style/values/computed/length.rs index 365e377614a..945ce8b841f 100644 --- a/components/style/values/computed/length.rs +++ b/components/style/values/computed/length.rs @@ -286,7 +286,7 @@ impl ToComputedValue for specified::CalcLengthOrPercentage { #[allow(missing_docs)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] -#[derive(Clone, Copy, PartialEq, ToCss)] +#[derive(Clone, Copy, PartialEq, ToAnimatedZero, ToCss)] pub enum LengthOrPercentage { Length(Au), Percentage(Percentage), diff --git a/components/style/values/computed/percentage.rs b/components/style/values/computed/percentage.rs index 01f8195456c..c870680e5a0 100644 --- a/components/style/values/computed/percentage.rs +++ b/components/style/values/computed/percentage.rs @@ -7,11 +7,12 @@ use std::fmt; use style_traits::ToCss; use values::{CSSFloat, serialize_percentage}; -use values::animated::{Animate, Procedure, ToAnimatedZero}; +use values::animated::{Animate, Procedure}; /// A computed percentage. -#[derive(Clone, ComputeSquaredDistance, Copy, Debug, Default, HasViewportPercentage, PartialEq, PartialOrd)] #[cfg_attr(feature = "servo", derive(Deserialize, HeapSizeOf, Serialize))] +#[derive(Clone, ComputeSquaredDistance, Copy, Debug, Default)] +#[derive(HasViewportPercentage, PartialEq, PartialOrd, ToAnimatedZero)] pub struct Percentage(pub CSSFloat); impl Percentage { @@ -42,13 +43,6 @@ impl Animate for Percentage { } } -impl ToAnimatedZero for Percentage { - #[inline] - fn to_animated_zero(&self) -> Result<Self, ()> { - Ok(Percentage(0.)) - } -} - impl ToCss for Percentage { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where diff --git a/components/style/values/computed/transform.rs b/components/style/values/computed/transform.rs index 1d4495bf94e..91ac9904296 100644 --- a/components/style/values/computed/transform.rs +++ b/components/style/values/computed/transform.rs @@ -4,7 +4,7 @@ //! Computed types for CSS values that are related to transformations. -use values::animated::{Animate, Procedure, ToAnimatedZero}; +use values::animated::{Animate, Procedure}; use values::computed::{Length, LengthOrPercentage, Number, Percentage}; use values::generics::transform::TimingFunction as GenericTimingFunction; use values::generics::transform::TransformOrigin as GenericTransformOrigin; @@ -37,14 +37,3 @@ impl Animate for TransformOrigin { )) } } - -impl ToAnimatedZero for TransformOrigin { - #[inline] - fn to_animated_zero(&self) -> Result<Self, ()> { - Ok(Self::new( - self.horizontal.to_animated_zero()?, - self.vertical.to_animated_zero()?, - self.depth.to_animated_zero()?, - )) - } -} diff --git a/components/style/values/generics/effects.rs b/components/style/values/generics/effects.rs index cff8d87c078..e79a30a097a 100644 --- a/components/style/values/generics/effects.rs +++ b/components/style/values/generics/effects.rs @@ -65,7 +65,8 @@ 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, ComputeSquaredDistance, Debug, HasViewportPercentage, PartialEq, ToAnimatedValue, ToCss)] +#[derive(Clone, ComputeSquaredDistance, Debug, HasViewportPercentage, PartialEq)] +#[derive(ToAnimatedValue, ToAnimatedZero, ToCss)] pub struct SimpleShadow<Color, SizeLength, ShapeLength> { /// Color. pub color: Color, diff --git a/components/style/values/generics/mod.rs b/components/style/values/generics/mod.rs index b097b0f85de..00bc508059f 100644 --- a/components/style/values/generics/mod.rs +++ b/components/style/values/generics/mod.rs @@ -269,11 +269,11 @@ impl ToCss for FontSettingTagFloat { /// A wrapper of Non-negative values. #[cfg_attr(feature = "servo", derive(Deserialize, HeapSizeOf, Serialize))] #[derive(Clone, ComputeSquaredDistance, Copy, Debug, HasViewportPercentage)] -#[derive(PartialEq, PartialOrd, ToComputedValue, ToCss)] +#[derive(PartialEq, PartialOrd, ToAnimatedZero, ToComputedValue, ToCss)] pub struct NonNegative<T>(pub T); /// A wrapper of greater-than-or-equal-to-one values. #[cfg_attr(feature = "servo", derive(Deserialize, HeapSizeOf, Serialize))] #[derive(Clone, ComputeSquaredDistance, Copy, Debug, HasViewportPercentage)] -#[derive(PartialEq, PartialOrd, ToComputedValue, ToCss)] +#[derive(PartialEq, PartialOrd, ToAnimatedZero, ToComputedValue, ToCss)] pub struct GreaterThanOrEqualToOne<T>(pub T); diff --git a/components/style/values/generics/position.rs b/components/style/values/generics/position.rs index 503cb9a1563..620d36a74ae 100644 --- a/components/style/values/generics/position.rs +++ b/components/style/values/generics/position.rs @@ -7,7 +7,8 @@ /// 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)] +#[derive(Clone, ComputeSquaredDistance, Copy, Debug, HasViewportPercentage)] +#[derive(PartialEq, ToAnimatedZero, ToComputedValue)] pub struct Position<H, V> { /// The horizontal component of position. pub horizontal: H, diff --git a/components/style/values/generics/svg.rs b/components/style/values/generics/svg.rs index c393f70dd70..f8362a7cb9e 100644 --- a/components/style/values/generics/svg.rs +++ b/components/style/values/generics/svg.rs @@ -102,12 +102,12 @@ impl<ColorType: Parse, UrlPaintServer: Parse> Parse for SVGPaint<ColorType, UrlP /// https://www.w3.org/TR/SVG11/painting.html#StrokeProperties #[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, ToAnimatedValue)] -#[derive(ToCss, ToComputedValue)] -pub enum SvgLengthOrPercentageOrNumber<LengthOrPercentageType, NumberType> { +#[derive(ToAnimatedZero, ToCss, ToComputedValue)] +pub enum SvgLengthOrPercentageOrNumber<LengthOrPercentage, Number> { /// <length> | <percentage> - LengthOrPercentage(LengthOrPercentageType), + LengthOrPercentage(LengthOrPercentage), /// <number> - Number(NumberType), + Number(Number), } impl<L, N> ComputeSquaredDistance for SvgLengthOrPercentageOrNumber<L, N> @@ -184,7 +184,8 @@ impl <LengthOrPercentageType: Parse, NumberType: Parse> Parse for /// An SVG length value supports `context-value` in addition to length. #[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[derive(Clone, ComputeSquaredDistance, Copy, Debug, PartialEq)] -#[derive(HasViewportPercentage, ToAnimatedValue, ToComputedValue, ToCss)] +#[derive(HasViewportPercentage, ToAnimatedValue, ToAnimatedZero)] +#[derive(ToComputedValue, ToCss)] pub enum SVGLength<LengthType> { /// `<length> | <percentage> | <number>` Length(LengthType), @@ -228,7 +229,8 @@ 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, ComputeSquaredDistance, Copy, Debug, PartialEq, HasViewportPercentage, ToComputedValue, ToCss)] +#[derive(Clone, ComputeSquaredDistance, Copy, Debug, HasViewportPercentage)] +#[derive(PartialEq, ToAnimatedZero, ToComputedValue, ToCss)] pub enum SVGOpacity<OpacityType> { /// `<opacity-value>` Opacity(OpacityType), diff --git a/components/style/values/generics/transform.rs b/components/style/values/generics/transform.rs index 32f37caec12..fd395106aa3 100644 --- a/components/style/values/generics/transform.rs +++ b/components/style/values/generics/transform.rs @@ -24,7 +24,8 @@ pub struct Matrix<T, U = T> { /// A generic transform origin. #[cfg_attr(feature = "servo", derive(HeapSizeOf))] -#[derive(Clone, ComputeSquaredDistance, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue, ToCss)] +#[derive(Clone, ComputeSquaredDistance, Copy, Debug, HasViewportPercentage)] +#[derive(PartialEq, ToAnimatedZero, ToComputedValue, ToCss)] pub struct TransformOrigin<H, V, Depth> { /// The horizontal origin. pub horizontal: H, diff --git a/components/style_derive/lib.rs b/components/style_derive/lib.rs index 13a855eb6ba..20f974a9e99 100644 --- a/components/style_derive/lib.rs +++ b/components/style_derive/lib.rs @@ -12,6 +12,7 @@ use proc_macro::TokenStream; mod compute_squared_distance; mod has_viewport_percentage; mod to_animated_value; +mod to_animated_zero; mod to_computed_value; mod to_css; @@ -33,6 +34,12 @@ pub fn derive_to_animated_value(stream: TokenStream) -> TokenStream { to_animated_value::derive(input).to_string().parse().unwrap() } +#[proc_macro_derive(ToAnimatedZero)] +pub fn derive_to_animated_zero(stream: TokenStream) -> TokenStream { + let input = syn::parse_derive_input(&stream.to_string()).unwrap(); + to_animated_zero::derive(input).to_string().parse().unwrap() +} + #[proc_macro_derive(ToComputedValue)] pub fn derive_to_computed_value(stream: TokenStream) -> TokenStream { let input = syn::parse_derive_input(&stream.to_string()).unwrap(); diff --git a/components/style_derive/to_animated_zero.rs b/components/style_derive/to_animated_zero.rs new file mode 100644 index 00000000000..f44443f4a34 --- /dev/null +++ b/components/style_derive/to_animated_zero.rs @@ -0,0 +1,79 @@ +/* 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 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 to_body = match_body(&input); + + quote! { + impl #impl_generics ::values::animated::ToAnimatedZero for #name #ty_generics #where_clause { + #[allow(unused_variables)] + #[inline] + fn to_animated_zero(&self) -> Result<Self, ()> { + match *self { + #to_body + } + } + } + } +} + +fn match_body(input: &syn::DeriveInput) -> quote::Tokens { + synstructure::each_variant(&input, &synstructure::BindStyle::Ref.into(), |fields, variant| { + let name = if let syn::Body::Enum(_) = input.body { + format!("{}::{}", input.ident, variant.ident).into() + } else { + variant.ident.clone() + }; + let (zero, computed_fields) = synstructure::match_pattern( + &name, + &variant.data, + &synstructure::BindStyle::Move.into(), + ); + let fields_pairs = fields.iter().zip(computed_fields.iter()); + let mut computations = quote!(); + computations.append_all(fields_pairs.map(|(field, computed_field)| { + quote! { + let #computed_field = ::values::animated::ToAnimatedZero::to_animated_zero(#field)?; + } + })); + Some(quote!( + #computations + Ok(#zero) + )) + }) +} + +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(), + "animated".into(), + "ToAnimatedZero".into(), + ], + }, + }, + syn::TraitBoundModifier::None, + )], + }) +} |