Skip to content

Commit

Permalink
Add private NonZero<T> type alias.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Jan 15, 2024
1 parent e6ce562 commit 988772d
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 29 deletions.
3 changes: 2 additions & 1 deletion library/core/src/convert/num.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
32 changes: 16 additions & 16 deletions library/core/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i8>; }
type_alias! { "c_uchar.md", c_uchar = u8, NonZero_c_uchar = NonZero<u8>; }
type_alias! { "c_short.md", c_short = i16, NonZero_c_short = NonZero<i16>; }
type_alias! { "c_ushort.md", c_ushort = u16, NonZero_c_ushort = NonZero<u16>; }

type_alias! { "c_int.md", c_int = c_int_definition::c_int, NonZero_c_int = c_int_definition::NonZero_c_int;
#[doc(cfg(all()))] }
Expand 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<i64>; }
type_alias! { "c_ulonglong.md", c_ulonglong = u64, NonZero_c_ulonglong = NonZero<u64>; }

type_alias_no_nz! { "c_float.md", c_float = f32; }
type_alias_no_nz! { "c_double.md", c_double = f64; }
Expand Down Expand Up @@ -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<u8>;
} 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<i8>;
}
}
}
Expand All @@ -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<i16>;
pub type c_uint = u16;
pub type NonZero_c_uint = crate::num::NonZeroU16;
pub type NonZero_c_uint = crate::num::NonZero<u16>;
} else {
pub type c_int = i32;
pub type NonZero_c_int = crate::num::NonZeroI32;
pub type NonZero_c_int = crate::num::NonZero<i32>;
pub type c_uint = u32;
pub type NonZero_c_uint = crate::num::NonZeroU32;
pub type NonZero_c_uint = crate::num::NonZero<u32>;
}
}
}
Expand All @@ -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<i64>;
pub type c_ulong = u64;
pub type NonZero_c_ulong = crate::num::NonZeroU64;
pub type NonZero_c_ulong = crate::num::NonZero<u64>;
} 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<i32>;
pub type c_ulong = u32;
pub type NonZero_c_ulong = crate::num::NonZeroU32;
pub type NonZero_c_ulong = crate::num::NonZero<u32>;
}
}
}
Expand Down
18 changes: 10 additions & 8 deletions library/core/src/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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<u8>, 8, 255, 2, "0x82", "0xa", "0x12", "0x12", "0x48", "[0x12]",
"[0x12]", "", "", "" }
widening_impl! { u8, u16, 8, unsigned }

Expand Down Expand Up @@ -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<u16>, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
"[0x34, 0x12]", "[0x12, 0x34]", "", "", "" }
widening_impl! { u16, u32, 16, unsigned }

Expand Down Expand Up @@ -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<u32>, 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<u64>, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa",
"0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
"[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
Expand All @@ -930,7 +932,7 @@ impl u64 {
}

impl u128 {
uint_impl! { u128, u128, i128, NonZeroU128, 128, 340282366920938463463374607431768211455, 16,
uint_impl! { u128, u128, i128, NonZero<u128>, 128, 340282366920938463463374607431768211455, 16,
"0x13f40000000000000000000000004f76", "0x4f7613f4", "0x12345678901234567890123456789012",
"0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48",
"[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
Expand All @@ -942,15 +944,15 @@ 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<usize>, 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" }
widening_impl! { usize, u32, 16, unsigned }
}
#[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<usize>, 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" }
Expand All @@ -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<usize>, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa",
"0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
"[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
Expand Down
63 changes: 63 additions & 0 deletions library/core/src/num/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>`](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<T> = <T as ZeroablePrimitive>::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 ) => {
$(
Expand Down
4 changes: 3 additions & 1 deletion library/core/src/num/uint_macros.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -808,6 +808,7 @@ macro_rules! uint_impl {
without modifying the original"]
#[inline]
pub const fn checked_ilog2(self) -> Option<u32> {
// FIXME: Simply use `NonZero::new` once it is actually generic.
if let Some(x) = <$NonZeroT>::new(self) {
Some(x.ilog2())
} else {
Expand All @@ -830,6 +831,7 @@ macro_rules! uint_impl {
without modifying the original"]
#[inline]
pub const fn checked_ilog10(self) -> Option<u32> {
// FIXME: Simply use `NonZero::new` once it is actually generic.
if let Some(x) = <$NonZeroT>::new(self) {
Some(x.ilog10())
} else {
Expand Down
6 changes: 3 additions & 3 deletions library/core/src/slice/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<usize>,
}

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<usize>) -> Self {
Self { v: slice, size }
}
}
Expand Down

0 comments on commit 988772d

Please sign in to comment.