From 36d194f5612852a6d4fcf9d5fae8e0fba942adfa Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Mon, 19 Feb 2024 05:51:10 +0100 Subject: [PATCH] Use generic `NonZero` everywhere in `alloc`. --- library/alloc/src/ffi/c_str.rs | 18 ++++++------ library/alloc/src/vec/is_zero.rs | 49 ++++++++++++-------------------- 2 files changed, 27 insertions(+), 40 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> for CString { } #[stable(feature = "cstring_from_vec_of_nonzerou8", since = "1.43.0")] -impl From> for CString { - /// Converts a [Vec]<[NonZeroU8]> into a [`CString`] without +impl From>> for CString { + /// Converts a [Vec]<[NonZero]<[u8]>> into a [`CString`] without /// copying nor checking for inner nul bytes. #[inline] - fn from(v: Vec) -> CString { + fn from(v: Vec>) -> CString { unsafe { - // Transmute `Vec` to `Vec`. + // Transmute `Vec>` to `Vec`. let v: Vec = { // SAFETY: - // - transmuting between `NonZeroU8` and `u8` is sound; - // - `alloc::Layout == alloc::Layout`. - let (ptr, len, cap): (*mut NonZeroU8, _, _) = Vec::into_raw_parts(v); + // - transmuting between `NonZero` and `u8` is sound; + // - `alloc::Layout> == alloc::Layout`. + let (ptr, len, cap): (*mut NonZero, _, _) = Vec::into_raw_parts(v); Vec::from_raw_parts(ptr.cast::(), len, cap) }; // SAFETY: `v` cannot contain nul bytes, given the type-level - // invariant of `NonZeroU8`. + // invariant of `NonZero`. 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 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>` 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 IsZero for Option> { } } -// `Option` and similar have a representation guarantee that +// `Option>` 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` works. +// that transmuting between `NonZero` and `Option>` 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 { +macro_rules! impl_is_zero_option_of_nonzero_int { + ($($t:ty),+ $(,)?) => {$( + unsafe impl IsZero for Option> { #[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, -); +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_num { - ($($t:ty,)+) => {$( +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 IsZero for Wrapping { #[inline] @@ -179,8 +165,8 @@ unsafe impl IsZero for Saturating { } } -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, Option>, Option>>, - // Could go further, but not worth the metadata overhead + // Could go further, but not worth the metadata overhead. }