Skip to content

Commit d9e3d2a

Browse files
Make euclidean division const
Co-Authored-By: 9999years <rbt@sent.as>
1 parent 126ad2b commit d9e3d2a

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
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_euclidean)]
7576
#![feature(const_panic)]
7677
#![feature(const_fn_union)]
7778
#![feature(const_generics)]

src/libcore/num/mod.rs

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -802,10 +802,11 @@ assert_eq!(", stringify!($SelfT), "::min_value().checked_div_euclid(-1), None);
802802
assert_eq!((1", stringify!($SelfT), ").checked_div_euclid(0), None);
803803
```"),
804804
#[stable(feature = "euclidean_division", since = "1.38.0")]
805+
#[rustc_const_unstable(feature = "const_int_euclidean", issue = "53718")]
805806
#[must_use = "this returns the result of the operation, \
806807
without modifying the original"]
807808
#[inline]
808-
pub fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
809+
pub const fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
809810
if rhs == 0 || (self == Self::min_value() && rhs == -1) {
810811
None
811812
} else {
@@ -860,10 +861,11 @@ assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(0), None);
860861
assert_eq!(", stringify!($SelfT), "::MIN.checked_rem_euclid(-1), None);
861862
```"),
862863
#[stable(feature = "euclidean_division", since = "1.38.0")]
864+
#[rustc_const_unstable(feature = "const_int_euclidean", issue = "53718")]
863865
#[must_use = "this returns the result of the operation, \
864866
without modifying the original"]
865867
#[inline]
866-
pub fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
868+
pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
867869
if rhs == 0 || (self == Self::min_value() && rhs == -1) {
868870
None
869871
} else {
@@ -1298,10 +1300,11 @@ assert_eq!(100", stringify!($SelfT), ".wrapping_div_euclid(10), 10);
12981300
assert_eq!((-128i8).wrapping_div_euclid(-1), -128);
12991301
```"),
13001302
#[stable(feature = "euclidean_division", since = "1.38.0")]
1303+
#[rustc_const_unstable(feature = "const_int_euclidean", issue = "53718")]
13011304
#[must_use = "this returns the result of the operation, \
13021305
without modifying the original"]
13031306
#[inline]
1304-
pub fn wrapping_div_euclid(self, rhs: Self) -> Self {
1307+
pub const fn wrapping_div_euclid(self, rhs: Self) -> Self {
13051308
self.overflowing_div_euclid(rhs).0
13061309
}
13071310
}
@@ -1356,10 +1359,11 @@ assert_eq!(100", stringify!($SelfT), ".wrapping_rem_euclid(10), 0);
13561359
assert_eq!((-128i8).wrapping_rem_euclid(-1), 0);
13571360
```"),
13581361
#[stable(feature = "euclidean_division", since = "1.38.0")]
1362+
#[rustc_const_unstable(feature = "const_int_euclidean", issue = "53718")]
13591363
#[must_use = "this returns the result of the operation, \
13601364
without modifying the original"]
13611365
#[inline]
1362-
pub fn wrapping_rem_euclid(self, rhs: Self) -> Self {
1366+
pub const fn wrapping_rem_euclid(self, rhs: Self) -> Self {
13631367
self.overflowing_rem_euclid(rhs).0
13641368
}
13651369
}
@@ -1669,9 +1673,10 @@ assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div_euclid(-1), (", stringi
16691673
```"),
16701674
#[inline]
16711675
#[stable(feature = "euclidean_division", since = "1.38.0")]
1676+
#[rustc_const_unstable(feature = "const_int_euclidean", issue = "53718")]
16721677
#[must_use = "this returns the result of the operation, \
16731678
without modifying the original"]
1674-
pub fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
1679+
pub const fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
16751680
if self == Self::min_value() && rhs == -1 {
16761681
(self, true)
16771682
} else {
@@ -1736,10 +1741,11 @@ assert_eq!(5", stringify!($SelfT), ".overflowing_rem_euclid(2), (1, false));
17361741
assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem_euclid(-1), (0, true));
17371742
```"),
17381743
#[stable(feature = "euclidean_division", since = "1.38.0")]
1744+
#[rustc_const_unstable(feature = "const_int_euclidean", issue = "53718")]
17391745
#[must_use = "this returns the result of the operation, \
17401746
without modifying the original"]
17411747
#[inline]
1742-
pub fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
1748+
pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
17431749
if self == Self::min_value() && rhs == -1 {
17441750
(0, true)
17451751
} else {
@@ -1981,11 +1987,12 @@ assert_eq!((-a).div_euclid(b), -2); // -7 >= 4 * -2
19811987
assert_eq!((-a).div_euclid(-b), 2); // -7 >= -4 * 2
19821988
```"),
19831989
#[stable(feature = "euclidean_division", since = "1.38.0")]
1990+
#[rustc_const_unstable(feature = "const_int_euclidean", issue = "53718")]
19841991
#[must_use = "this returns the result of the operation, \
19851992
without modifying the original"]
19861993
#[inline]
19871994
#[rustc_inherit_overflow_checks]
1988-
pub fn div_euclid(self, rhs: Self) -> Self {
1995+
pub const fn div_euclid(self, rhs: Self) -> Self {
19891996
let q = self / rhs;
19901997
if self % rhs < 0 {
19911998
return if rhs > 0 { q - 1 } else { q + 1 }
@@ -2020,11 +2027,12 @@ assert_eq!(a.rem_euclid(-b), 3);
20202027
assert_eq!((-a).rem_euclid(-b), 1);
20212028
```"),
20222029
#[stable(feature = "euclidean_division", since = "1.38.0")]
2030+
#[rustc_const_unstable(feature = "const_int_euclidean", issue = "53718")]
20232031
#[must_use = "this returns the result of the operation, \
20242032
without modifying the original"]
20252033
#[inline]
20262034
#[rustc_inherit_overflow_checks]
2027-
pub fn rem_euclid(self, rhs: Self) -> Self {
2035+
pub const fn rem_euclid(self, rhs: Self) -> Self {
20282036
let r = self % rhs;
20292037
if r < 0 {
20302038
if rhs < 0 {
@@ -2939,10 +2947,11 @@ assert_eq!(128", stringify!($SelfT), ".checked_div_euclid(2), Some(64));
29392947
assert_eq!(1", stringify!($SelfT), ".checked_div_euclid(0), None);
29402948
```"),
29412949
#[stable(feature = "euclidean_division", since = "1.38.0")]
2950+
#[rustc_const_unstable(feature = "const_int_euclidean", issue = "53718")]
29422951
#[must_use = "this returns the result of the operation, \
29432952
without modifying the original"]
29442953
#[inline]
2945-
pub fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
2954+
pub const fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
29462955
if rhs == 0 {
29472956
None
29482957
} else {
@@ -2992,10 +3001,11 @@ assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(2), Some(1));
29923001
assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(0), None);
29933002
```"),
29943003
#[stable(feature = "euclidean_division", since = "1.38.0")]
3004+
#[rustc_const_unstable(feature = "const_int_euclidean", issue = "53718")]
29953005
#[must_use = "this returns the result of the operation, \
29963006
without modifying the original"]
29973007
#[inline]
2998-
pub fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
3008+
pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
29993009
if rhs == 0 {
30003010
None
30013011
} else {
@@ -3315,10 +3325,11 @@ Basic usage:
33153325
assert_eq!(100", stringify!($SelfT), ".wrapping_div_euclid(10), 10);
33163326
```"),
33173327
#[stable(feature = "euclidean_division", since = "1.38.0")]
3328+
#[rustc_const_unstable(feature = "const_int_euclidean", issue = "53718")]
33183329
#[must_use = "this returns the result of the operation, \
33193330
without modifying the original"]
33203331
#[inline]
3321-
pub fn wrapping_div_euclid(self, rhs: Self) -> Self {
3332+
pub const fn wrapping_div_euclid(self, rhs: Self) -> Self {
33223333
self / rhs
33233334
}
33243335
}
@@ -3366,10 +3377,11 @@ Basic usage:
33663377
assert_eq!(100", stringify!($SelfT), ".wrapping_rem_euclid(10), 0);
33673378
```"),
33683379
#[stable(feature = "euclidean_division", since = "1.38.0")]
3380+
#[rustc_const_unstable(feature = "const_int_euclidean", issue = "53718")]
33693381
#[must_use = "this returns the result of the operation, \
33703382
without modifying the original"]
33713383
#[inline]
3372-
pub fn wrapping_rem_euclid(self, rhs: Self) -> Self {
3384+
pub const fn wrapping_rem_euclid(self, rhs: Self) -> Self {
33733385
self % rhs
33743386
}
33753387
}
@@ -3645,9 +3657,10 @@ assert_eq!(5", stringify!($SelfT), ".overflowing_div_euclid(2), (2, false));
36453657
```"),
36463658
#[inline]
36473659
#[stable(feature = "euclidean_division", since = "1.38.0")]
3660+
#[rustc_const_unstable(feature = "const_int_euclidean", issue = "53718")]
36483661
#[must_use = "this returns the result of the operation, \
36493662
without modifying the original"]
3650-
pub fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
3663+
pub const fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
36513664
(self / rhs, false)
36523665
}
36533666
}
@@ -3704,9 +3717,10 @@ assert_eq!(5", stringify!($SelfT), ".overflowing_rem_euclid(2), (1, false));
37043717
```"),
37053718
#[inline]
37063719
#[stable(feature = "euclidean_division", since = "1.38.0")]
3720+
#[rustc_const_unstable(feature = "const_int_euclidean", issue = "53718")]
37073721
#[must_use = "this returns the result of the operation, \
37083722
without modifying the original"]
3709-
pub fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
3723+
pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
37103724
(self % rhs, false)
37113725
}
37123726
}
@@ -3897,11 +3911,12 @@ Basic usage:
38973911
assert_eq!(7", stringify!($SelfT), ".div_euclid(4), 1); // or any other integer type
38983912
```"),
38993913
#[stable(feature = "euclidean_division", since = "1.38.0")]
3914+
#[rustc_const_unstable(feature = "const_int_euclidean", issue = "53718")]
39003915
#[must_use = "this returns the result of the operation, \
39013916
without modifying the original"]
39023917
#[inline]
39033918
#[rustc_inherit_overflow_checks]
3904-
pub fn div_euclid(self, rhs: Self) -> Self {
3919+
pub const fn div_euclid(self, rhs: Self) -> Self {
39053920
self / rhs
39063921
}
39073922
}
@@ -3926,11 +3941,12 @@ Basic usage:
39263941
assert_eq!(7", stringify!($SelfT), ".rem_euclid(4), 3); // or any other integer type
39273942
```"),
39283943
#[stable(feature = "euclidean_division", since = "1.38.0")]
3944+
#[rustc_const_unstable(feature = "const_int_euclidean", issue = "53718")]
39293945
#[must_use = "this returns the result of the operation, \
39303946
without modifying the original"]
39313947
#[inline]
39323948
#[rustc_inherit_overflow_checks]
3933-
pub fn rem_euclid(self, rhs: Self) -> Self {
3949+
pub const fn rem_euclid(self, rhs: Self) -> Self {
39343950
self % rhs
39353951
}
39363952
}

0 commit comments

Comments
 (0)