From 257fb1e982b1e4f590cdcba420530643a8502455 Mon Sep 17 00:00:00 2001 From: Andre Bogus Date: Fri, 4 Oct 2019 18:38:16 +0200 Subject: [PATCH] constify some checked arithmetic --- src/libcore/num/mod.rs | 48 +++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index ebde82de83457..b84283b8ef8eb 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -626,9 +626,9 @@ $EndFeature, " #[must_use = "this returns the result of the operation, \ without modifying the original"] #[inline] - pub fn checked_add(self, rhs: Self) -> Option { + pub const fn checked_add(self, rhs: Self) -> Option { let (a, b) = self.overflowing_add(rhs); - if b {None} else {Some(a)} + [Some(a), None][b as usize] } } @@ -650,9 +650,9 @@ $EndFeature, " #[must_use = "this returns the result of the operation, \ without modifying the original"] #[inline] - pub fn checked_sub(self, rhs: Self) -> Option { + pub const fn checked_sub(self, rhs: Self) -> Option { let (a, b) = self.overflowing_sub(rhs); - if b {None} else {Some(a)} + [Some(a), None][b as usize] } } @@ -674,9 +674,9 @@ $EndFeature, " #[must_use = "this returns the result of the operation, \ without modifying the original"] #[inline] - pub fn checked_mul(self, rhs: Self) -> Option { + pub const fn checked_mul(self, rhs: Self) -> Option { let (a, b) = self.overflowing_mul(rhs); - if b {None} else {Some(a)} + [Some(a), None][b as usize] } } @@ -808,9 +808,9 @@ $EndFeature, " ```"), #[stable(feature = "wrapping", since = "1.7.0")] #[inline] - pub fn checked_neg(self) -> Option { + pub const fn checked_neg(self) -> Option { let (a, b) = self.overflowing_neg(); - if b {None} else {Some(a)} + [Some(a), None][b as usize] } } @@ -831,9 +831,9 @@ $EndFeature, " #[must_use = "this returns the result of the operation, \ without modifying the original"] #[inline] - pub fn checked_shl(self, rhs: u32) -> Option { + pub const fn checked_shl(self, rhs: u32) -> Option { let (a, b) = self.overflowing_shl(rhs); - if b {None} else {Some(a)} + [Some(a), None][b as usize] } } @@ -854,9 +854,9 @@ $EndFeature, " #[must_use = "this returns the result of the operation, \ without modifying the original"] #[inline] - pub fn checked_shr(self, rhs: u32) -> Option { + pub const fn checked_shr(self, rhs: u32) -> Option { let (a, b) = self.overflowing_shr(rhs); - if b {None} else {Some(a)} + [Some(a), None][b as usize] } } @@ -2679,9 +2679,9 @@ assert_eq!((", stringify!($SelfT), "::max_value() - 2).checked_add(3), None);", #[must_use = "this returns the result of the operation, \ without modifying the original"] #[inline] - pub fn checked_add(self, rhs: Self) -> Option { + pub const fn checked_add(self, rhs: Self) -> Option { let (a, b) = self.overflowing_add(rhs); - if b {None} else {Some(a)} + [Some(a), None][b as usize] } } @@ -2701,9 +2701,9 @@ assert_eq!(0", stringify!($SelfT), ".checked_sub(1), None);", $EndFeature, " #[must_use = "this returns the result of the operation, \ without modifying the original"] #[inline] - pub fn checked_sub(self, rhs: Self) -> Option { + pub const fn checked_sub(self, rhs: Self) -> Option { let (a, b) = self.overflowing_sub(rhs); - if b {None} else {Some(a)} + [Some(a), None][b as usize] } } @@ -2723,9 +2723,9 @@ assert_eq!(", stringify!($SelfT), "::max_value().checked_mul(2), None);", $EndFe #[must_use = "this returns the result of the operation, \ without modifying the original"] #[inline] - pub fn checked_mul(self, rhs: Self) -> Option { + pub const fn checked_mul(self, rhs: Self) -> Option { let (a, b) = self.overflowing_mul(rhs); - if b {None} else {Some(a)} + [Some(a), None][b as usize] } } @@ -2845,9 +2845,9 @@ assert_eq!(1", stringify!($SelfT), ".checked_neg(), None);", $EndFeature, " ```"), #[stable(feature = "wrapping", since = "1.7.0")] #[inline] - pub fn checked_neg(self) -> Option { + pub const fn checked_neg(self) -> Option { let (a, b) = self.overflowing_neg(); - if b {None} else {Some(a)} + [Some(a), None][b as usize] } } @@ -2867,9 +2867,9 @@ assert_eq!(0x10", stringify!($SelfT), ".checked_shl(129), None);", $EndFeature, #[must_use = "this returns the result of the operation, \ without modifying the original"] #[inline] - pub fn checked_shl(self, rhs: u32) -> Option { + pub const fn checked_shl(self, rhs: u32) -> Option { let (a, b) = self.overflowing_shl(rhs); - if b {None} else {Some(a)} + [Some(a), None][b as usize] } } @@ -2889,9 +2889,9 @@ assert_eq!(0x10", stringify!($SelfT), ".checked_shr(129), None);", $EndFeature, #[must_use = "this returns the result of the operation, \ without modifying the original"] #[inline] - pub fn checked_shr(self, rhs: u32) -> Option { + pub const fn checked_shr(self, rhs: u32) -> Option { let (a, b) = self.overflowing_shr(rhs); - if b {None} else {Some(a)} + [Some(a), None][b as usize] } }