Bug 1354053 - Introduce a macro to define Interpolate trait for tuple struct which has Option<T>. r?emilio draft
authorHiroyuki Ikezoe <hikezoe@mozilla.com>
Thu, 06 Apr 2017 19:39:58 +0900
changeset 556956 e9fd927688fcbb9f474c4548bb22c2d3d013c852
parent 556955 691bb079cb81855c1e1acbee092f072ce5b99bcd
child 556957 657dff2f9b791c9a85ee5ed3db05fae66cb291a0
push id52636
push userhikezoe@mozilla.com
push dateThu, 06 Apr 2017 10:40:44 +0000
reviewersemilio
bugs1354053
milestone55.0a1
Bug 1354053 - Introduce a macro to define Interpolate trait for tuple struct which has Option<T>. r?emilio The computed value of word-spacing is T(pub Option<LengthOrPercentage>), the computed value of letter-spacing is similar to this, T(pub Option<Au>). It would be nice to have re-usable macro for these kind of struct. MozReview-Commit-ID: 6bYl6anO3yp
servo/components/style/properties/helpers.mako.rs
servo/components/style/properties/longhand/inherited_text.mako.rs
--- a/servo/components/style/properties/helpers.mako.rs
+++ b/servo/components/style/properties/helpers.mako.rs
@@ -731,8 +731,31 @@
 <%def name="to_nscsspropertyid(ident)">
     <%
         if ident == "float":
             ident = "float_"
         return "nsCSSPropertyID::eCSSProperty_%s" % ident
     %>
 </%def>
 
+/// Macro for defining Interpolate trait for tuple struct which has Option<T>,
+/// e.g. struct T(pub Option<Au>).
+<%def name="impl_interpolate_for_option_tuple(value_for_none)">
+    impl Interpolate for T {
+        #[inline]
+        fn interpolate(&self, other: &Self, progress: f64) -> Result<Self, ()> {
+            match (self, other) {
+                (&T(Some(ref this)), &T(Some(ref other))) => {
+                    Ok(T(this.interpolate(other, progress).ok()))
+                },
+                (&T(Some(ref this)), &T(None)) => {
+                    Ok(T(this.interpolate(&${value_for_none}, progress).ok()))
+                },
+                (&T(None), &T(Some(ref other))) => {
+                    Ok(T(${value_for_none}.interpolate(other, progress).ok()))
+                },
+                (&T(None), &T(None)) => {
+                    Ok(T(None))
+                },
+            }
+        }
+    }
+</%def>
--- a/servo/components/style/properties/longhand/inherited_text.mako.rs
+++ b/servo/components/style/properties/longhand/inherited_text.mako.rs
@@ -438,36 +438,17 @@
     pub mod computed_value {
         use app_units::Au;
         use properties::animated_properties::Interpolate;
 
         #[derive(Debug, Clone, PartialEq)]
         #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
         pub struct T(pub Option<Au>);
 
-        impl Interpolate for T {
-            #[inline]
-            fn interpolate(&self, other: &Self, progress: f64) -> Result<Self, ()> {
-                match (self, other) {
-                    (&T(Some(ref this)), &T(Some(ref other))) => {
-                        Ok(T(this.interpolate(other, progress).ok()))
-                    },
-                    (&T(Some(ref this)), &T(None)) => {
-                        Ok(T(this.interpolate(&Au(0), progress).ok()))
-                    },
-                    (&T(None), &T(Some(ref other))) => {
-                        Ok(T(Au(0).interpolate(other, progress).ok()))
-                    },
-                    (&T(None), &T(None)) => {
-                        Ok(T(None))
-                    },
-                }
-            }
-        }
-
+        ${helpers.impl_interpolate_for_option_tuple('Au(0)')}
     }
 
     impl ToCss for computed_value::T {
         fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
             match self.0 {
                 None => dest.write_str("normal"),
                 Some(l) => l.to_css(dest),
             }