Skip to content

Commit 37c1418

Browse files
Make checked arithmetic besides division const
Co-Authored-By: 9999years <rbt@sent.as>
1 parent d9e3d2a commit 37c1418

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

src/libcore/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
#![feature(concat_idents)]
7373
#![feature(const_alloc_layout)]
7474
#![feature(const_if_match)]
75+
#![feature(const_int_checked)]
7576
#![feature(const_int_euclidean)]
7677
#![feature(const_panic)]
7778
#![feature(const_fn_union)]

src/libcore/num/mod.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -701,10 +701,11 @@ assert_eq!((", stringify!($SelfT), "::max_value() - 2).checked_add(3), None);",
701701
$EndFeature, "
702702
```"),
703703
#[stable(feature = "rust1", since = "1.0.0")]
704+
#[rustc_const_unstable(feature = "const_int_checked", issue = "53718")]
704705
#[must_use = "this returns the result of the operation, \
705706
without modifying the original"]
706707
#[inline]
707-
pub fn checked_add(self, rhs: Self) -> Option<Self> {
708+
pub const fn checked_add(self, rhs: Self) -> Option<Self> {
708709
let (a, b) = self.overflowing_add(rhs);
709710
if b {None} else {Some(a)}
710711
}
@@ -725,10 +726,11 @@ assert_eq!((", stringify!($SelfT), "::min_value() + 2).checked_sub(3), None);",
725726
$EndFeature, "
726727
```"),
727728
#[stable(feature = "rust1", since = "1.0.0")]
729+
#[rustc_const_unstable(feature = "const_int_checked", issue = "53718")]
728730
#[must_use = "this returns the result of the operation, \
729731
without modifying the original"]
730732
#[inline]
731-
pub fn checked_sub(self, rhs: Self) -> Option<Self> {
733+
pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
732734
let (a, b) = self.overflowing_sub(rhs);
733735
if b {None} else {Some(a)}
734736
}
@@ -749,10 +751,11 @@ assert_eq!(", stringify!($SelfT), "::max_value().checked_mul(2), None);",
749751
$EndFeature, "
750752
```"),
751753
#[stable(feature = "rust1", since = "1.0.0")]
754+
#[rustc_const_unstable(feature = "const_int_checked", issue = "53718")]
752755
#[must_use = "this returns the result of the operation, \
753756
without modifying the original"]
754757
#[inline]
755-
pub fn checked_mul(self, rhs: Self) -> Option<Self> {
758+
pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
756759
let (a, b) = self.overflowing_mul(rhs);
757760
if b {None} else {Some(a)}
758761
}
@@ -889,8 +892,9 @@ assert_eq!(", stringify!($SelfT), "::MIN.checked_neg(), None);",
889892
$EndFeature, "
890893
```"),
891894
#[stable(feature = "wrapping", since = "1.7.0")]
895+
#[rustc_const_unstable(feature = "const_int_checked", issue = "53718")]
892896
#[inline]
893-
pub fn checked_neg(self) -> Option<Self> {
897+
pub const fn checked_neg(self) -> Option<Self> {
894898
let (a, b) = self.overflowing_neg();
895899
if b {None} else {Some(a)}
896900
}
@@ -910,10 +914,11 @@ assert_eq!(0x1", stringify!($SelfT), ".checked_shl(129), None);",
910914
$EndFeature, "
911915
```"),
912916
#[stable(feature = "wrapping", since = "1.7.0")]
917+
#[rustc_const_unstable(feature = "const_int_checked", issue = "53718")]
913918
#[must_use = "this returns the result of the operation, \
914919
without modifying the original"]
915920
#[inline]
916-
pub fn checked_shl(self, rhs: u32) -> Option<Self> {
921+
pub const fn checked_shl(self, rhs: u32) -> Option<Self> {
917922
let (a, b) = self.overflowing_shl(rhs);
918923
if b {None} else {Some(a)}
919924
}
@@ -933,10 +938,11 @@ assert_eq!(0x10", stringify!($SelfT), ".checked_shr(128), None);",
933938
$EndFeature, "
934939
```"),
935940
#[stable(feature = "wrapping", since = "1.7.0")]
941+
#[rustc_const_unstable(feature = "const_int_checked", issue = "53718")]
936942
#[must_use = "this returns the result of the operation, \
937943
without modifying the original"]
938944
#[inline]
939-
pub fn checked_shr(self, rhs: u32) -> Option<Self> {
945+
pub const fn checked_shr(self, rhs: u32) -> Option<Self> {
940946
let (a, b) = self.overflowing_shr(rhs);
941947
if b {None} else {Some(a)}
942948
}
@@ -958,8 +964,9 @@ assert_eq!(", stringify!($SelfT), "::MIN.checked_abs(), None);",
958964
$EndFeature, "
959965
```"),
960966
#[stable(feature = "no_panic_abs", since = "1.13.0")]
967+
#[rustc_const_unstable(feature = "const_int_checked", issue = "53718")]
961968
#[inline]
962-
pub fn checked_abs(self) -> Option<Self> {
969+
pub const fn checked_abs(self) -> Option<Self> {
963970
if self.is_negative() {
964971
self.checked_neg()
965972
} else {
@@ -2855,10 +2862,11 @@ Basic usage:
28552862
assert_eq!((", stringify!($SelfT), "::max_value() - 2).checked_add(3), None);", $EndFeature, "
28562863
```"),
28572864
#[stable(feature = "rust1", since = "1.0.0")]
2865+
#[rustc_const_unstable(feature = "const_int_checked", issue = "53718")]
28582866
#[must_use = "this returns the result of the operation, \
28592867
without modifying the original"]
28602868
#[inline]
2861-
pub fn checked_add(self, rhs: Self) -> Option<Self> {
2869+
pub const fn checked_add(self, rhs: Self) -> Option<Self> {
28622870
let (a, b) = self.overflowing_add(rhs);
28632871
if b {None} else {Some(a)}
28642872
}
@@ -2877,10 +2885,11 @@ Basic usage:
28772885
assert_eq!(0", stringify!($SelfT), ".checked_sub(1), None);", $EndFeature, "
28782886
```"),
28792887
#[stable(feature = "rust1", since = "1.0.0")]
2888+
#[rustc_const_unstable(feature = "const_int_checked", issue = "53718")]
28802889
#[must_use = "this returns the result of the operation, \
28812890
without modifying the original"]
28822891
#[inline]
2883-
pub fn checked_sub(self, rhs: Self) -> Option<Self> {
2892+
pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
28842893
let (a, b) = self.overflowing_sub(rhs);
28852894
if b {None} else {Some(a)}
28862895
}
@@ -2899,10 +2908,11 @@ Basic usage:
28992908
assert_eq!(", stringify!($SelfT), "::max_value().checked_mul(2), None);", $EndFeature, "
29002909
```"),
29012910
#[stable(feature = "rust1", since = "1.0.0")]
2911+
#[rustc_const_unstable(feature = "const_int_checked", issue = "53718")]
29022912
#[must_use = "this returns the result of the operation, \
29032913
without modifying the original"]
29042914
#[inline]
2905-
pub fn checked_mul(self, rhs: Self) -> Option<Self> {
2915+
pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
29062916
let (a, b) = self.overflowing_mul(rhs);
29072917
if b {None} else {Some(a)}
29082918
}
@@ -3029,8 +3039,9 @@ Basic usage:
30293039
assert_eq!(1", stringify!($SelfT), ".checked_neg(), None);", $EndFeature, "
30303040
```"),
30313041
#[stable(feature = "wrapping", since = "1.7.0")]
3042+
#[rustc_const_unstable(feature = "const_int_checked", issue = "53718")]
30323043
#[inline]
3033-
pub fn checked_neg(self) -> Option<Self> {
3044+
pub const fn checked_neg(self) -> Option<Self> {
30343045
let (a, b) = self.overflowing_neg();
30353046
if b {None} else {Some(a)}
30363047
}
@@ -3049,10 +3060,11 @@ Basic usage:
30493060
assert_eq!(0x10", stringify!($SelfT), ".checked_shl(129), None);", $EndFeature, "
30503061
```"),
30513062
#[stable(feature = "wrapping", since = "1.7.0")]
3063+
#[rustc_const_unstable(feature = "const_int_checked", issue = "53718")]
30523064
#[must_use = "this returns the result of the operation, \
30533065
without modifying the original"]
30543066
#[inline]
3055-
pub fn checked_shl(self, rhs: u32) -> Option<Self> {
3067+
pub const fn checked_shl(self, rhs: u32) -> Option<Self> {
30563068
let (a, b) = self.overflowing_shl(rhs);
30573069
if b {None} else {Some(a)}
30583070
}
@@ -3071,10 +3083,11 @@ Basic usage:
30713083
assert_eq!(0x10", stringify!($SelfT), ".checked_shr(129), None);", $EndFeature, "
30723084
```"),
30733085
#[stable(feature = "wrapping", since = "1.7.0")]
3086+
#[rustc_const_unstable(feature = "const_int_checked", issue = "53718")]
30743087
#[must_use = "this returns the result of the operation, \
30753088
without modifying the original"]
30763089
#[inline]
3077-
pub fn checked_shr(self, rhs: u32) -> Option<Self> {
3090+
pub const fn checked_shr(self, rhs: u32) -> Option<Self> {
30783091
let (a, b) = self.overflowing_shr(rhs);
30793092
if b {None} else {Some(a)}
30803093
}

0 commit comments

Comments
 (0)