]> git.lizzy.rs Git - rust.git/commitdiff
Add doctests to and fix saturating_div for signed integer types
authorMichael Watzko <michael@watzko.de>
Thu, 19 Aug 2021 09:06:08 +0000 (11:06 +0200)
committerMichael Watzko <michael@watzko.de>
Thu, 19 Aug 2021 09:07:29 +0000 (11:07 +0200)
library/core/src/num/int_macros.rs
library/std/src/lib.rs

index f5966a53ed26ecde0072e208341902a04f00da57..7c36f4cdd203fb49b801fb8829d24ee76005ce46 100644 (file)
@@ -926,21 +926,34 @@ pub const fn saturating_mul(self, rhs: Self) -> Self {
         /// 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
             }
         }
 
index 818ca7df3e37a159de2335b072635ff4e1f80148..676ca5dea5d262f46495fb60963da115d25fe9d4 100644 (file)
 #![feature(ptr_internals)]
 #![feature(rustc_attrs)]
 #![feature(rustc_private)]
+#![feature(saturating_div)]
 #![feature(saturating_int_impl)]
 #![feature(slice_concat_ext)]
 #![feature(slice_internals)]