Skip to content

Commit 2f5d245

Browse files
committed
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.
1 parent 741a3d4 commit 2f5d245

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

src/libcore/num/mod.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,7 @@ $EndFeature, "
11751175
```"),
11761176
#[stable(feature = "num_wrapping", since = "1.2.0")]
11771177
#[inline]
1178-
pub fn wrapping_neg(self) -> Self {
1178+
pub const fn wrapping_neg(self) -> Self {
11791179
self.overflowing_neg().0
11801180
}
11811181
}
@@ -1529,12 +1529,8 @@ assert_eq!(", stringify!($SelfT), "::MIN.overflowing_neg(), (", stringify!($Self
15291529
```"),
15301530
#[inline]
15311531
#[stable(feature = "wrapping", since = "1.7.0")]
1532-
pub fn overflowing_neg(self) -> (Self, bool) {
1533-
if self == Self::min_value() {
1534-
(Self::min_value(), true)
1535-
} else {
1536-
(-self, false)
1537-
}
1532+
pub const fn overflowing_neg(self) -> (Self, bool) {
1533+
((self ^ -1).wrapping_add(1), s == $SelfT::min_value())
15381534
}
15391535
}
15401536

@@ -3017,7 +3013,7 @@ assert_eq!(100", stringify!($SelfT), ".wrapping_rem_euclid(10), 0);
30173013
/// ```
30183014
#[stable(feature = "num_wrapping", since = "1.2.0")]
30193015
#[inline]
3020-
pub fn wrapping_neg(self) -> Self {
3016+
pub const fn wrapping_neg(self) -> Self {
30213017
self.overflowing_neg().0
30223018
}
30233019

@@ -3322,7 +3318,7 @@ assert_eq!(2", stringify!($SelfT), ".overflowing_neg(), (-2i32 as ", stringify!(
33223318
```"),
33233319
#[inline]
33243320
#[stable(feature = "wrapping", since = "1.7.0")]
3325-
pub fn overflowing_neg(self) -> (Self, bool) {
3321+
pub const fn overflowing_neg(self) -> (Self, bool) {
33263322
((!self).wrapping_add(1), self != 0)
33273323
}
33283324
}

0 commit comments

Comments
 (0)