diff options
| author | Markus Reiter <me@reitermark.us> | 2024-02-19 05:51:10 +0100 |
|---|---|---|
| committer | Markus Reiter <me@reitermark.us> | 2024-02-22 15:17:34 +0100 |
| commit | 36d194f5612852a6d4fcf9d5fae8e0fba942adfa (patch) | |
| tree | 624cb2f8904421793e067d0682c3096df93866d9 | |
| parent | e0732e42d829cd8aa8480811d0c860387437e6ca (diff) | |
| download | rust-36d194f5612852a6d4fcf9d5fae8e0fba942adfa.tar.gz rust-36d194f5612852a6d4fcf9d5fae8e0fba942adfa.zip | |
Use generic `NonZero` everywhere in `alloc`.
| -rw-r--r-- | library/alloc/src/ffi/c_str.rs | 18 | ||||
| -rw-r--r-- | library/alloc/src/vec/is_zero.rs | 51 |
2 files changed, 28 insertions, 41 deletions
diff --git a/library/alloc/src/ffi/c_str.rs b/library/alloc/src/ffi/c_str.rs index 9419b0cfb24..61ec04a4849 100644 --- a/library/alloc/src/ffi/c_str.rs +++ b/library/alloc/src/ffi/c_str.rs @@ -11,7 +11,7 @@ use core::borrow::Borrow; use core::ffi::{c_char, CStr}; use core::fmt; use core::mem; -use core::num::NonZeroU8; +use core::num::NonZero; use core::ops; use core::ptr; use core::slice; @@ -795,22 +795,22 @@ impl From<Box<CStr>> for CString { } #[stable(feature = "cstring_from_vec_of_nonzerou8", since = "1.43.0")] -impl From<Vec<NonZeroU8>> for CString { - /// Converts a <code>[Vec]<[NonZeroU8]></code> into a [`CString`] without +impl From<Vec<NonZero<u8>>> for CString { + /// Converts a <code>[Vec]<[NonZero]<[u8]>></code> into a [`CString`] without /// copying nor checking for inner nul bytes. #[inline] - fn from(v: Vec<NonZeroU8>) -> CString { + fn from(v: Vec<NonZero<u8>>) -> CString { unsafe { - // Transmute `Vec<NonZeroU8>` to `Vec<u8>`. + // Transmute `Vec<NonZero<u8>>` to `Vec<u8>`. let v: Vec<u8> = { // SAFETY: - // - transmuting between `NonZeroU8` and `u8` is sound; - // - `alloc::Layout<NonZeroU8> == alloc::Layout<u8>`. - let (ptr, len, cap): (*mut NonZeroU8, _, _) = Vec::into_raw_parts(v); + // - transmuting between `NonZero<u8>` and `u8` is sound; + // - `alloc::Layout<NonZero<u8>> == alloc::Layout<u8>`. + let (ptr, len, cap): (*mut NonZero<u8>, _, _) = Vec::into_raw_parts(v); Vec::from_raw_parts(ptr.cast::<u8>(), len, cap) }; // SAFETY: `v` cannot contain nul bytes, given the type-level - // invariant of `NonZeroU8`. + // invariant of `NonZero<u8>`. Self::_from_vec_unchecked(v) } } diff --git a/library/alloc/src/vec/is_zero.rs b/library/alloc/src/vec/is_zero.rs index cb9adf05c25..bcc5bf4d65b 100644 --- a/library/alloc/src/vec/is_zero.rs +++ b/library/alloc/src/vec/is_zero.rs @@ -1,4 +1,4 @@ -use core::num::{Saturating, Wrapping}; +use core::num::{NonZero, Saturating, Wrapping}; use crate::boxed::Box; @@ -69,7 +69,7 @@ unsafe impl<T: IsZero, const N: usize> IsZero for [T; N] { } // This is recursive macro. -macro_rules! impl_for_tuples { +macro_rules! impl_is_zero_tuples { // Stopper () => { // No use for implementing for empty tuple because it is ZST. @@ -88,11 +88,11 @@ macro_rules! impl_for_tuples { } } - impl_for_tuples!($($rest),*); + impl_is_zero_tuples!($($rest),*); } } -impl_for_tuples!(A, B, C, D, E, F, G, H); +impl_is_zero_tuples!(A, B, C, D, E, F, G, H); // `Option<&T>` and `Option<Box<T>>` are guaranteed to represent `None` as null. // For fat pointers, the bytes that would be the pointer metadata in the `Some` @@ -115,16 +115,15 @@ unsafe impl<T: ?Sized> IsZero for Option<Box<T>> { } } -// `Option<num::NonZeroU32>` and similar have a representation guarantee that +// `Option<NonZero<u32>>` and similar have a representation guarantee that // they're the same size as the corresponding `u32` type, as well as a guarantee -// that transmuting between `NonZeroU32` and `Option<num::NonZeroU32>` works. +// that transmuting between `NonZero<u32>` and `Option<NonZero<u32>>` works. // While the documentation officially makes it UB to transmute from `None`, // we're the standard library so we can make extra inferences, and we know that // the only niche available to represent `None` is the one that's all zeros. - -macro_rules! impl_is_zero_option_of_nonzero { - ($($t:ident,)+) => {$( - unsafe impl IsZero for Option<core::num::$t> { +macro_rules! impl_is_zero_option_of_nonzero_int { + ($($t:ty),+ $(,)?) => {$( + unsafe impl IsZero for Option<NonZero<$t>> { #[inline] fn is_zero(&self) -> bool { self.is_none() @@ -133,23 +132,10 @@ macro_rules! impl_is_zero_option_of_nonzero { )+}; } -impl_is_zero_option_of_nonzero!( - NonZeroU8, - NonZeroU16, - NonZeroU32, - NonZeroU64, - NonZeroU128, - NonZeroI8, - NonZeroI16, - NonZeroI32, - NonZeroI64, - NonZeroI128, - NonZeroUsize, - NonZeroIsize, -); - -macro_rules! impl_is_zero_option_of_num { - ($($t:ty,)+) => {$( +impl_is_zero_option_of_nonzero_int!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize); + +macro_rules! impl_is_zero_option_of_int { + ($($t:ty),+ $(,)?) => {$( unsafe impl IsZero for Option<$t> { #[inline] fn is_zero(&self) -> bool { @@ -163,7 +149,7 @@ macro_rules! impl_is_zero_option_of_num { )+}; } -impl_is_zero_option_of_num!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, usize, isize,); +impl_is_zero_option_of_int!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, usize, isize); unsafe impl<T: IsZero> IsZero for Wrapping<T> { #[inline] @@ -179,8 +165,8 @@ unsafe impl<T: IsZero> IsZero for Saturating<T> { } } -macro_rules! impl_for_optional_bool { - ($($t:ty,)+) => {$( +macro_rules! impl_is_zero_option_of_bool { + ($($t:ty),+ $(,)?) => {$( unsafe impl IsZero for $t { #[inline] fn is_zero(&self) -> bool { @@ -194,9 +180,10 @@ macro_rules! impl_for_optional_bool { } )+}; } -impl_for_optional_bool! { + +impl_is_zero_option_of_bool! { Option<bool>, Option<Option<bool>>, Option<Option<Option<bool>>>, - // Could go further, but not worth the metadata overhead + // Could go further, but not worth the metadata overhead. } |
