]> git.lizzy.rs Git - rust.git/commitdiff
Add saturating_div to unsigned integer types
authorMichael Watzko <michael@watzko.de>
Thu, 19 Aug 2021 09:07:34 +0000 (11:07 +0200)
committerMichael Watzko <michael@watzko.de>
Thu, 19 Aug 2021 09:07:34 +0000 (11:07 +0200)
library/core/src/num/uint_macros.rs

index ae113a47e95d61a0b7712471012694045fb6207f..d6bd3115a0254dc330d6491a0e4d58770bf5bc04 100644 (file)
@@ -1041,6 +1041,36 @@ pub const fn saturating_mul(self, rhs: Self) -> Self {
             }
         }
 
+        /// 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.
         ///