Skip to content

Commit

Permalink
Auto merge of #65107 - llogiq:const-checked, r=<try>
Browse files Browse the repository at this point in the history
constify some checked arithmetics

This makes the `checked_`{`add`, `sub`, `mul`, "neg", "shl", "shr"} operations const fns. Division is a bit more involved. I guess I could solve it with some trickery, but I'm not sure if it would negatively affect performance, so I stopped there.
  • Loading branch information
bors committed Oct 4, 2019
2 parents 2e72448 + 257fb1e commit 9c45d16
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 9c45d16

Please sign in to comment.