From b9505ae72b7a4c1c758e0c367dcda908c15eabe2 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Sat, 10 Feb 2018 03:49:11 +0100 Subject: [PATCH] =?UTF-8?q?Merge=20similar=20arms=20in=20AnimationValue::c?= =?UTF-8?q?ompute=5Fsquared=5Fdistance=20=F0=9F=90=89=F0=9F=90=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This uses the same trick as in PropertyDeclaration::eq. --- .../helpers/animated_properties.mako.rs | 64 ++++++++++--------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/components/style/properties/helpers/animated_properties.mako.rs b/components/style/properties/helpers/animated_properties.mako.rs index 3e765a997d4c..ba0eb950005f 100644 --- a/components/style/properties/helpers/animated_properties.mako.rs +++ b/components/style/properties/helpers/animated_properties.mako.rs @@ -564,16 +564,16 @@ fn animate_discrete(this: &T, other: &T, procedure: Procedure) -> Resu } } +#[repr(C)] +struct AnimationValueVariantRepr { + tag: u16, + value: T +} + impl Animate for AnimationValue { fn animate(&self, other: &Self, procedure: Procedure) -> Result { - use self::AnimationValue::*; - Ok(unsafe { - #[repr(C)] - struct AnimationValueVariantRepr { - tag: u16, - value: T - } + use self::AnimationValue::*; let this_tag = *(self as *const _ as *const u16); let other_tag = *(other as *const _ as *const u16); @@ -612,31 +612,35 @@ impl Animate for AnimationValue { } } +<% + nondiscrete = [] + for prop in animated: + if prop.animation_value_type != "discrete": + nondiscrete.append(prop) +%> + impl ComputeSquaredDistance for AnimationValue { fn compute_squared_distance(&self, other: &Self) -> Result { - match *self { - % for i, prop in enumerate([p for p in data.longhands if p.animatable and p.animation_value_type == "discrete"]): - % if i > 0: - | - % endif - AnimationValue::${prop.camel_case}(..) - % endfor - => return Err(()), - _ => (), - } - match (self, other) { - % for prop in data.longhands: - % if prop.animatable: - % if prop.animation_value_type != "discrete": - (&AnimationValue::${prop.camel_case}(ref this), &AnimationValue::${prop.camel_case}(ref other)) => { - this.compute_squared_distance(other) - }, - % endif - % endif - % endfor - _ => { - panic!("computed values should be of the same property"); - }, + unsafe { + use self::AnimationValue::*; + + let this_tag = *(self as *const _ as *const u16); + let other_tag = *(other as *const _ as *const u16); + if this_tag != other_tag { + panic!("Unexpected AnimationValue::compute_squared_distance call"); + } + + match *self { + % for ty, props in groupby(nondiscrete, key=lambda x: x.animated_type()): + ${" |\n".join("{}(ref this)".format(prop.camel_case) for prop in props)} => { + let other_repr = + &*(other as *const _ as *const AnimationValueVariantRepr<${ty}>); + + this.compute_squared_distance(&other_repr.value) + } + % endfor + _ => Err(()), + } } } }