diff options
| author | Lokathor <zefria@gmail.com> | 2019-02-01 01:31:52 -0700 |
|---|---|---|
| committer | Lokathor <zefria@gmail.com> | 2019-02-01 01:31:52 -0700 |
| commit | 2f5d2455a43203bce98a808b8687798199f4f815 (patch) | |
| tree | 0a4f16d9540ca97683350c57b86e47b787ad1763 /src/libcore | |
| parent | 741a3d42cb9350bcf8380b78f7695cbd10b11951 (diff) | |
| download | rust-2f5d2455a43203bce98a808b8687798199f4f815.tar.gz rust-2f5d2455a43203bce98a808b8687798199f4f815.zip | |
Make overflowing and wrapping negation const
Remember that the signed and unsigned versions are slightly different here, so there's four functions made const instead of just two.
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/num/mod.rs | 14 |
1 files changed, 5 insertions, 9 deletions
diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index f80f8392827..81aec346c93 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -1175,7 +1175,7 @@ $EndFeature, " ```"), #[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 } } @@ -1529,12 +1529,8 @@ assert_eq!(", stringify!($SelfT), "::MIN.overflowing_neg(), (", stringify!($Self ```"), #[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 ^ -1).wrapping_add(1), s == $SelfT::min_value()) } } @@ -3017,7 +3013,7 @@ assert_eq!(100", stringify!($SelfT), ".wrapping_rem_euclid(10), 0); /// ``` #[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 } @@ -3322,7 +3318,7 @@ assert_eq!(2", stringify!($SelfT), ".overflowing_neg(), (-2i32 as ", stringify!( ```"), #[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) } } |
