Skip to content
/ rust Public
forked from rust-lang/rust

Commit 36d194f

Browse files
committed
Use generic NonZero everywhere in alloc.
1 parent e0732e4 commit 36d194f

File tree

2 files changed

+28
-41
lines changed

2 files changed

+28
-41
lines changed

library/alloc/src/ffi/c_str.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use core::borrow::Borrow;
1111
use core::ffi::{c_char, CStr};
1212
use core::fmt;
1313
use core::mem;
14-
use core::num::NonZeroU8;
14+
use core::num::NonZero;
1515
use core::ops;
1616
use core::ptr;
1717
use core::slice;
@@ -795,22 +795,22 @@ impl From<Box<CStr>> for CString {
795795
}
796796

797797
#[stable(feature = "cstring_from_vec_of_nonzerou8", since = "1.43.0")]
798-
impl From<Vec<NonZeroU8>> for CString {
799-
/// Converts a <code>[Vec]<[NonZeroU8]></code> into a [`CString`] without
798+
impl From<Vec<NonZero<u8>>> for CString {
799+
/// Converts a <code>[Vec]<[NonZero]<[u8]>></code> into a [`CString`] without
800800
/// copying nor checking for inner nul bytes.
801801
#[inline]
802-
fn from(v: Vec<NonZeroU8>) -> CString {
802+
fn from(v: Vec<NonZero<u8>>) -> CString {
803803
unsafe {
804-
// Transmute `Vec<NonZeroU8>` to `Vec<u8>`.
804+
// Transmute `Vec<NonZero<u8>>` to `Vec<u8>`.
805805
let v: Vec<u8> = {
806806
// SAFETY:
807-
// - transmuting between `NonZeroU8` and `u8` is sound;
808-
// - `alloc::Layout<NonZeroU8> == alloc::Layout<u8>`.
809-
let (ptr, len, cap): (*mut NonZeroU8, _, _) = Vec::into_raw_parts(v);
807+
// - transmuting between `NonZero<u8>` and `u8` is sound;
808+
// - `alloc::Layout<NonZero<u8>> == alloc::Layout<u8>`.
809+
let (ptr, len, cap): (*mut NonZero<u8>, _, _) = Vec::into_raw_parts(v);
810810
Vec::from_raw_parts(ptr.cast::<u8>(), len, cap)
811811
};
812812
// SAFETY: `v` cannot contain nul bytes, given the type-level
813-
// invariant of `NonZeroU8`.
813+
// invariant of `NonZero<u8>`.
814814
Self::_from_vec_unchecked(v)
815815
}
816816
}

library/alloc/src/vec/is_zero.rs

+19-32
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::num::{Saturating, Wrapping};
1+
use core::num::{NonZero, Saturating, Wrapping};
22

33
use crate::boxed::Box;
44

@@ -69,7 +69,7 @@ unsafe impl<T: IsZero, const N: usize> IsZero for [T; N] {
6969
}
7070

7171
// This is recursive macro.
72-
macro_rules! impl_for_tuples {
72+
macro_rules! impl_is_zero_tuples {
7373
// Stopper
7474
() => {
7575
// No use for implementing for empty tuple because it is ZST.
@@ -88,11 +88,11 @@ macro_rules! impl_for_tuples {
8888
}
8989
}
9090

91-
impl_for_tuples!($($rest),*);
91+
impl_is_zero_tuples!($($rest),*);
9292
}
9393
}
9494

95-
impl_for_tuples!(A, B, C, D, E, F, G, H);
95+
impl_is_zero_tuples!(A, B, C, D, E, F, G, H);
9696

9797
// `Option<&T>` and `Option<Box<T>>` are guaranteed to represent `None` as null.
9898
// 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>> {
115115
}
116116
}
117117

118-
// `Option<num::NonZeroU32>` and similar have a representation guarantee that
118+
// `Option<NonZero<u32>>` and similar have a representation guarantee that
119119
// they're the same size as the corresponding `u32` type, as well as a guarantee
120-
// that transmuting between `NonZeroU32` and `Option<num::NonZeroU32>` works.
120+
// that transmuting between `NonZero<u32>` and `Option<NonZero<u32>>` works.
121121
// While the documentation officially makes it UB to transmute from `None`,
122122
// we're the standard library so we can make extra inferences, and we know that
123123
// the only niche available to represent `None` is the one that's all zeros.
124-
125-
macro_rules! impl_is_zero_option_of_nonzero {
126-
($($t:ident,)+) => {$(
127-
unsafe impl IsZero for Option<core::num::$t> {
124+
macro_rules! impl_is_zero_option_of_nonzero_int {
125+
($($t:ty),+ $(,)?) => {$(
126+
unsafe impl IsZero for Option<NonZero<$t>> {
128127
#[inline]
129128
fn is_zero(&self) -> bool {
130129
self.is_none()
@@ -133,23 +132,10 @@ macro_rules! impl_is_zero_option_of_nonzero {
133132
)+};
134133
}
135134

136-
impl_is_zero_option_of_nonzero!(
137-
NonZeroU8,
138-
NonZeroU16,
139-
NonZeroU32,
140-
NonZeroU64,
141-
NonZeroU128,
142-
NonZeroI8,
143-
NonZeroI16,
144-
NonZeroI32,
145-
NonZeroI64,
146-
NonZeroI128,
147-
NonZeroUsize,
148-
NonZeroIsize,
149-
);
150-
151-
macro_rules! impl_is_zero_option_of_num {
152-
($($t:ty,)+) => {$(
135+
impl_is_zero_option_of_nonzero_int!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
136+
137+
macro_rules! impl_is_zero_option_of_int {
138+
($($t:ty),+ $(,)?) => {$(
153139
unsafe impl IsZero for Option<$t> {
154140
#[inline]
155141
fn is_zero(&self) -> bool {
@@ -163,7 +149,7 @@ macro_rules! impl_is_zero_option_of_num {
163149
)+};
164150
}
165151

166-
impl_is_zero_option_of_num!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, usize, isize,);
152+
impl_is_zero_option_of_int!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, usize, isize);
167153

168154
unsafe impl<T: IsZero> IsZero for Wrapping<T> {
169155
#[inline]
@@ -179,8 +165,8 @@ unsafe impl<T: IsZero> IsZero for Saturating<T> {
179165
}
180166
}
181167

182-
macro_rules! impl_for_optional_bool {
183-
($($t:ty,)+) => {$(
168+
macro_rules! impl_is_zero_option_of_bool {
169+
($($t:ty),+ $(,)?) => {$(
184170
unsafe impl IsZero for $t {
185171
#[inline]
186172
fn is_zero(&self) -> bool {
@@ -194,9 +180,10 @@ macro_rules! impl_for_optional_bool {
194180
}
195181
)+};
196182
}
197-
impl_for_optional_bool! {
183+
184+
impl_is_zero_option_of_bool! {
198185
Option<bool>,
199186
Option<Option<bool>>,
200187
Option<Option<Option<bool>>>,
201-
// Could go further, but not worth the metadata overhead
188+
// Could go further, but not worth the metadata overhead.
202189
}

0 commit comments

Comments
 (0)