Skip to content

Commit

Permalink
style: Make rust generate better code for some of the functions intro…
Browse files Browse the repository at this point in the history
  • Loading branch information
emilio committed Feb 12, 2020
1 parent 3e14422 commit 1a22fa7
Showing 1 changed file with 65 additions and 19 deletions.
84 changes: 65 additions & 19 deletions components/style/values/specified/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()")
},
})
}

Expand Down Expand Up @@ -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()")
},
})
}

Expand Down Expand Up @@ -500,14 +520,23 @@ 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)?),
(&ViewportPercentage(ref one), &ViewportPercentage(ref other)) => ViewportPercentage(one.try_sum(other)?),
(&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()")
},
})
}

Expand All @@ -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?")
},
}
}
}
Expand Down Expand Up @@ -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?")
},
}
}
}
Expand Down Expand Up @@ -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?")
},
}
}
}
Expand Down

0 comments on commit 1a22fa7

Please sign in to comment.