Skip to content

Commit b422a19

Browse files
Make saturating_mul a const fn
Co-Authored-By: 9999years <rbt@sent.as>
1 parent de52a54 commit b422a19

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/libcore/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
#![feature(const_int_checked)]
7676
#![feature(const_int_euclidean)]
7777
#![feature(const_int_overflowing)]
78+
#![feature(const_int_saturating)]
7879
#![feature(const_panic)]
7980
#![feature(const_fn_union)]
8081
#![feature(const_generics)]

src/libcore/num/mod.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,17 +1142,19 @@ assert_eq!(", stringify!($SelfT), "::MIN.saturating_mul(10), ", stringify!($Self
11421142
$EndFeature, "
11431143
```"),
11441144
#[stable(feature = "wrapping", since = "1.7.0")]
1145+
#[rustc_const_unstable(feature = "const_int_saturating", issue = "53718")]
11451146
#[must_use = "this returns the result of the operation, \
11461147
without modifying the original"]
11471148
#[inline]
1148-
pub fn saturating_mul(self, rhs: Self) -> Self {
1149-
self.checked_mul(rhs).unwrap_or_else(|| {
1150-
if (self < 0) == (rhs < 0) {
1149+
pub const fn saturating_mul(self, rhs: Self) -> Self {
1150+
match self.checked_mul(rhs) {
1151+
Some(x) => x,
1152+
None => if (self < 0) == (rhs < 0) {
11511153
Self::max_value()
11521154
} else {
11531155
Self::min_value()
11541156
}
1155-
})
1157+
}
11561158
}
11571159
}
11581160

@@ -3195,11 +3197,15 @@ assert_eq!((", stringify!($SelfT), "::MAX).saturating_mul(10), ", stringify!($Se
31953197
"::MAX);", $EndFeature, "
31963198
```"),
31973199
#[stable(feature = "wrapping", since = "1.7.0")]
3200+
#[rustc_const_unstable(feature = "const_int_saturating", issue = "53718")]
31983201
#[must_use = "this returns the result of the operation, \
31993202
without modifying the original"]
32003203
#[inline]
3201-
pub fn saturating_mul(self, rhs: Self) -> Self {
3202-
self.checked_mul(rhs).unwrap_or(Self::max_value())
3204+
pub const fn saturating_mul(self, rhs: Self) -> Self {
3205+
match self.checked_mul(rhs) {
3206+
Some(x) => x,
3207+
None => Self::max_value(),
3208+
}
32033209
}
32043210
}
32053211

0 commit comments

Comments
 (0)