Skip to content

Commit

Permalink
Restore and deprecate Saturating trait. Fix up doc strings for Satura…
Browse files Browse the repository at this point in the history
…ting* traits.
  • Loading branch information
trepetti committed May 2, 2020
1 parent 964752f commit 86c3126
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
6 changes: 2 additions & 4 deletions src/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::ops::{BitAnd, BitOr, BitXor, Not, Shl, Shr};

use bounds::Bounded;
use ops::checked::*;
use ops::saturating::*;
use ops::saturating::Saturating;
use {Num, NumCast};

/// Generic trait for primitive integers.
Expand Down Expand Up @@ -50,9 +50,7 @@ pub trait PrimInt:
+ CheckedSub<Output = Self>
+ CheckedMul<Output = Self>
+ CheckedDiv<Output = Self>
+ SaturatingAdd
+ SaturatingSub
+ SaturatingMul
+ Saturating
{
/// Returns the number of ones in the binary representation of `self`.
///
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub use ops::checked::{
};
pub use ops::inv::Inv;
pub use ops::mul_add::{MulAdd, MulAddAssign};
pub use ops::saturating::{SaturatingAdd, SaturatingMul, SaturatingSub};
pub use ops::saturating::{Saturating, SaturatingAdd, SaturatingMul, SaturatingSub};
pub use ops::wrapping::{
WrappingAdd, WrappingMul, WrappingNeg, WrappingShl, WrappingShr, WrappingSub,
};
Expand Down
38 changes: 35 additions & 3 deletions src/ops/saturating.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
use core::ops::{Add, Mul, Sub};

/// Saturating math operations. Deprecated, use `SaturatingAdd`, `SaturatingSub` and
/// `SaturatingMul` instead.
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;
}

macro_rules! deprecated_saturating_impl {
($trait_name:ident for $($t:ty)*) => {$(
impl $trait_name for $t {
#[inline]
fn saturating_add(self, v: Self) -> Self {
Self::saturating_add(self, v)
}

#[inline]
fn saturating_sub(self, v: Self) -> Self {
Self::saturating_sub(self, v)
}
}
)*}
}

deprecated_saturating_impl!(Saturating for isize usize i8 u8 i16 u16 i32 u32 i64 u64);
#[cfg(has_i128)]
deprecated_saturating_impl!(Saturating for i128 u128);

macro_rules! saturating_impl {
($trait_name:ident, $method:ident, $t:ty) => {
impl $trait_name for $t {
Expand All @@ -19,7 +51,7 @@ macro_rules! saturating_impl {
};
}

/// Performs addition that saturates high on overflow and low on underflow.
/// Performs addition that saturates at the numeric bounds instead of overflowing.
pub trait SaturatingAdd: Sized + Add<Self, Output = Self> {
/// Saturating addition. Computes `self + other`, saturating at the relevant high or low boundary of
/// the type.
Expand All @@ -42,7 +74,7 @@ saturating_impl!(SaturatingAdd, saturating_add, isize);
#[cfg(has_i128)]
saturating_impl!(SaturatingAdd, saturating_add, i128);

/// Performs subtraction that saturates high on overflow and low on underflow.
/// Performs subtraction that saturates at the numeric bounds instead of overflowing.
pub trait SaturatingSub: Sized + Sub<Self, Output = Self> {
/// Saturating subtraction. Computes `self - other`, saturating at the relevant high or low boundary of
/// the type.
Expand All @@ -65,7 +97,7 @@ saturating_impl!(SaturatingSub, saturating_sub, isize);
#[cfg(has_i128)]
saturating_impl!(SaturatingSub, saturating_sub, i128);

/// Performs subtraction that saturates high on overflow and low on underflow.
/// Performs multiplication that saturates at the numeric bounds instead of overflowing.
pub trait SaturatingMul: Sized + Mul<Self, Output = Self> {
/// Saturating multiplication. Computes `self * other`, saturating at the relevant high or low boundary of
/// the type.
Expand Down

0 comments on commit 86c3126

Please sign in to comment.