Skip to content

Commit 47edde1

Browse files
committed
Optimize saturating_add_signed
1 parent 4846fd9 commit 47edde1

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

library/core/src/num/uint_macros.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1037,10 +1037,13 @@ macro_rules! uint_impl {
10371037
without modifying the original"]
10381038
#[inline]
10391039
pub const fn saturating_add_signed(self, rhs: $SignedT) -> Self {
1040-
if rhs >= 0 {
1041-
self.saturating_add(rhs as Self)
1040+
let (res, overflow) = self.overflowing_add(rhs as Self);
1041+
if overflow == (rhs < 0) {
1042+
res
1043+
} else if overflow {
1044+
Self::MAX
10421045
} else {
1043-
self.saturating_sub(rhs.unsigned_abs())
1046+
0
10441047
}
10451048
}
10461049

0 commit comments

Comments
 (0)