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
This commit is contained in:
Emilio Cobos Álvarez 2020-02-10 14:09:38 +00:00
parent 3e14422788
commit 1a22fa7b1d
No known key found for this signature in database
GPG key ID: E1152D0994E4BF8A

View file

@ -98,12 +98,22 @@ impl FontRelativeLength {
fn try_sum(&self, other: &Self) -> Result<Self, ()> {
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<Self, ()> {
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<Self, ()> {
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<cmp::Ordering> {
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<CSSFloat> for Length {
impl PartialOrd for FontRelativeLength {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
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<CSSFloat> for ViewportPercentageLength {
impl PartialOrd for ViewportPercentageLength {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
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?")
},
}
}
}