]> git.lizzy.rs Git - rust.git/commitdiff
Implement `BITS` constant for non-zero integers
authorNikolai Vazquez <hello@nikolaivazquez.com>
Sat, 12 Mar 2022 13:00:45 +0000 (08:00 -0500)
committerNikolai Vazquez <hello@nikolaivazquez.com>
Sat, 12 Mar 2022 13:00:45 +0000 (08:00 -0500)
library/core/src/num/nonzero.rs

index 5bdd78aa2dea4d9808041546c80d7786750b6eb8..05e53175873d47d9c45a22ae942d9ce1834d48d6 100644 (file)
@@ -1090,3 +1090,41 @@ impl $Ty {
     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);
+}