Optimize saturating_add_signed

This commit is contained in:
Benoît du Garreau 2021-10-04 18:52:17 +02:00
parent 4846fd92c0
commit 47edde1086

View File

@ -1037,10 +1037,13 @@ pub const fn saturating_add(self, rhs: Self) -> Self {
without modifying the original"]
#[inline]
pub const fn saturating_add_signed(self, rhs: $SignedT) -> Self {
if rhs >= 0 {
self.saturating_add(rhs as Self)
let (res, overflow) = self.overflowing_add(rhs as Self);
if overflow == (rhs < 0) {
res
} else if overflow {
Self::MAX
} else {
self.saturating_sub(rhs.unsigned_abs())
0
}
}