diff options
| author | Patrick McCarter <p.mccarter@gmail.com> | 2019-02-05 15:36:31 -0500 |
|---|---|---|
| committer | Patrick McCarter <p.mccarter@gmail.com> | 2019-02-05 15:36:31 -0500 |
| commit | 9204497c2999ce4a3df9802d48cb990be7ee1164 (patch) | |
| tree | 8874b6c26e45da907d65bcf79b4e14457d1db1d5 /src/libcore/num | |
| parent | 3e58dabc16b1678da8702c71f70c72644764226c (diff) | |
| download | rust-9204497c2999ce4a3df9802d48cb990be7ee1164.tar.gz rust-9204497c2999ce4a3df9802d48cb990be7ee1164.zip | |
Allow const assignment for int saturating_add() calls for #58030
Diffstat (limited to 'src/libcore/num')
| -rw-r--r-- | src/libcore/num/mod.rs | 57 |
1 files changed, 47 insertions, 10 deletions
diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index f80f8392827..55de04db028 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -882,17 +882,37 @@ $EndFeature, " ```"), #[stable(feature = "rust1", since = "1.0.0")] #[inline] + #[cfg(stage0)] pub fn saturating_add(self, rhs: Self) -> Self { - #[cfg(stage0)] match self.checked_add(rhs) { Some(x) => x, None if rhs >= 0 => Self::max_value(), None => Self::min_value(), } - #[cfg(not(stage0))] - { - intrinsics::saturating_add(self, rhs) - } + } + + } + + doc_comment! { + concat!("Saturating integer addition. Computes `self + rhs`, saturating at the numeric +bounds instead of overflowing. + +# Examples + +Basic usage: + +``` +", $Feature, "assert_eq!(100", stringify!($SelfT), ".saturating_add(1), 101); +assert_eq!(", stringify!($SelfT), "::max_value().saturating_add(100), ", stringify!($SelfT), +"::max_value());", +$EndFeature, " +```"), + + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + #[cfg(not(stage0))] + pub const fn saturating_add(self, rhs: Self) -> Self { + intrinsics::saturating_add(self, rhs) } } @@ -2753,16 +2773,33 @@ assert_eq!(200u8.saturating_add(127), 255);", $EndFeature, " ```"), #[stable(feature = "rust1", since = "1.0.0")] #[inline] + #[cfg(stage0)] pub fn saturating_add(self, rhs: Self) -> Self { - #[cfg(stage0)] match self.checked_add(rhs) { Some(x) => x, None => Self::max_value(), } - #[cfg(not(stage0))] - { - intrinsics::saturating_add(self, rhs) - } + } + } + + doc_comment! { + concat!("Saturating integer addition. Computes `self + rhs`, saturating at +the numeric bounds instead of overflowing. + +# Examples + +Basic usage: + +``` +", $Feature, "assert_eq!(100", stringify!($SelfT), ".saturating_add(1), 101); +assert_eq!(200u8.saturating_add(127), 255);", $EndFeature, " +```"), + + #[stable(feature = "rust1", since = "1.0.0")] + #[inline] + #[cfg(not(stage0))] + pub const fn saturating_add(self, rhs: Self) -> Self { + intrinsics::saturating_add(self, rhs) } } |
