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 @@ macro_rules! uint_impl {
without modifying the original"] without modifying the original"]
#[inline] #[inline]
pub const fn saturating_add_signed(self, rhs: $SignedT) -> Self { pub const fn saturating_add_signed(self, rhs: $SignedT) -> Self {
if rhs >= 0 { let (res, overflow) = self.overflowing_add(rhs as Self);
self.saturating_add(rhs as Self) if overflow == (rhs < 0) {
res
} else if overflow {
Self::MAX
} else { } else {
self.saturating_sub(rhs.unsigned_abs()) 0
} }
} }