Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make overflowing and wrapping negation const #58044

Merged
merged 6 commits into from
Feb 20, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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), self == Self::min_value())
Lokathor marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}
}
Expand Down