-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #58246 - pmccarter:master, r=oli-obk
Make `saturating_add` and `saturating_sub` `const` functions Fixes #58030
- Loading branch information
Showing
5 changed files
with
177 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// ignore-emscripten no i128 support | ||
#![feature(const_saturating_int_methods)] | ||
|
||
const INT_U32_NO: u32 = (42 as u32).saturating_add(2); | ||
const INT_U32: u32 = u32::max_value().saturating_add(1); | ||
const INT_U128: u128 = u128::max_value().saturating_add(1); | ||
const INT_I128: i128 = i128::max_value().saturating_add(1); | ||
const INT_I128_NEG: i128 = i128::min_value().saturating_add(-1); | ||
|
||
const INT_U32_NO_SUB: u32 = (42 as u32).saturating_sub(2); | ||
const INT_U32_SUB: u32 = (1 as u32).saturating_sub(2); | ||
const INT_I32_NO_SUB: i32 = (-42 as i32).saturating_sub(2); | ||
const INT_I32_NEG_SUB: i32 = i32::min_value().saturating_sub(1); | ||
const INT_I32_POS_SUB: i32 = i32::max_value().saturating_sub(-1); | ||
const INT_U128_SUB: u128 = (0 as u128).saturating_sub(1); | ||
const INT_I128_NEG_SUB: i128 = i128::min_value().saturating_sub(1); | ||
const INT_I128_POS_SUB: i128 = i128::max_value().saturating_sub(-1); | ||
|
||
fn main() { | ||
assert_eq!(INT_U32_NO, 44); | ||
assert_eq!(INT_U32, u32::max_value()); | ||
assert_eq!(INT_U128, u128::max_value()); | ||
assert_eq!(INT_I128, i128::max_value()); | ||
assert_eq!(INT_I128_NEG, i128::min_value()); | ||
|
||
assert_eq!(INT_U32_NO_SUB, 40); | ||
assert_eq!(INT_U32_SUB, 0); | ||
assert_eq!(INT_I32_NO_SUB, -44); | ||
assert_eq!(INT_I32_NEG_SUB, i32::min_value()); | ||
assert_eq!(INT_I32_POS_SUB, i32::max_value()); | ||
assert_eq!(INT_U128_SUB, 0); | ||
assert_eq!(INT_I128_NEG_SUB, i128::min_value()); | ||
assert_eq!(INT_I128_POS_SUB, i128::max_value()); | ||
} |