Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1195,9 +1195,18 @@ pub trait Saturating {
/// Saturating subtraction operator.
/// Returns a-b, saturating at the numeric bounds instead of overflowing.
fn saturating_sub(self, v: Self) -> Self;

/// Saturating multiplication operator.
/// Returns a\*b, saturating at the numeric bounds instead of overflowing.
fn saturating_mul(self, v: Self) -> Self;

/// Saturating division operator.
/// Returns a/b, saturating at the numeric bounds instead of overflowing.
fn saturating_div(self, v: Self) -> Self;
}

impl<T: CheckedAdd + CheckedSub + Zero + PartialOrd + Bounded> Saturating for T {
impl<T: CheckedAdd + CheckedSub + CheckedDiv + CheckedMul + Zero + PartialOrd + Bounded>
Saturating for T {
#[inline]
fn saturating_add(self, v: T) -> T {
match self.checked_add(&v) {
Expand All @@ -1221,6 +1230,32 @@ impl<T: CheckedAdd + CheckedSub + Zero + PartialOrd + Bounded> Saturating for T
}
}
}

#[inline]
fn saturating_mul(self, v: T) -> T {
match self.checked_mul(&v) {
Some(x) => x,
None => if (self < Zero::zero()) == (v < Zero::zero()) {
Bounded::max_value()
} else {
Bounded::min_value()
}
}
}

#[inline]
fn saturating_div(self, v: T) -> T {
match self.checked_div(&v) {
Some(x) => x,
None => if v == Zero::zero() {
fail!("div by zero cannot be saturated")
} else if (self < Zero::zero()) == (v < Zero::zero()) {
Bounded::max_value()
} else {
Bounded::min_value()
}
}
}
}

/// Performs addition that returns `None` instead of wrapping around on overflow.
Expand Down