Skip to content

Commit

Permalink
Move saturating operator methods into Int
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanzab committed Nov 12, 2014
1 parent d1eb68e commit d431a67
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
59 changes: 22 additions & 37 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use option::Option;
pub use core::num::{Num, div_rem, Zero, zero, One, one};
pub use core::num::{Signed, abs, signum};
pub use core::num::{Unsigned, pow, Bounded};
pub use core::num::{Primitive, Int, UnsignedInt, Saturating};
pub use core::num::{Primitive, Int, UnsignedInt};
pub use core::num::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv};
pub use core::num::{cast, FromPrimitive, NumCast, ToPrimitive};
pub use core::num::{next_power_of_two, is_power_of_two};
Expand Down
1 change: 0 additions & 1 deletion src/libtest/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ enum NamePadding { PadNone, PadOnLeft, PadOnRight }

impl TestDesc {
fn padded_name(&self, column_count: uint, align: NamePadding) -> String {
use std::num::Saturating;
let mut name = String::from_str(self.name.as_slice());
let fill = column_count.saturating_sub(name.len());
let mut pad = " ".repeat(fill);
Expand Down

0 comments on commit d431a67

Please sign in to comment.