diff options
| author | Brendan Zabarauskas <bjzaba@yahoo.com.au> | 2014-11-09 17:46:33 +1100 |
|---|---|---|
| committer | Brendan Zabarauskas <bjzaba@yahoo.com.au> | 2014-11-13 02:02:44 +1100 |
| commit | d431a67cecc426a4d24dcf24d72a9147b8e08860 (patch) | |
| tree | 55ed6997324f89d1b29afb8a987fef8d57389e3b /src/libcore | |
| parent | d1eb68e8d7d2883c70304021d5443c96bca18abb (diff) | |
| download | rust-d431a67cecc426a4d24dcf24d72a9147b8e08860.tar.gz rust-d431a67cecc426a4d24dcf24d72a9147b8e08860.zip | |
Move saturating operator methods into Int
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/iter.rs | 2 | ||||
| -rw-r--r-- | src/libcore/num/mod.rs | 59 | ||||
| -rw-r--r-- | src/libcore/slice.rs | 2 | ||||
| -rw-r--r-- | src/libcore/str.rs | 2 |
4 files changed, 25 insertions, 40 deletions
diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index a717d3c2c99..73d3aef5ee0 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -62,7 +62,7 @@ use clone::Clone; use cmp; use cmp::{PartialEq, PartialOrd, Ord}; use mem; -use num::{Zero, One, CheckedAdd, CheckedSub, Saturating, ToPrimitive, Int}; +use num::{Zero, One, CheckedAdd, CheckedSub, ToPrimitive, Int}; use ops::{Add, Mul, Sub}; use option::{Option, Some, None}; use uint; diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 8bab90c57e2..457aa57d93d 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -525,6 +525,28 @@ pub trait Int: Primitive fn to_le(self) -> Self { if cfg!(target_endian = "little") { self } else { self.swap_bytes() } } + + /// Saturating addition. Returns `self + other`, saturating at the + /// numeric bounds instead of overflowing. + #[inline] + fn saturating_add(self, other: Self) -> Self { + match self.checked_add(&other) { + Some(x) => x, + None if other >= Zero::zero() => Bounded::max_value(), + None => Bounded::min_value(), + } + } + + /// Saturating subtraction. Returns `self - other`, saturating at the + /// numeric bounds instead of overflowing. + #[inline] + fn saturating_sub(self, other: Self) -> Self { + match self.checked_sub(&other) { + Some(x) => x, + None if other >= Zero::zero() => Bounded::min_value(), + None => Bounded::max_value(), + } + } } macro_rules! int_impl { @@ -1150,43 +1172,6 @@ impl_num_cast!(int, to_int) impl_num_cast!(f32, to_f32) impl_num_cast!(f64, to_f64) -/// Saturating math operations -pub trait Saturating { - /// Saturating addition operator. - /// Returns a+b, saturating at the numeric bounds instead of overflowing. - fn saturating_add(self, v: Self) -> Self; - - /// Saturating subtraction operator. - /// Returns a-b, saturating at the numeric bounds instead of overflowing. - fn saturating_sub(self, v: Self) -> Self; -} - -impl<T: CheckedAdd + CheckedSub + Zero + PartialOrd + Bounded> Saturating for T { - #[inline] - fn saturating_add(self, v: T) -> T { - match self.checked_add(&v) { - Some(x) => x, - None => if v >= Zero::zero() { - Bounded::max_value() - } else { - Bounded::min_value() - } - } - } - - #[inline] - fn saturating_sub(self, v: T) -> T { - match self.checked_sub(&v) { - Some(x) => x, - None => if v >= Zero::zero() { - Bounded::min_value() - } else { - Bounded::max_value() - } - } - } -} - /// Performs addition that returns `None` instead of wrapping around on overflow. pub trait CheckedAdd: Add<Self, Self> { /// Adds two numbers, checking for overflow. If overflow happens, `None` is returned. diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index 138422ceff1..fd01ed69f81 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -40,7 +40,7 @@ use cmp::{PartialEq, PartialOrd, Eq, Ord, Ordering, Less, Equal, Greater, Equiv} use cmp; use default::Default; use iter::*; -use num::{CheckedAdd, Saturating, div_rem}; +use num::{CheckedAdd, Int, div_rem}; use ops; use option::{None, Option, Some}; use ptr; diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 4c1bfb61709..3aee6867fd5 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -25,7 +25,7 @@ use iter::{Map, Iterator}; use iter::{DoubleEndedIterator, ExactSize}; use iter::range; use kinds::Sized; -use num::{CheckedMul, Saturating}; +use num::{CheckedMul, Int}; use option::{Option, None, Some}; use raw::Repr; use slice::{mod, SlicePrelude}; |
