Skip to content

Commit

Permalink
constify some checked arithmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
llogiq committed Oct 4, 2019
1 parent 2e72448 commit 257fb1e
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
pub const fn checked_add(self, rhs: Self) -> Option<Self> {
let (a, b) = self.overflowing_add(rhs);
if b {None} else {Some(a)}
[Some(a), None][b as usize]
}
}

Expand All @@ -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<Self> {
pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
let (a, b) = self.overflowing_sub(rhs);
if b {None} else {Some(a)}
[Some(a), None][b as usize]
}
}

Expand All @@ -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<Self> {
pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
let (a, b) = self.overflowing_mul(rhs);
if b {None} else {Some(a)}
[Some(a), None][b as usize]
}
}

Expand Down Expand Up @@ -808,9 +808,9 @@ $EndFeature, "
```"),
#[stable(feature = "wrapping", since = "1.7.0")]
#[inline]
pub fn checked_neg(self) -> Option<Self> {
pub const fn checked_neg(self) -> Option<Self> {
let (a, b) = self.overflowing_neg();
if b {None} else {Some(a)}
[Some(a), None][b as usize]
}
}

Expand All @@ -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<Self> {
pub const fn checked_shl(self, rhs: u32) -> Option<Self> {
let (a, b) = self.overflowing_shl(rhs);
if b {None} else {Some(a)}
[Some(a), None][b as usize]
}
}

Expand All @@ -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<Self> {
pub const fn checked_shr(self, rhs: u32) -> Option<Self> {
let (a, b) = self.overflowing_shr(rhs);
if b {None} else {Some(a)}
[Some(a), None][b as usize]
}
}

Expand Down Expand Up @@ -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<Self> {
pub const fn checked_add(self, rhs: Self) -> Option<Self> {
let (a, b) = self.overflowing_add(rhs);
if b {None} else {Some(a)}
[Some(a), None][b as usize]
}
}

Expand All @@ -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<Self> {
pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
let (a, b) = self.overflowing_sub(rhs);
if b {None} else {Some(a)}
[Some(a), None][b as usize]
}
}

Expand All @@ -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<Self> {
pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
let (a, b) = self.overflowing_mul(rhs);
if b {None} else {Some(a)}
[Some(a), None][b as usize]
}
}

Expand Down Expand Up @@ -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<Self> {
pub const fn checked_neg(self) -> Option<Self> {
let (a, b) = self.overflowing_neg();
if b {None} else {Some(a)}
[Some(a), None][b as usize]
}
}

Expand All @@ -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<Self> {
pub const fn checked_shl(self, rhs: u32) -> Option<Self> {
let (a, b) = self.overflowing_shl(rhs);
if b {None} else {Some(a)}
[Some(a), None][b as usize]
}
}

Expand All @@ -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<Self> {
pub const fn checked_shr(self, rhs: u32) -> Option<Self> {
let (a, b) = self.overflowing_shr(rhs);
if b {None} else {Some(a)}
[Some(a), None][b as usize]
}
}

Expand Down

0 comments on commit 257fb1e

Please sign in to comment.