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
--- 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),
}