diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2022-03-13 20:01:58 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-13 20:01:58 +0100 |
| commit | 2f9bc56e5a21534326f4483f3f6381175ada2cea (patch) | |
| tree | 7b441a7c0eb6e03d628d6258b700bc938e7fbc9a | |
| parent | ebed06fcba3b58913a5087039a81478d43b47b2f (diff) | |
| parent | 6b5acf0d4063fc726f68fcd444140ffd576b7304 (diff) | |
| download | rust-2f9bc56e5a21534326f4483f3f6381175ada2cea.tar.gz rust-2f9bc56e5a21534326f4483f3f6381175ada2cea.zip | |
Rollup merge of #93292 - nvzqz:nonzero-bits, r=dtolnay
Implement `BITS` constant for non-zero integers
This adds the associated [`BITS`](https://doc.rust-lang.org/stable/std/primitive.usize.html#associatedconstant.BITS) constant to `NonZero{U,I}{8,16,32,64,128,size}`.
This is useful when a type alias refers to either a regular or non-zero integer.
| -rw-r--r-- | library/core/src/num/nonzero.rs | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index 5bdd78aa2de..c3652931478 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -465,7 +465,7 @@ macro_rules! nonzero_unsigned_operations { without modifying the original"] #[inline] pub const fn log2(self) -> u32 { - <$Int>::BITS - 1 - self.leading_zeros() + Self::BITS - 1 - self.leading_zeros() } /// Returns the base 10 logarithm of the number, rounded down. @@ -1090,3 +1090,41 @@ nonzero_min_max_signed! { NonZeroI128(i128); NonZeroIsize(isize); } + +macro_rules! nonzero_bits { + ( $( $Ty: ident($Int: ty); )+ ) => { + $( + impl $Ty { + /// The size of this non-zero integer type in bits. + /// + #[doc = concat!("This value is equal to [`", stringify!($Int), "::BITS`].")] + /// + /// # Examples + /// + /// ``` + /// #![feature(nonzero_bits)] + #[doc = concat!("# use std::num::", stringify!($Ty), ";")] + /// + #[doc = concat!("assert_eq!(", stringify!($Ty), "::BITS, ", stringify!($Int), "::BITS);")] + /// ``` + #[unstable(feature = "nonzero_bits", issue = "94881")] + pub const BITS: u32 = <$Int>::BITS; + } + )+ + } +} + +nonzero_bits! { + NonZeroU8(u8); + NonZeroI8(i8); + NonZeroU16(u16); + NonZeroI16(i16); + NonZeroU32(u32); + NonZeroI32(i32); + NonZeroU64(u64); + NonZeroI64(i64); + NonZeroU128(u128); + NonZeroI128(i128); + NonZeroUsize(usize); + NonZeroIsize(isize); +} |
