Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 7e0b9a8

Browse files
committed
NonZero saturating_mul.
1 parent ac3eb90 commit 7e0b9a8

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

library/core/src/num/nonzero.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,36 @@ macro_rules! nonzero_unsigned_signed_operations {
679679
None
680680
}
681681
}
682+
683+
/// Multiply two non-zero integers together.
684+
#[doc = concat!("Return [`", stringify!($Int), "::MAX`] on overflow.")]
685+
///
686+
/// # Examples
687+
///
688+
/// ```
689+
/// #![feature(nonzero_ops)]
690+
/// # #![feature(try_trait)]
691+
#[doc = concat!("# use std::num::", stringify!($Ty), ";")]
692+
///
693+
/// # fn main() -> Result<(), std::option::NoneError> {
694+
#[doc = concat!("let two = ", stringify!($Ty), "::new(2)?;")]
695+
#[doc = concat!("let four = ", stringify!($Ty), "::new(4)?;")]
696+
#[doc = concat!("let max = ", stringify!($Ty), "::new(",
697+
stringify!($Int), "::MAX)?;")]
698+
///
699+
/// assert_eq!(four, two.saturating_mul(two));
700+
/// assert_eq!(max, four.saturating_mul(max));
701+
/// # Ok(())
702+
/// # }
703+
/// ```
704+
#[unstable(feature = "nonzero_ops", issue = "84186")]
705+
#[inline]
706+
pub const fn saturating_mul(self, other: $Ty) -> $Ty {
707+
// SAFETY: saturating_mul returns u*::MAX on overflow
708+
// and `other` is also non-null
709+
// so the result cannot be zero.
710+
unsafe { $Ty::new_unchecked(self.get().saturating_mul(other.get())) }
711+
}
682712
}
683713
)+
684714
}

0 commit comments

Comments
 (0)