Skip to content

Commit 9c45d16

Browse files
committed
Auto merge of #65107 - llogiq:const-checked, r=<try>
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.
2 parents 2e72448 + 257fb1e commit 9c45d16

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

src/libcore/num/mod.rs

+24-24
Original file line numberDiff line numberDiff line change
@@ -626,9 +626,9 @@ $EndFeature, "
626626
#[must_use = "this returns the result of the operation, \
627627
without modifying the original"]
628628
#[inline]
629-
pub fn checked_add(self, rhs: Self) -> Option<Self> {
629+
pub const fn checked_add(self, rhs: Self) -> Option<Self> {
630630
let (a, b) = self.overflowing_add(rhs);
631-
if b {None} else {Some(a)}
631+
[Some(a), None][b as usize]
632632
}
633633
}
634634

@@ -650,9 +650,9 @@ $EndFeature, "
650650
#[must_use = "this returns the result of the operation, \
651651
without modifying the original"]
652652
#[inline]
653-
pub fn checked_sub(self, rhs: Self) -> Option<Self> {
653+
pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
654654
let (a, b) = self.overflowing_sub(rhs);
655-
if b {None} else {Some(a)}
655+
[Some(a), None][b as usize]
656656
}
657657
}
658658

@@ -674,9 +674,9 @@ $EndFeature, "
674674
#[must_use = "this returns the result of the operation, \
675675
without modifying the original"]
676676
#[inline]
677-
pub fn checked_mul(self, rhs: Self) -> Option<Self> {
677+
pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
678678
let (a, b) = self.overflowing_mul(rhs);
679-
if b {None} else {Some(a)}
679+
[Some(a), None][b as usize]
680680
}
681681
}
682682

@@ -808,9 +808,9 @@ $EndFeature, "
808808
```"),
809809
#[stable(feature = "wrapping", since = "1.7.0")]
810810
#[inline]
811-
pub fn checked_neg(self) -> Option<Self> {
811+
pub const fn checked_neg(self) -> Option<Self> {
812812
let (a, b) = self.overflowing_neg();
813-
if b {None} else {Some(a)}
813+
[Some(a), None][b as usize]
814814
}
815815
}
816816

@@ -831,9 +831,9 @@ $EndFeature, "
831831
#[must_use = "this returns the result of the operation, \
832832
without modifying the original"]
833833
#[inline]
834-
pub fn checked_shl(self, rhs: u32) -> Option<Self> {
834+
pub const fn checked_shl(self, rhs: u32) -> Option<Self> {
835835
let (a, b) = self.overflowing_shl(rhs);
836-
if b {None} else {Some(a)}
836+
[Some(a), None][b as usize]
837837
}
838838
}
839839

@@ -854,9 +854,9 @@ $EndFeature, "
854854
#[must_use = "this returns the result of the operation, \
855855
without modifying the original"]
856856
#[inline]
857-
pub fn checked_shr(self, rhs: u32) -> Option<Self> {
857+
pub const fn checked_shr(self, rhs: u32) -> Option<Self> {
858858
let (a, b) = self.overflowing_shr(rhs);
859-
if b {None} else {Some(a)}
859+
[Some(a), None][b as usize]
860860
}
861861
}
862862

@@ -2679,9 +2679,9 @@ assert_eq!((", stringify!($SelfT), "::max_value() - 2).checked_add(3), None);",
26792679
#[must_use = "this returns the result of the operation, \
26802680
without modifying the original"]
26812681
#[inline]
2682-
pub fn checked_add(self, rhs: Self) -> Option<Self> {
2682+
pub const fn checked_add(self, rhs: Self) -> Option<Self> {
26832683
let (a, b) = self.overflowing_add(rhs);
2684-
if b {None} else {Some(a)}
2684+
[Some(a), None][b as usize]
26852685
}
26862686
}
26872687

@@ -2701,9 +2701,9 @@ assert_eq!(0", stringify!($SelfT), ".checked_sub(1), None);", $EndFeature, "
27012701
#[must_use = "this returns the result of the operation, \
27022702
without modifying the original"]
27032703
#[inline]
2704-
pub fn checked_sub(self, rhs: Self) -> Option<Self> {
2704+
pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
27052705
let (a, b) = self.overflowing_sub(rhs);
2706-
if b {None} else {Some(a)}
2706+
[Some(a), None][b as usize]
27072707
}
27082708
}
27092709

@@ -2723,9 +2723,9 @@ assert_eq!(", stringify!($SelfT), "::max_value().checked_mul(2), None);", $EndFe
27232723
#[must_use = "this returns the result of the operation, \
27242724
without modifying the original"]
27252725
#[inline]
2726-
pub fn checked_mul(self, rhs: Self) -> Option<Self> {
2726+
pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
27272727
let (a, b) = self.overflowing_mul(rhs);
2728-
if b {None} else {Some(a)}
2728+
[Some(a), None][b as usize]
27292729
}
27302730
}
27312731

@@ -2845,9 +2845,9 @@ assert_eq!(1", stringify!($SelfT), ".checked_neg(), None);", $EndFeature, "
28452845
```"),
28462846
#[stable(feature = "wrapping", since = "1.7.0")]
28472847
#[inline]
2848-
pub fn checked_neg(self) -> Option<Self> {
2848+
pub const fn checked_neg(self) -> Option<Self> {
28492849
let (a, b) = self.overflowing_neg();
2850-
if b {None} else {Some(a)}
2850+
[Some(a), None][b as usize]
28512851
}
28522852
}
28532853

@@ -2867,9 +2867,9 @@ assert_eq!(0x10", stringify!($SelfT), ".checked_shl(129), None);", $EndFeature,
28672867
#[must_use = "this returns the result of the operation, \
28682868
without modifying the original"]
28692869
#[inline]
2870-
pub fn checked_shl(self, rhs: u32) -> Option<Self> {
2870+
pub const fn checked_shl(self, rhs: u32) -> Option<Self> {
28712871
let (a, b) = self.overflowing_shl(rhs);
2872-
if b {None} else {Some(a)}
2872+
[Some(a), None][b as usize]
28732873
}
28742874
}
28752875

@@ -2889,9 +2889,9 @@ assert_eq!(0x10", stringify!($SelfT), ".checked_shr(129), None);", $EndFeature,
28892889
#[must_use = "this returns the result of the operation, \
28902890
without modifying the original"]
28912891
#[inline]
2892-
pub fn checked_shr(self, rhs: u32) -> Option<Self> {
2892+
pub const fn checked_shr(self, rhs: u32) -> Option<Self> {
28932893
let (a, b) = self.overflowing_shr(rhs);
2894-
if b {None} else {Some(a)}
2894+
[Some(a), None][b as usize]
28952895
}
28962896
}
28972897

0 commit comments

Comments
 (0)