From 1a22fa7b1dd4be36a3c09186e243653783609bc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Mon, 10 Feb 2020 14:09:38 +0000 Subject: [PATCH] style: Make rust generate better code for some of the functions introduced here. See https://github.com/rust-lang/rust/issues/68867. Differential Revision: https://phabricator.services.mozilla.com/D61760 --- components/style/values/specified/length.rs | 84 ++++++++++++++++----- 1 file changed, 65 insertions(+), 19 deletions(-) diff --git a/components/style/values/specified/length.rs b/components/style/values/specified/length.rs index 3580d1105c6..3b971b422c2 100644 --- a/components/style/values/specified/length.rs +++ b/components/style/values/specified/length.rs @@ -98,12 +98,22 @@ impl FontRelativeLength { fn try_sum(&self, other: &Self) -> Result { use self::FontRelativeLength::*; + + if std::mem::discriminant(self) != std::mem::discriminant(other) { + return Err(()); + } + Ok(match (self, other) { (&Em(one), &Em(other)) => Em(one + other), (&Ex(one), &Ex(other)) => Ex(one + other), (&Ch(one), &Ch(other)) => Ch(one + other), (&Rem(one), &Rem(other)) => Rem(one + other), - _ => return Err(()), + // See https://github.com/rust-lang/rust/issues/68867. rustc isn't + // able to figure it own on its own so we help. + _ => unsafe { + match *self { Em(..) | Ex(..) | Ch(..) | Rem(..) => {} } + debug_unreachable!("Forgot to handle unit in try_sum()") + }, }) } @@ -260,12 +270,22 @@ impl ViewportPercentageLength { fn try_sum(&self, other: &Self) -> Result { use self::ViewportPercentageLength::*; + + if std::mem::discriminant(self) != std::mem::discriminant(other) { + return Err(()); + } + Ok(match (self, other) { (&Vw(one), &Vw(other)) => Vw(one + other), (&Vh(one), &Vh(other)) => Vh(one + other), (&Vmin(one), &Vmin(other)) => Vmin(one + other), (&Vmax(one), &Vmax(other)) => Vmax(one + other), - _ => return Err(()), + // See https://github.com/rust-lang/rust/issues/68867. rustc isn't + // able to figure it own on its own so we help. + _ => unsafe { + match *self { Vw(..) | Vh(..) | Vmin(..) | Vmax(..) => {} } + debug_unreachable!("Forgot to handle unit in try_sum()") + }, }) } @@ -500,6 +520,10 @@ impl NoCalcLength { pub(crate) fn try_sum(&self, other: &Self) -> Result { use self::NoCalcLength::*; + if std::mem::discriminant(self) != std::mem::discriminant(other) { + return Err(()); + } + Ok(match (self, other) { (&Absolute(ref one), &Absolute(ref other)) => Absolute(*one + *other), (&FontRelative(ref one), &FontRelative(ref other)) => FontRelative(one.try_sum(other)?), @@ -507,7 +531,12 @@ impl NoCalcLength { (&ServoCharacterWidth(ref one), &ServoCharacterWidth(ref other)) => { ServoCharacterWidth(CharacterWidth(one.0 + other.0)) }, - _ => return Err(()), + // See https://github.com/rust-lang/rust/issues/68867. rustc isn't + // able to figure it own on its own so we help. + _ => unsafe { + match *self { Absolute(..) | FontRelative(..) | ViewportPercentage(..) | ServoCharacterWidth(..) => {} } + debug_unreachable!("Forgot to handle unit in try_sum()") + }, }) } @@ -532,17 +561,22 @@ impl SpecifiedValueInfo for NoCalcLength {} impl PartialOrd for NoCalcLength { fn partial_cmp(&self, other: &Self) -> Option { use self::NoCalcLength::*; + + if std::mem::discriminant(self) != std::mem::discriminant(other) { + return None; + } + match (self, other) { (&Absolute(ref one), &Absolute(ref other)) => one.to_px().partial_cmp(&other.to_px()), (&FontRelative(ref one), &FontRelative(ref other)) => one.partial_cmp(other), (&ViewportPercentage(ref one), &ViewportPercentage(ref other)) => one.partial_cmp(other), (&ServoCharacterWidth(ref one), &ServoCharacterWidth(ref other)) => one.0.partial_cmp(&other.0), - _ => { - // This will at least catch issues some of the time... Maybe - // could be worth a derive thing? - debug_assert_ne!(self, other, "Forgot a match arm?"); - None - } + // See https://github.com/rust-lang/rust/issues/68867. rustc isn't + // able to figure it own on its own so we help. + _ => unsafe { + match *self { Absolute(..) | FontRelative(..) | ViewportPercentage(..) | ServoCharacterWidth(..) => {} } + debug_unreachable!("Forgot an arm in partial_cmp?") + }, } } } @@ -598,16 +632,22 @@ impl Mul for Length { impl PartialOrd for FontRelativeLength { fn partial_cmp(&self, other: &Self) -> Option { use self::FontRelativeLength::*; + + if std::mem::discriminant(self) != std::mem::discriminant(other) { + return None; + } + match (self, other) { (&Em(ref one), &Em(ref other)) => one.partial_cmp(other), (&Ex(ref one), &Ex(ref other)) => one.partial_cmp(other), (&Ch(ref one), &Ch(ref other)) => one.partial_cmp(other), (&Rem(ref one), &Rem(ref other)) => one.partial_cmp(other), - _ => { - // This will at least catch issues some of the time. - debug_assert_ne!(self, other, "Forgot a match arm?"); - None - } + // See https://github.com/rust-lang/rust/issues/68867. rustc isn't + // able to figure it own on its own so we help. + _ => unsafe { + match *self { Em(..) | Ex(..) | Ch(..) | Rem(..) => {} } + debug_unreachable!("Forgot an arm in partial_cmp?") + }, } } } @@ -643,16 +683,22 @@ impl Mul for ViewportPercentageLength { impl PartialOrd for ViewportPercentageLength { fn partial_cmp(&self, other: &Self) -> Option { use self::ViewportPercentageLength::*; + + if std::mem::discriminant(self) != std::mem::discriminant(other) { + return None; + } + match (self, other) { (&Vw(ref one), &Vw(ref other)) => one.partial_cmp(other), (&Vh(ref one), &Vh(ref other)) => one.partial_cmp(other), (&Vmin(ref one), &Vmin(ref other)) => one.partial_cmp(other), (&Vmax(ref one), &Vmax(ref other)) => one.partial_cmp(other), - _ => { - // This will at least catch issues some of the time. - debug_assert_ne!(self, other, "Forgot a match arm?"); - None - } + // See https://github.com/rust-lang/rust/issues/68867. rustc isn't + // able to figure it own on its own so we help. + _ => unsafe { + match *self { Vw(..) | Vh(..) | Vmin(..) | Vmax(..) => {} } + debug_unreachable!("Forgot an arm in partial_cmp?") + }, } } }