Add doctests to and fix saturating_div for signed integer types

This commit is contained in:
Michael Watzko 2021-08-19 11:06:08 +02:00
parent 742d450783
commit 6bb3acab74
2 changed files with 23 additions and 9 deletions

View File

@ -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);")]
///
/// ```
#[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
}
}

View File

@ -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)]