Skip to content

Commit 6979bb4

Browse files
committed
NonZero unchecked_mul.
1 parent 7e0b9a8 commit 6979bb4

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

library/core/src/num/nonzero.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,34 @@ macro_rules! nonzero_unsigned_signed_operations {
709709
// so the result cannot be zero.
710710
unsafe { $Ty::new_unchecked(self.get().saturating_mul(other.get())) }
711711
}
712+
713+
/// Multiply two non-zero integers together,
714+
/// assuming overflow cannot occur.
715+
/// This results in undefined behavior when
716+
#[doc = concat!("`self * rhs > ", stringify!($Int), "::MAX`, ")]
717+
#[doc = concat!("or `self * rhs < ", stringify!($Int), "::MIN`.")]
718+
///
719+
/// # Examples
720+
///
721+
/// ```
722+
/// #![feature(nonzero_ops)]
723+
/// # #![feature(try_trait)]
724+
#[doc = concat!("# use std::num::", stringify!($Ty), ";")]
725+
///
726+
/// # fn main() -> Result<(), std::option::NoneError> {
727+
#[doc = concat!("let two = ", stringify!($Ty), "::new(2)?;")]
728+
#[doc = concat!("let four = ", stringify!($Ty), "::new(4)?;")]
729+
///
730+
/// assert_eq!(four, unsafe { two.unchecked_mul(two) });
731+
/// # Ok(())
732+
/// # }
733+
/// ```
734+
#[unstable(feature = "nonzero_ops", issue = "84186")]
735+
#[inline]
736+
pub unsafe fn unchecked_mul(self, other: $Ty) -> $Ty {
737+
// SAFETY: The caller ensures there is no overflow.
738+
unsafe { $Ty::new_unchecked(self.get().unchecked_mul(other.get())) }
739+
}
712740
}
713741
)+
714742
}

0 commit comments

Comments
 (0)