diff options
| author | Iago-lito <iago-lito@etak> | 2021-04-15 12:31:32 +0200 |
|---|---|---|
| committer | Iago-lito <iago-lito@etak> | 2021-06-09 17:28:33 +0200 |
| commit | 6979bb40f8dd1a67e7508779873e7525441de0ce (patch) | |
| tree | 3a1f2a394f89e3ccceca42eff50b07e0b0323ffd | |
| parent | 7e0b9a8bd0452a027582f26cd139fa92fba618f7 (diff) | |
| download | rust-6979bb40f8dd1a67e7508779873e7525441de0ce.tar.gz rust-6979bb40f8dd1a67e7508779873e7525441de0ce.zip | |
NonZero unchecked_mul.
| -rw-r--r-- | library/core/src/num/nonzero.rs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index 7a1b4d6c719..fdb874bc723 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -709,6 +709,34 @@ macro_rules! nonzero_unsigned_signed_operations { // so the result cannot be zero. unsafe { $Ty::new_unchecked(self.get().saturating_mul(other.get())) } } + + /// Multiply two non-zero integers together, + /// assuming overflow cannot occur. + /// This results in undefined behavior when + #[doc = concat!("`self * rhs > ", stringify!($Int), "::MAX`, ")] + #[doc = concat!("or `self * rhs < ", stringify!($Int), "::MIN`.")] + /// + /// # Examples + /// + /// ``` + /// #![feature(nonzero_ops)] + /// # #![feature(try_trait)] + #[doc = concat!("# use std::num::", stringify!($Ty), ";")] + /// + /// # fn main() -> Result<(), std::option::NoneError> { + #[doc = concat!("let two = ", stringify!($Ty), "::new(2)?;")] + #[doc = concat!("let four = ", stringify!($Ty), "::new(4)?;")] + /// + /// assert_eq!(four, unsafe { two.unchecked_mul(two) }); + /// # Ok(()) + /// # } + /// ``` + #[unstable(feature = "nonzero_ops", issue = "84186")] + #[inline] + pub unsafe fn unchecked_mul(self, other: $Ty) -> $Ty { + // SAFETY: The caller ensures there is no overflow. + unsafe { $Ty::new_unchecked(self.get().unchecked_mul(other.get())) } + } } )+ } |
