Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Saturating type (based on Wrapping type) #87921

Merged
merged 20 commits into from
Aug 29, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
31 changes: 22 additions & 9 deletions library/core/src/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -926,21 +926,34 @@ macro_rules! int_impl {
/// Basic usage:
///
/// ```
/// #![feature(saturating_div)]
///
#[doc = concat!("assert_eq!(5", stringify!($SelfT), ".saturating_div(2), 2);")]
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_div(-1), ", stringify!($SelfT), "::MIN + 1);")]
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_div(-1), ", stringify!($SelfT), "::MAX);")]
///
kellerkindt marked this conversation as resolved.
Show resolved Hide resolved
/// ```
#[unstable(feature = "saturating_int_impl", issue = "87920")]
#[rustc_const_unstable(feature = "saturating_int_impl", issue = "87920")]
///
/// ```should_panic
/// #![feature(saturating_div)]
///
#[doc = concat!("let _ = 1", stringify!($SelfT), ".saturating_div(0);")]
///
/// ```
#[unstable(feature = "saturating_div", issue = "87920")]
#[rustc_const_unstable(feature = "saturating_div", issue = "87920")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_div(self, rhs: Self) -> Self {
match self.checked_div(rhs) {
Some(x) => x,
None => if (self < 0) == (rhs < 0) {
Self::MAX
} else {
Self::MIN
}
let (result, overflowed) = self.overflowing_div(rhs);

if !overflowed {
result
} else if (self < 0) == (rhs < 0) {
Self::MAX
} else {
Self::MIN
kennytm marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
68 changes: 31 additions & 37 deletions library/core/src/num/saturating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,37 @@ macro_rules! saturating_impl {
}
forward_ref_op_assign! { impl MulAssign, mul_assign for Saturating<$t>, Saturating<$t> }

/// # Examples
///
/// Basic usage:
///
/// ```
/// #![feature(saturating_int_impl, saturating_div)]
/// use std::num::Saturating;
///
#[doc = concat!("assert_eq!(Saturating(2", stringify!($t), "), Saturating(5", stringify!($t), ") / Saturating(2));")]
#[doc = concat!("assert_eq!(Saturating(", stringify!($t), "::MAX), Saturating(", stringify!($t), "::MAX) / Saturating(1));")]
#[doc = concat!("assert_eq!(Saturating(", stringify!($t), "::MIN), Saturating(", stringify!($t), "::MIN) / Saturating(1));")]
/// ```
///
/// ```should_panic
/// #![feature(saturating_int_impl, saturating_div)]
/// use std::num::Saturating;
///
#[doc = concat!("let _ = Saturating(0", stringify!($t), ") / Saturating(0);")]
/// ```
#[unstable(feature = "saturating_int_impl", issue = "87920")]
Comment on lines +271 to +290
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure whether to keep this doc(-tests)

impl Div for Saturating<$t> {
type Output = Saturating<$t>;

#[inline]
fn div(self, other: Saturating<$t>) -> Saturating<$t> {
Saturating(self.0.saturating_div(other.0))
}
}
forward_ref_binop! { impl Div, div for Saturating<$t>, Saturating<$t>,
#[unstable(feature = "saturating_int_impl", issue = "87920")] }

#[unstable(feature = "saturating_int_impl", issue = "87920")]
impl DivAssign for Saturating<$t> {
#[inline]
Expand Down Expand Up @@ -851,31 +882,6 @@ macro_rules! saturating_int_impl_signed {
}
forward_ref_unop! { impl Neg, neg for Saturating<$t>,
#[unstable(feature = "saturating_int_impl", issue = "87920")] }

/// # Examples
///
/// Basic usage:
///
/// ```
/// #![feature(saturating_int_impl)]
/// use std::num::Saturating;
///
#[doc = concat!("assert_eq!(Saturating(", stringify!($t), "::MIN + 1), Saturating(", stringify!($t), "::MAX) / Saturating(-1));")]
#[doc = concat!("assert_eq!(Saturating(", stringify!($t), "::MAX), Saturating(", stringify!($t), "::MIN) / Saturating(-1));")]
#[doc = concat!("assert_eq!(Saturating(", stringify!($t), "::MAX), Saturating(", stringify!($t), "::MAX) / Saturating(1));")]
#[doc = concat!("assert_eq!(Saturating(", stringify!($t), "::MIN), Saturating(", stringify!($t), "::MIN) / Saturating(1));")]
/// ```
#[unstable(feature = "saturating_int_impl", issue = "87920")]
impl Div for Saturating<$t> {
type Output = Saturating<$t>;

#[inline]
fn div(self, other: Saturating<$t>) -> Saturating<$t> {
Saturating(self.0.saturating_div(other.0))
}
}
forward_ref_binop! { impl Div, div for Saturating<$t>, Saturating<$t>,
#[unstable(feature = "saturating_int_impl", issue = "87920")] }
)*)
}

Expand Down Expand Up @@ -924,18 +930,6 @@ macro_rules! saturating_int_impl_unsigned {
}

}

#[unstable(feature = "saturating_int_impl", issue = "87920")]
impl Div for Saturating<$t> {
type Output = Saturating<$t>;

#[inline]
fn div(self, other: Saturating<$t>) -> Saturating<$t> {
Saturating(self.0.div(other.0))
}
}
forward_ref_binop! { impl Div, div for Saturating<$t>, Saturating<$t>,
#[unstable(feature = "saturating_int_impl", issue = "87920")] }
)*)
}

Expand Down
30 changes: 30 additions & 0 deletions library/core/src/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,36 @@ macro_rules! uint_impl {
}
}

/// Saturating integer division. Computes `self / rhs`, saturating at the
/// numeric bounds instead of overflowing.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// #![feature(saturating_div)]
///
#[doc = concat!("assert_eq!(5", stringify!($SelfT), ".saturating_div(2), 2);")]
///
/// ```
///
/// ```should_panic
/// #![feature(saturating_div)]
///
#[doc = concat!("let _ = 1", stringify!($SelfT), ".saturating_div(0);")]
///
/// ```
#[unstable(feature = "saturating_div", issue = "87920")]
#[rustc_const_unstable(feature = "saturating_div", issue = "87920")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub const fn saturating_div(self, rhs: Self) -> Self {
// on unsigned types, there is no overflow in integer division
self.wrapping_div(rhs)
}

/// Saturating integer exponentiation. Computes `self.pow(exp)`,
/// saturating at the numeric bounds instead of overflowing.
///
Expand Down
1 change: 1 addition & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@
#![feature(ptr_internals)]
#![feature(rustc_attrs)]
#![feature(rustc_private)]
#![feature(saturating_div)]
#![feature(saturating_int_impl)]
#![feature(slice_concat_ext)]
#![feature(slice_internals)]
Expand Down