From 988772d217e222228dbf9815f4a33a4c51bf5ae2 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Mon, 15 Jan 2024 11:49:03 +0100 Subject: [PATCH] Add private `NonZero` type alias. --- library/core/src/convert/num.rs | 3 +- library/core/src/ffi/mod.rs | 32 +++++++-------- library/core/src/num/mod.rs | 18 +++++---- library/core/src/num/nonzero.rs | 63 +++++++++++++++++++++++++++++ library/core/src/num/uint_macros.rs | 4 +- library/core/src/slice/iter.rs | 6 +-- 6 files changed, 97 insertions(+), 29 deletions(-) diff --git a/library/core/src/convert/num.rs b/library/core/src/convert/num.rs index 4fa5d129bc6af..09870eb6bef5e 100644 --- a/library/core/src/convert/num.rs +++ b/library/core/src/convert/num.rs @@ -1,6 +1,7 @@ -use super::{From, TryFrom}; use crate::num::TryFromIntError; +use super::{From, TryFrom}; + mod private { /// This trait being unreachable from outside the crate /// prevents other implementations of the `FloatToInt` trait, diff --git a/library/core/src/ffi/mod.rs b/library/core/src/ffi/mod.rs index ec1eaa99f0b8e..76479e9ee9678 100644 --- a/library/core/src/ffi/mod.rs +++ b/library/core/src/ffi/mod.rs @@ -59,10 +59,10 @@ type_alias! { "c_char.md", c_char = c_char_definition::c_char, NonZero_c_char = #[cfg(all())] #[doc(cfg(all()))] } -type_alias! { "c_schar.md", c_schar = i8, NonZero_c_schar = NonZeroI8; } -type_alias! { "c_uchar.md", c_uchar = u8, NonZero_c_uchar = NonZeroU8; } -type_alias! { "c_short.md", c_short = i16, NonZero_c_short = NonZeroI16; } -type_alias! { "c_ushort.md", c_ushort = u16, NonZero_c_ushort = NonZeroU16; } +type_alias! { "c_schar.md", c_schar = i8, NonZero_c_schar = NonZero; } +type_alias! { "c_uchar.md", c_uchar = u8, NonZero_c_uchar = NonZero; } +type_alias! { "c_short.md", c_short = i16, NonZero_c_short = NonZero; } +type_alias! { "c_ushort.md", c_ushort = u16, NonZero_c_ushort = NonZero; } type_alias! { "c_int.md", c_int = c_int_definition::c_int, NonZero_c_int = c_int_definition::NonZero_c_int; #[doc(cfg(all()))] } @@ -74,8 +74,8 @@ type_alias! { "c_long.md", c_long = c_long_definition::c_long, NonZero_c_long = type_alias! { "c_ulong.md", c_ulong = c_long_definition::c_ulong, NonZero_c_ulong = c_long_definition::NonZero_c_ulong; #[doc(cfg(all()))] } -type_alias! { "c_longlong.md", c_longlong = i64, NonZero_c_longlong = NonZeroI64; } -type_alias! { "c_ulonglong.md", c_ulonglong = u64, NonZero_c_ulonglong = NonZeroU64; } +type_alias! { "c_longlong.md", c_longlong = i64, NonZero_c_longlong = NonZero; } +type_alias! { "c_ulonglong.md", c_ulonglong = u64, NonZero_c_ulonglong = NonZero; } type_alias_no_nz! { "c_float.md", c_float = f32; } type_alias_no_nz! { "c_double.md", c_double = f64; } @@ -147,11 +147,11 @@ mod c_char_definition { target_os = "horizon" ))] { pub type c_char = u8; - pub type NonZero_c_char = crate::num::NonZeroU8; + pub type NonZero_c_char = crate::num::NonZero; } else { // On every other target, c_char is signed. pub type c_char = i8; - pub type NonZero_c_char = crate::num::NonZeroI8; + pub type NonZero_c_char = crate::num::NonZero; } } } @@ -160,14 +160,14 @@ mod c_int_definition { cfg_if! { if #[cfg(any(target_arch = "avr", target_arch = "msp430"))] { pub type c_int = i16; - pub type NonZero_c_int = crate::num::NonZeroI16; + pub type NonZero_c_int = crate::num::NonZero; pub type c_uint = u16; - pub type NonZero_c_uint = crate::num::NonZeroU16; + pub type NonZero_c_uint = crate::num::NonZero; } else { pub type c_int = i32; - pub type NonZero_c_int = crate::num::NonZeroI32; + pub type NonZero_c_int = crate::num::NonZero; pub type c_uint = u32; - pub type NonZero_c_uint = crate::num::NonZeroU32; + pub type NonZero_c_uint = crate::num::NonZero; } } } @@ -176,15 +176,15 @@ mod c_long_definition { cfg_if! { if #[cfg(all(target_pointer_width = "64", not(windows)))] { pub type c_long = i64; - pub type NonZero_c_long = crate::num::NonZeroI64; + pub type NonZero_c_long = crate::num::NonZero; pub type c_ulong = u64; - pub type NonZero_c_ulong = crate::num::NonZeroU64; + pub type NonZero_c_ulong = crate::num::NonZero; } else { // The minimal size of `long` in the C standard is 32 bits pub type c_long = i32; - pub type NonZero_c_long = crate::num::NonZeroI32; + pub type NonZero_c_long = crate::num::NonZero; pub type c_ulong = u32; - pub type NonZero_c_ulong = crate::num::NonZeroU32; + pub type NonZero_c_ulong = crate::num::NonZero; } } } diff --git a/library/core/src/num/mod.rs b/library/core/src/num/mod.rs index 311c5fa5b6834..9a70469d90b28 100644 --- a/library/core/src/num/mod.rs +++ b/library/core/src/num/mod.rs @@ -70,6 +70,8 @@ impl Error for ParseFloatError { #[stable(feature = "rust1", since = "1.0.0")] pub use error::ParseIntError; +pub(crate) use nonzero::NonZero; + #[stable(feature = "nonzero", since = "1.28.0")] pub use nonzero::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize}; @@ -299,7 +301,7 @@ impl isize { const ASCII_CASE_MASK: u8 = 0b0010_0000; impl u8 { - uint_impl! { u8, u8, i8, NonZeroU8, 8, 255, 2, "0x82", "0xa", "0x12", "0x12", "0x48", "[0x12]", + uint_impl! { u8, u8, i8, NonZero, 8, 255, 2, "0x82", "0xa", "0x12", "0x12", "0x48", "[0x12]", "[0x12]", "", "", "" } widening_impl! { u8, u16, 8, unsigned } @@ -884,7 +886,7 @@ impl u8 { } impl u16 { - uint_impl! { u16, u16, i16, NonZeroU16, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48", + uint_impl! { u16, u16, i16, NonZero, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48", "[0x34, 0x12]", "[0x12, 0x34]", "", "", "" } widening_impl! { u16, u32, 16, unsigned } @@ -915,13 +917,13 @@ impl u16 { } impl u32 { - uint_impl! { u32, u32, i32, NonZeroU32, 32, 4294967295, 8, "0x10000b3", "0xb301", "0x12345678", + uint_impl! { u32, u32, i32, NonZero, 32, 4294967295, 8, "0x10000b3", "0xb301", "0x12345678", "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]", "", "", "" } widening_impl! { u32, u64, 32, unsigned } } impl u64 { - uint_impl! { u64, u64, i64, NonZeroU64, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa", + uint_impl! { u64, u64, i64, NonZero, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa", "0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48", "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]", @@ -930,7 +932,7 @@ impl u64 { } impl u128 { - uint_impl! { u128, u128, i128, NonZeroU128, 128, 340282366920938463463374607431768211455, 16, + uint_impl! { u128, u128, i128, NonZero, 128, 340282366920938463463374607431768211455, 16, "0x13f40000000000000000000000004f76", "0x4f7613f4", "0x12345678901234567890123456789012", "0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48", "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \ @@ -942,7 +944,7 @@ impl u128 { #[cfg(target_pointer_width = "16")] impl usize { - uint_impl! { usize, u16, isize, NonZeroUsize, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48", + uint_impl! { usize, u16, isize, NonZero, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48", "[0x34, 0x12]", "[0x12, 0x34]", usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!(), " on 16-bit targets" } @@ -950,7 +952,7 @@ impl usize { } #[cfg(target_pointer_width = "32")] impl usize { - uint_impl! { usize, u32, isize, NonZeroUsize, 32, 4294967295, 8, "0x10000b3", "0xb301", "0x12345678", + uint_impl! { usize, u32, isize, NonZero, 32, 4294967295, 8, "0x10000b3", "0xb301", "0x12345678", "0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]", usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!(), " on 32-bit targets" } @@ -959,7 +961,7 @@ impl usize { #[cfg(target_pointer_width = "64")] impl usize { - uint_impl! { usize, u64, isize, NonZeroUsize, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa", + uint_impl! { usize, u64, isize, NonZero, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa", "0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48", "[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]", diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index da402d66502a6..3b59095dfa7e2 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -8,6 +8,69 @@ use super::from_str_radix; use super::{IntErrorKind, ParseIntError}; use crate::intrinsics; +mod private { + #[unstable( + feature = "nonzero_internals", + reason = "implementation detail which may disappear or be replaced at any time", + issue = "none" + )] + #[const_trait] + pub trait Sealed {} +} + +/// A marker trait for primitive types which can be zero. +/// +/// This is an implementation detail for [`NonZero`](NonZero) which may disappear or be replaced at any time. +#[unstable( + feature = "nonzero_internals", + reason = "implementation detail which may disappear or be replaced at any time", + issue = "none" +)] +#[const_trait] +pub trait ZeroablePrimitive: Sized + Copy + private::Sealed { + type NonZero; +} + +#[unstable( + feature = "nonzero_internals", + reason = "implementation detail which may disappear or be replaced at any time", + issue = "none" +)] +pub(crate) type NonZero = ::NonZero; + +macro_rules! impl_zeroable_primitive { + ($NonZero:ident ( $primitive:ty )) => { + #[unstable( + feature = "nonzero_internals", + reason = "implementation detail which may disappear or be replaced at any time", + issue = "none" + )] + impl const private::Sealed for $primitive {} + + #[unstable( + feature = "nonzero_internals", + reason = "implementation detail which may disappear or be replaced at any time", + issue = "none" + )] + impl const ZeroablePrimitive for $primitive { + type NonZero = $NonZero; + } + }; +} + +impl_zeroable_primitive!(NonZeroU8(u8)); +impl_zeroable_primitive!(NonZeroU16(u16)); +impl_zeroable_primitive!(NonZeroU32(u32)); +impl_zeroable_primitive!(NonZeroU64(u64)); +impl_zeroable_primitive!(NonZeroU128(u128)); +impl_zeroable_primitive!(NonZeroUsize(usize)); +impl_zeroable_primitive!(NonZeroI8(i8)); +impl_zeroable_primitive!(NonZeroI16(i16)); +impl_zeroable_primitive!(NonZeroI32(i32)); +impl_zeroable_primitive!(NonZeroI64(i64)); +impl_zeroable_primitive!(NonZeroI128(i128)); +impl_zeroable_primitive!(NonZeroIsize(isize)); + macro_rules! impl_nonzero_fmt { ( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => { $( diff --git a/library/core/src/num/uint_macros.rs b/library/core/src/num/uint_macros.rs index d921ff9ba1026..87ea98be62dda 100644 --- a/library/core/src/num/uint_macros.rs +++ b/library/core/src/num/uint_macros.rs @@ -1,5 +1,5 @@ macro_rules! uint_impl { - ($SelfT:ty, $ActualT:ident, $SignedT:ident, $NonZeroT:ident, + ($SelfT:ty, $ActualT:ident, $SignedT:ident, $NonZeroT:ty, $BITS:expr, $MaxV:expr, $rot:expr, $rot_op:expr, $rot_result:expr, $swap_op:expr, $swapped:expr, $reversed:expr, $le_bytes:expr, $be_bytes:expr, @@ -808,6 +808,7 @@ macro_rules! uint_impl { without modifying the original"] #[inline] pub const fn checked_ilog2(self) -> Option { + // FIXME: Simply use `NonZero::new` once it is actually generic. if let Some(x) = <$NonZeroT>::new(self) { Some(x.ilog2()) } else { @@ -830,6 +831,7 @@ macro_rules! uint_impl { without modifying the original"] #[inline] pub const fn checked_ilog10(self) -> Option { + // FIXME: Simply use `NonZero::new` once it is actually generic. if let Some(x) = <$NonZeroT>::new(self) { Some(x.ilog10()) } else { diff --git a/library/core/src/slice/iter.rs b/library/core/src/slice/iter.rs index 8a8962828e980..48e2ac7b06ea8 100644 --- a/library/core/src/slice/iter.rs +++ b/library/core/src/slice/iter.rs @@ -10,7 +10,7 @@ use crate::intrinsics::{assume, exact_div, unchecked_sub}; use crate::iter::{FusedIterator, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce}; use crate::marker::{PhantomData, Send, Sized, Sync}; use crate::mem::{self, SizedTypeProperties}; -use crate::num::NonZeroUsize; +use crate::num::NonZero; use crate::ptr::NonNull; use super::{from_raw_parts, from_raw_parts_mut}; @@ -1297,12 +1297,12 @@ forward_iterator! { RSplitNMut: T, &'a mut [T] } #[must_use = "iterators are lazy and do nothing unless consumed"] pub struct Windows<'a, T: 'a> { v: &'a [T], - size: NonZeroUsize, + size: NonZero, } impl<'a, T: 'a> Windows<'a, T> { #[inline] - pub(super) fn new(slice: &'a [T], size: NonZeroUsize) -> Self { + pub(super) fn new(slice: &'a [T], size: NonZero) -> Self { Self { v: slice, size } } }