Rollup merge of #74759 - carbotaniuman:uabs, r=shepmaster

add `unsigned_abs` to signed integers

Mentioned on rust-lang/rfcs#2914

This PR simply adds an `unsigned_abs` to signed integers function which returns the correct absolute value as a unsigned integer.
This commit is contained in:
Yuki Okushi 2020-08-04 09:26:58 +09:00 committed by GitHub
commit cc0ac7eece
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 0 deletions

View File

@ -1601,6 +1601,29 @@ $EndFeature, "
}
}
doc_comment! {
concat!("Computes the absolute value of `self` without any wrapping
or panicking.
# Examples
Basic usage:
```
", $Feature, "#![feature(unsigned_abs)]
assert_eq!(100", stringify!($SelfT), ".unsigned_abs(), 100", stringify!($UnsignedT), ");
assert_eq!((-100", stringify!($SelfT), ").unsigned_abs(), 100", stringify!($UnsignedT), ");
assert_eq!((-128i8).unsigned_abs(), 128u8);",
$EndFeature, "
```"),
#[unstable(feature = "unsigned_abs", issue = "74913")]
#[inline]
pub const fn unsigned_abs(self) -> $UnsignedT {
self.wrapping_abs() as $UnsignedT
}
}
doc_comment! {
concat!("Wrapping (modular) exponentiation. Computes `self.pow(exp)`,
wrapping around at the boundary of the type.