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: /// 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, \ #[must_use = "this returns the result of the operation, \
without modifying the original"] without modifying the original"]
#[inline] #[inline]
pub const fn saturating_div(self, rhs: Self) -> Self { pub const fn saturating_div(self, rhs: Self) -> Self {
match self.checked_div(rhs) { let (result, overflowed) = self.overflowing_div(rhs);
Some(x) => x,
None => if (self < 0) == (rhs < 0) { if !overflowed {
Self::MAX result
} else { } else if (self < 0) == (rhs < 0) {
Self::MIN Self::MAX
} } else {
Self::MIN
} }
} }

View File

@ -308,6 +308,7 @@
#![feature(ptr_internals)] #![feature(ptr_internals)]
#![feature(rustc_attrs)] #![feature(rustc_attrs)]
#![feature(rustc_private)] #![feature(rustc_private)]
#![feature(saturating_div)]
#![feature(saturating_int_impl)] #![feature(saturating_int_impl)]
#![feature(slice_concat_ext)] #![feature(slice_concat_ext)]
#![feature(slice_internals)] #![feature(slice_internals)]