]> git.lizzy.rs Git - rust.git/blobdiff - src/libcore/num/mod.rs
Auto merge of #58302 - SimonSapin:tryfrom, r=alexcrichton
[rust.git] / src / libcore / num / mod.rs
index ffd9e4a5cab058727333f6f99c37e8787d00cf0e..3ceba83afeef83b309ac04a83ff5ed3d7b49ba88 100644 (file)
@@ -1215,7 +1215,7 @@ pub fn wrapping_rem_euclid(self, rhs: Self) -> Self {
 ```"),
             #[stable(feature = "num_wrapping", since = "1.2.0")]
             #[inline]
-            pub fn wrapping_neg(self) -> Self {
+            pub const fn wrapping_neg(self) -> Self {
                 self.overflowing_neg().0
             }
         }
@@ -1569,12 +1569,8 @@ pub fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
 ```"),
             #[inline]
             #[stable(feature = "wrapping", since = "1.7.0")]
-            pub fn overflowing_neg(self) -> (Self, bool) {
-                if self == Self::min_value() {
-                    (Self::min_value(), true)
-                } else {
-                    (-self, false)
-                }
+            pub const fn overflowing_neg(self) -> (Self, bool) {
+                ((!self).wrapping_add(1), self == Self::min_value())
             }
         }
 
@@ -3089,7 +3085,7 @@ pub fn wrapping_rem_euclid(self, rhs: Self) -> Self {
         /// ```
         #[stable(feature = "num_wrapping", since = "1.2.0")]
         #[inline]
-        pub fn wrapping_neg(self) -> Self {
+        pub const fn wrapping_neg(self) -> Self {
             self.overflowing_neg().0
         }
 
@@ -3394,7 +3390,7 @@ pub fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
 ```"),
             #[inline]
             #[stable(feature = "wrapping", since = "1.7.0")]
-            pub fn overflowing_neg(self) -> (Self, bool) {
+            pub const fn overflowing_neg(self) -> (Self, bool) {
                 ((!self).wrapping_add(1), self != 0)
             }
         }