Rollup merge of #118326 - WaffleLapkin:nz_count_ones, r=scottmcm

Add `NonZero*::count_ones`

This PR adds the following APIs to the standard library:

```rust
impl NonZero* {
    pub const fn count_ones(self) -> NonZeroU32;
}
```

This is potentially interesting, given that `count_ones` can't ever return 0.

r? libs-api
This commit is contained in:
León Orell Valerian Liehr 2024-01-24 15:43:11 +01:00 committed by GitHub
commit 3529d45b74
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -288,6 +288,43 @@ pub const fn trailing_zeros(self) -> u32 {
unsafe { intrinsics::cttz_nonzero(self.get() as $UnsignedPrimitive) as u32 }
}
/// Returns the number of ones in the binary representation of `self`.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// #![feature(non_zero_count_ones)]
/// # fn main() { test().unwrap(); }
/// # fn test() -> Option<()> {
#[doc = concat!("# use std::num::{self, ", stringify!($Ty), "};")]
///
/// let one = num::NonZeroU32::new(1)?;
/// let three = num::NonZeroU32::new(3)?;
#[doc = concat!("let a = ", stringify!($Ty), "::new(0b100_0000)?;")]
#[doc = concat!("let b = ", stringify!($Ty), "::new(0b100_0011)?;")]
///
/// assert_eq!(a.count_ones(), one);
/// assert_eq!(b.count_ones(), three);
/// # Some(())
/// # }
/// ```
///
#[unstable(feature = "non_zero_count_ones", issue = "120287")]
#[rustc_const_unstable(feature = "non_zero_count_ones", issue = "120287")]
#[doc(alias = "popcount")]
#[doc(alias = "popcnt")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn count_ones(self) -> NonZeroU32 {
// SAFETY:
// `self` is non-zero, which means it has at least one bit set, which means
// that the result of `count_ones` is non-zero.
unsafe { NonZeroU32::new_unchecked(self.get().count_ones()) }
}
nonzero_integer_signedness_dependent_methods! {
Self = $Ty,
Primitive = $signedness $Int,