mirror of
https://github.com/servo/servo.git
synced 2025-08-05 13:40:08 +01:00
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:
parent
3e14422788
commit
1a22fa7b1d
1 changed files with 65 additions and 19 deletions
|
@ -98,12 +98,22 @@ impl FontRelativeLength {
|
||||||
|
|
||||||
fn try_sum(&self, other: &Self) -> Result<Self, ()> {
|
fn try_sum(&self, other: &Self) -> Result<Self, ()> {
|
||||||
use self::FontRelativeLength::*;
|
use self::FontRelativeLength::*;
|
||||||
|
|
||||||
|
if std::mem::discriminant(self) != std::mem::discriminant(other) {
|
||||||
|
return Err(());
|
||||||
|
}
|
||||||
|
|
||||||
Ok(match (self, other) {
|
Ok(match (self, other) {
|
||||||
(&Em(one), &Em(other)) => Em(one + other),
|
(&Em(one), &Em(other)) => Em(one + other),
|
||||||
(&Ex(one), &Ex(other)) => Ex(one + other),
|
(&Ex(one), &Ex(other)) => Ex(one + other),
|
||||||
(&Ch(one), &Ch(other)) => Ch(one + other),
|
(&Ch(one), &Ch(other)) => Ch(one + other),
|
||||||
(&Rem(one), &Rem(other)) => Rem(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, ()> {
|
fn try_sum(&self, other: &Self) -> Result<Self, ()> {
|
||||||
use self::ViewportPercentageLength::*;
|
use self::ViewportPercentageLength::*;
|
||||||
|
|
||||||
|
if std::mem::discriminant(self) != std::mem::discriminant(other) {
|
||||||
|
return Err(());
|
||||||
|
}
|
||||||
|
|
||||||
Ok(match (self, other) {
|
Ok(match (self, other) {
|
||||||
(&Vw(one), &Vw(other)) => Vw(one + other),
|
(&Vw(one), &Vw(other)) => Vw(one + other),
|
||||||
(&Vh(one), &Vh(other)) => Vh(one + other),
|
(&Vh(one), &Vh(other)) => Vh(one + other),
|
||||||
(&Vmin(one), &Vmin(other)) => Vmin(one + other),
|
(&Vmin(one), &Vmin(other)) => Vmin(one + other),
|
||||||
(&Vmax(one), &Vmax(other)) => Vmax(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, ()> {
|
pub(crate) fn try_sum(&self, other: &Self) -> Result<Self, ()> {
|
||||||
use self::NoCalcLength::*;
|
use self::NoCalcLength::*;
|
||||||
|
|
||||||
|
if std::mem::discriminant(self) != std::mem::discriminant(other) {
|
||||||
|
return Err(());
|
||||||
|
}
|
||||||
|
|
||||||
Ok(match (self, other) {
|
Ok(match (self, other) {
|
||||||
(&Absolute(ref one), &Absolute(ref other)) => Absolute(*one + *other),
|
(&Absolute(ref one), &Absolute(ref other)) => Absolute(*one + *other),
|
||||||
(&FontRelative(ref one), &FontRelative(ref other)) => FontRelative(one.try_sum(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(ref one), &ServoCharacterWidth(ref other)) => {
|
||||||
ServoCharacterWidth(CharacterWidth(one.0 + other.0))
|
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 {
|
impl PartialOrd for NoCalcLength {
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
|
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
|
||||||
use self::NoCalcLength::*;
|
use self::NoCalcLength::*;
|
||||||
|
|
||||||
|
if std::mem::discriminant(self) != std::mem::discriminant(other) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(&Absolute(ref one), &Absolute(ref other)) => one.to_px().partial_cmp(&other.to_px()),
|
(&Absolute(ref one), &Absolute(ref other)) => one.to_px().partial_cmp(&other.to_px()),
|
||||||
(&FontRelative(ref one), &FontRelative(ref other)) => one.partial_cmp(other),
|
(&FontRelative(ref one), &FontRelative(ref other)) => one.partial_cmp(other),
|
||||||
(&ViewportPercentage(ref one), &ViewportPercentage(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),
|
(&ServoCharacterWidth(ref one), &ServoCharacterWidth(ref other)) => one.0.partial_cmp(&other.0),
|
||||||
_ => {
|
// See https://github.com/rust-lang/rust/issues/68867. rustc isn't
|
||||||
// This will at least catch issues some of the time... Maybe
|
// able to figure it own on its own so we help.
|
||||||
// could be worth a derive thing?
|
_ => unsafe {
|
||||||
debug_assert_ne!(self, other, "Forgot a match arm?");
|
match *self { Absolute(..) | FontRelative(..) | ViewportPercentage(..) | ServoCharacterWidth(..) => {} }
|
||||||
None
|
debug_unreachable!("Forgot an arm in partial_cmp?")
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -598,16 +632,22 @@ impl Mul<CSSFloat> for Length {
|
||||||
impl PartialOrd for FontRelativeLength {
|
impl PartialOrd for FontRelativeLength {
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
|
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
|
||||||
use self::FontRelativeLength::*;
|
use self::FontRelativeLength::*;
|
||||||
|
|
||||||
|
if std::mem::discriminant(self) != std::mem::discriminant(other) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(&Em(ref one), &Em(ref other)) => one.partial_cmp(other),
|
(&Em(ref one), &Em(ref other)) => one.partial_cmp(other),
|
||||||
(&Ex(ref one), &Ex(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),
|
(&Ch(ref one), &Ch(ref other)) => one.partial_cmp(other),
|
||||||
(&Rem(ref one), &Rem(ref other)) => one.partial_cmp(other),
|
(&Rem(ref one), &Rem(ref other)) => one.partial_cmp(other),
|
||||||
_ => {
|
// See https://github.com/rust-lang/rust/issues/68867. rustc isn't
|
||||||
// This will at least catch issues some of the time.
|
// able to figure it own on its own so we help.
|
||||||
debug_assert_ne!(self, other, "Forgot a match arm?");
|
_ => unsafe {
|
||||||
None
|
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 {
|
impl PartialOrd for ViewportPercentageLength {
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
|
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
|
||||||
use self::ViewportPercentageLength::*;
|
use self::ViewportPercentageLength::*;
|
||||||
|
|
||||||
|
if std::mem::discriminant(self) != std::mem::discriminant(other) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(&Vw(ref one), &Vw(ref other)) => one.partial_cmp(other),
|
(&Vw(ref one), &Vw(ref other)) => one.partial_cmp(other),
|
||||||
(&Vh(ref one), &Vh(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),
|
(&Vmin(ref one), &Vmin(ref other)) => one.partial_cmp(other),
|
||||||
(&Vmax(ref one), &Vmax(ref other)) => one.partial_cmp(other),
|
(&Vmax(ref one), &Vmax(ref other)) => one.partial_cmp(other),
|
||||||
_ => {
|
// See https://github.com/rust-lang/rust/issues/68867. rustc isn't
|
||||||
// This will at least catch issues some of the time.
|
// able to figure it own on its own so we help.
|
||||||
debug_assert_ne!(self, other, "Forgot a match arm?");
|
_ => unsafe {
|
||||||
None
|
match *self { Vw(..) | Vh(..) | Vmin(..) | Vmax(..) => {} }
|
||||||
}
|
debug_unreachable!("Forgot an arm in partial_cmp?")
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue