Skip to content

Commit 6bb3aca

Browse files
committed
Add doctests to and fix saturating_div for signed integer types
1 parent 742d450 commit 6bb3aca

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

library/core/src/num/int_macros.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -926,21 +926,34 @@ macro_rules! int_impl {
926926
/// Basic usage:
927927
///
928928
/// ```
929+
/// #![feature(saturating_div)]
930+
///
931+
#[doc = concat!("assert_eq!(5", stringify!($SelfT), ".saturating_div(2), 2);")]
932+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.saturating_div(-1), ", stringify!($SelfT), "::MIN + 1);")]
933+
#[doc = concat!("assert_eq!(", stringify!($SelfT), "::MIN.saturating_div(-1), ", stringify!($SelfT), "::MAX);")]
929934
///
930935
/// ```
931-
#[unstable(feature = "saturating_int_impl", issue = "87920")]
932-
#[rustc_const_unstable(feature = "saturating_int_impl", issue = "87920")]
936+
///
937+
/// ```should_panic
938+
/// #![feature(saturating_div)]
939+
///
940+
#[doc = concat!("let _ = 1", stringify!($SelfT), ".saturating_div(0);")]
941+
///
942+
/// ```
943+
#[unstable(feature = "saturating_div", issue = "87920")]
944+
#[rustc_const_unstable(feature = "saturating_div", issue = "87920")]
933945
#[must_use = "this returns the result of the operation, \
934946
without modifying the original"]
935947
#[inline]
936948
pub const fn saturating_div(self, rhs: Self) -> Self {
937-
match self.checked_div(rhs) {
938-
Some(x) => x,
939-
None => if (self < 0) == (rhs < 0) {
940-
Self::MAX
941-
} else {
942-
Self::MIN
943-
}
949+
let (result, overflowed) = self.overflowing_div(rhs);
950+
951+
if !overflowed {
952+
result
953+
} else if (self < 0) == (rhs < 0) {
954+
Self::MAX
955+
} else {
956+
Self::MIN
944957
}
945958
}
946959

library/std/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@
308308
#![feature(ptr_internals)]
309309
#![feature(rustc_attrs)]
310310
#![feature(rustc_private)]
311+
#![feature(saturating_div)]
311312
#![feature(saturating_int_impl)]
312313
#![feature(slice_concat_ext)]
313314
#![feature(slice_internals)]

0 commit comments

Comments
 (0)