Skip to content

Commit c66ae29

Browse files
author
Jorge Aparicio
committed
rewrite checked_{div,rem} to no contain any reference to panics
even without optimizations
1 parent ca76c7e commit c66ae29

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/libcore/num/mod.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -516,11 +516,10 @@ macro_rules! int_impl {
516516
#[stable(feature = "rust1", since = "1.0.0")]
517517
#[inline]
518518
pub fn checked_div(self, other: Self) -> Option<Self> {
519-
if other == 0 {
519+
if other == 0 || (self == Self::min_value() && other == -1) {
520520
None
521521
} else {
522-
let (a, b) = self.overflowing_div(other);
523-
if b {None} else {Some(a)}
522+
Some(unsafe { intrinsics::unchecked_div(self, other) })
524523
}
525524
}
526525

@@ -541,11 +540,10 @@ macro_rules! int_impl {
541540
#[stable(feature = "wrapping", since = "1.7.0")]
542541
#[inline]
543542
pub fn checked_rem(self, other: Self) -> Option<Self> {
544-
if other == 0 {
543+
if other == 0 || (self == Self::min_value() && other == -1) {
545544
None
546545
} else {
547-
let (a, b) = self.overflowing_rem(other);
548-
if b {None} else {Some(a)}
546+
Some(unsafe { intrinsics::unchecked_rem(self, other) })
549547
}
550548
}
551549

@@ -1688,7 +1686,7 @@ macro_rules! uint_impl {
16881686
pub fn checked_div(self, other: Self) -> Option<Self> {
16891687
match other {
16901688
0 => None,
1691-
other => Some(self / other),
1689+
other => Some(unsafe { intrinsics::unchecked_div(self, other) }),
16921690
}
16931691
}
16941692

@@ -1709,7 +1707,7 @@ macro_rules! uint_impl {
17091707
if other == 0 {
17101708
None
17111709
} else {
1712-
Some(self % other)
1710+
Some(unsafe { intrinsics::unchecked_rem(self, other) })
17131711
}
17141712
}
17151713

0 commit comments

Comments
 (0)