]> git.lizzy.rs Git - rust.git/commitdiff
rewrite checked_{div,rem} to no contain any reference to panics
authorJorge Aparicio <japaricious@gmail.com>
Fri, 7 Oct 2016 20:12:21 +0000 (15:12 -0500)
committerJorge Aparicio <japaricious@gmail.com>
Fri, 7 Oct 2016 20:14:28 +0000 (15:14 -0500)
even without optimizations

src/libcore/num/mod.rs

index 9a403891ebf82b0e5feb6163ed71534c830b3173..516d6f7c4a0bd956d978b8090e9fc2e9bd78f844 100644 (file)
@@ -516,11 +516,10 @@ pub fn checked_mul(self, other: Self) -> Option<Self> {
         #[stable(feature = "rust1", since = "1.0.0")]
         #[inline]
         pub fn checked_div(self, other: Self) -> Option<Self> {
-            if other == 0 {
+            if other == 0 || (self == Self::min_value() && other == -1) {
                 None
             } else {
-                let (a, b) = self.overflowing_div(other);
-                if b {None} else {Some(a)}
+                Some(unsafe { intrinsics::unchecked_div(self, other) })
             }
         }
 
@@ -541,11 +540,10 @@ pub fn checked_div(self, other: Self) -> Option<Self> {
         #[stable(feature = "wrapping", since = "1.7.0")]
         #[inline]
         pub fn checked_rem(self, other: Self) -> Option<Self> {
-            if other == 0 {
+            if other == 0 || (self == Self::min_value() && other == -1) {
                 None
             } else {
-                let (a, b) = self.overflowing_rem(other);
-                if b {None} else {Some(a)}
+                Some(unsafe { intrinsics::unchecked_rem(self, other) })
             }
         }
 
@@ -1688,7 +1686,7 @@ pub fn checked_mul(self, other: Self) -> Option<Self> {
         pub fn checked_div(self, other: Self) -> Option<Self> {
             match other {
                 0 => None,
-                other => Some(self / other),
+                other => Some(unsafe { intrinsics::unchecked_div(self, other) }),
             }
         }
 
@@ -1709,7 +1707,7 @@ pub fn checked_rem(self, other: Self) -> Option<Self> {
             if other == 0 {
                 None
             } else {
-                Some(self % other)
+                Some(unsafe { intrinsics::unchecked_rem(self, other) })
             }
         }