Skip to content

Commit 91bfe74

Browse files
committed
Bug 1613491 - Make rust generate better code for some of the functions introduced here. r=heycam
See rust-lang/rust#68867. Differential Revision: https://phabricator.services.mozilla.com/D61760 UltraBlame original commit: c083885bd068a47a2e8890ad2e8541b1a635cdf1
1 parent 1091617 commit 91bfe74

File tree

1 file changed

+65
-19
lines changed
  • servo/components/style/values/specified

1 file changed

+65
-19
lines changed

servo/components/style/values/specified/length.rs

+65-19
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,22 @@ impl FontRelativeLength {
9898

9999
fn try_sum(&self, other: &Self) -> Result<Self, ()> {
100100
use self::FontRelativeLength::*;
101+
102+
if std::mem::discriminant(self) != std::mem::discriminant(other) {
103+
return Err(());
104+
}
105+
101106
Ok(match (self, other) {
102107
(&Em(one), &Em(other)) => Em(one + other),
103108
(&Ex(one), &Ex(other)) => Ex(one + other),
104109
(&Ch(one), &Ch(other)) => Ch(one + other),
105110
(&Rem(one), &Rem(other)) => Rem(one + other),
106-
_ => return Err(()),
111+
112+
113+
_ => unsafe {
114+
match *self { Em(..) | Ex(..) | Ch(..) | Rem(..) => {} }
115+
debug_unreachable!("Forgot to handle unit in try_sum()")
116+
},
107117
})
108118
}
109119

@@ -260,12 +270,22 @@ impl ViewportPercentageLength {
260270

261271
fn try_sum(&self, other: &Self) -> Result<Self, ()> {
262272
use self::ViewportPercentageLength::*;
273+
274+
if std::mem::discriminant(self) != std::mem::discriminant(other) {
275+
return Err(());
276+
}
277+
263278
Ok(match (self, other) {
264279
(&Vw(one), &Vw(other)) => Vw(one + other),
265280
(&Vh(one), &Vh(other)) => Vh(one + other),
266281
(&Vmin(one), &Vmin(other)) => Vmin(one + other),
267282
(&Vmax(one), &Vmax(other)) => Vmax(one + other),
268-
_ => return Err(()),
283+
284+
285+
_ => unsafe {
286+
match *self { Vw(..) | Vh(..) | Vmin(..) | Vmax(..) => {} }
287+
debug_unreachable!("Forgot to handle unit in try_sum()")
288+
},
269289
})
270290
}
271291

@@ -500,14 +520,23 @@ impl NoCalcLength {
500520
pub(crate) fn try_sum(&self, other: &Self) -> Result<Self, ()> {
501521
use self::NoCalcLength::*;
502522

523+
if std::mem::discriminant(self) != std::mem::discriminant(other) {
524+
return Err(());
525+
}
526+
503527
Ok(match (self, other) {
504528
(&Absolute(ref one), &Absolute(ref other)) => Absolute(*one + *other),
505529
(&FontRelative(ref one), &FontRelative(ref other)) => FontRelative(one.try_sum(other)?),
506530
(&ViewportPercentage(ref one), &ViewportPercentage(ref other)) => ViewportPercentage(one.try_sum(other)?),
507531
(&ServoCharacterWidth(ref one), &ServoCharacterWidth(ref other)) => {
508532
ServoCharacterWidth(CharacterWidth(one.0 + other.0))
509533
},
510-
_ => return Err(()),
534+
535+
536+
_ => unsafe {
537+
match *self { Absolute(..) | FontRelative(..) | ViewportPercentage(..) | ServoCharacterWidth(..) => {} }
538+
debug_unreachable!("Forgot to handle unit in try_sum()")
539+
},
511540
})
512541
}
513542

@@ -532,17 +561,22 @@ impl SpecifiedValueInfo for NoCalcLength {}
532561
impl PartialOrd for NoCalcLength {
533562
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
534563
use self::NoCalcLength::*;
564+
565+
if std::mem::discriminant(self) != std::mem::discriminant(other) {
566+
return None;
567+
}
568+
535569
match (self, other) {
536570
(&Absolute(ref one), &Absolute(ref other)) => one.to_px().partial_cmp(&other.to_px()),
537571
(&FontRelative(ref one), &FontRelative(ref other)) => one.partial_cmp(other),
538572
(&ViewportPercentage(ref one), &ViewportPercentage(ref other)) => one.partial_cmp(other),
539573
(&ServoCharacterWidth(ref one), &ServoCharacterWidth(ref other)) => one.0.partial_cmp(&other.0),
540-
_ => {
541-
542-
543-
debug_assert_ne!(self, other, "Forgot a match arm?");
544-
None
545-
}
574+
575+
576+
_ => unsafe {
577+
match *self { Absolute(..) | FontRelative(..) | ViewportPercentage(..) | ServoCharacterWidth(..) => {} }
578+
debug_unreachable!("Forgot an arm in partial_cmp?")
579+
},
546580
}
547581
}
548582
}
@@ -598,16 +632,22 @@ impl Mul<CSSFloat> for Length {
598632
impl PartialOrd for FontRelativeLength {
599633
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
600634
use self::FontRelativeLength::*;
635+
636+
if std::mem::discriminant(self) != std::mem::discriminant(other) {
637+
return None;
638+
}
639+
601640
match (self, other) {
602641
(&Em(ref one), &Em(ref other)) => one.partial_cmp(other),
603642
(&Ex(ref one), &Ex(ref other)) => one.partial_cmp(other),
604643
(&Ch(ref one), &Ch(ref other)) => one.partial_cmp(other),
605644
(&Rem(ref one), &Rem(ref other)) => one.partial_cmp(other),
606-
_ => {
607-
608-
debug_assert_ne!(self, other, "Forgot a match arm?");
609-
None
610-
}
645+
646+
647+
_ => unsafe {
648+
match *self { Em(..) | Ex(..) | Ch(..) | Rem(..) => {} }
649+
debug_unreachable!("Forgot an arm in partial_cmp?")
650+
},
611651
}
612652
}
613653
}
@@ -643,16 +683,22 @@ impl Mul<CSSFloat> for ViewportPercentageLength {
643683
impl PartialOrd for ViewportPercentageLength {
644684
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
645685
use self::ViewportPercentageLength::*;
686+
687+
if std::mem::discriminant(self) != std::mem::discriminant(other) {
688+
return None;
689+
}
690+
646691
match (self, other) {
647692
(&Vw(ref one), &Vw(ref other)) => one.partial_cmp(other),
648693
(&Vh(ref one), &Vh(ref other)) => one.partial_cmp(other),
649694
(&Vmin(ref one), &Vmin(ref other)) => one.partial_cmp(other),
650695
(&Vmax(ref one), &Vmax(ref other)) => one.partial_cmp(other),
651-
_ => {
652-
653-
debug_assert_ne!(self, other, "Forgot a match arm?");
654-
None
655-
}
696+
697+
698+
_ => unsafe {
699+
match *self { Vw(..) | Vh(..) | Vmin(..) | Vmax(..) => {} }
700+
debug_unreachable!("Forgot an arm in partial_cmp?")
701+
},
656702
}
657703
}
658704
}

0 commit comments

Comments
 (0)