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 73252d5 commit d0afa6d
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 31 deletions.
32 changes: 16 additions & 16 deletions library/core/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ macro_rules! type_alias {
type_alias! { "c_char.md", c_char = c_char_definition::c_char, NonZero_c_char = c_char_definition::NonZero_c_char;
#[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 @@ -69,8 +69,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 @@ -152,11 +152,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 @@ -165,14 +165,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 @@ -181,15 +181,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
24 changes: 13 additions & 11 deletions library/core/src/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ pub use dec2flt::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 @@ -482,7 +484,7 @@ impl u8 {
Self = u8,
ActualT = u8,
SignedT = i8,
NonZeroT = NonZeroU8,
NonZeroT = NonZero<u8>,
BITS = 8,
MAX = 255,
rot = 2,
Expand Down Expand Up @@ -791,7 +793,7 @@ impl u8 {
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
#[inline]
pub const fn is_ascii_alphanumeric(&self) -> bool {
matches!(*self, b'0'..=b'9') | matches!(*self, b'A'..=b'Z') | matches!(*self, b'a'..=b'z')
matches!(*self, b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z')
}

/// Checks if the value is an ASCII decimal digit:
Expand Down Expand Up @@ -1097,7 +1099,7 @@ impl u16 {
Self = u16,
ActualT = u16,
SignedT = i16,
NonZeroT = NonZeroU16,
NonZeroT = NonZero<u16>,
BITS = 16,
MAX = 65535,
rot = 4,
Expand Down Expand Up @@ -1146,7 +1148,7 @@ impl u32 {
Self = u32,
ActualT = u32,
SignedT = i32,
NonZeroT = NonZeroU32,
NonZeroT = NonZero<u32>,
BITS = 32,
MAX = 4294967295,
rot = 8,
Expand All @@ -1170,7 +1172,7 @@ impl u64 {
Self = u64,
ActualT = u64,
SignedT = i64,
NonZeroT = NonZeroU64,
NonZeroT = NonZero<u64>,
BITS = 64,
MAX = 18446744073709551615,
rot = 12,
Expand All @@ -1194,7 +1196,7 @@ impl u128 {
Self = u128,
ActualT = u128,
SignedT = i128,
NonZeroT = NonZeroU128,
NonZeroT = NonZero<u128>,
BITS = 128,
MAX = 340282366920938463463374607431768211455,
rot = 16,
Expand All @@ -1204,9 +1206,9 @@ impl u128 {
swapped = "0x12907856341290785634129078563412",
reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
le_bytes = "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]",
0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]",
to_xe_bytes_doc = "",
from_xe_bytes_doc = "",
bound_condition = "",
Expand All @@ -1220,7 +1222,7 @@ impl usize {
Self = usize,
ActualT = u16,
SignedT = isize,
NonZeroT = NonZeroUsize,
NonZeroT = NonZero<usize>,
BITS = 16,
MAX = 65535,
rot = 4,
Expand All @@ -1245,7 +1247,7 @@ impl usize {
Self = usize,
ActualT = u32,
SignedT = isize,
NonZeroT = NonZeroUsize,
NonZeroT = NonZero<usize>,
BITS = 32,
MAX = 4294967295,
rot = 8,
Expand All @@ -1270,7 +1272,7 @@ impl usize {
Self = usize,
ActualT = u64,
SignedT = isize,
NonZeroT = NonZeroUsize,
NonZeroT = NonZero<usize>,
BITS = 64,
MAX = 18446744073709551615,
rot = 12,
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
Expand Up @@ -3,7 +3,7 @@ macro_rules! uint_impl {
Self = $SelfT:ty,
ActualT = $ActualT:ident,
SignedT = $SignedT:ident,
NonZeroT = $NonZeroT:ident,
NonZeroT = $NonZeroT:ty,

// There are all for use *only* in doc comments.
// As such, they're all passed as literals -- passing them as a string
Expand Down Expand Up @@ -842,6 +842,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 @@ -864,6 +865,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 @@ -12,7 +12,7 @@ use crate::iter::{
};
use crate::marker::PhantomData;
use crate::mem::{self, SizedTypeProperties};
use crate::num::NonZeroUsize;
use crate::num::{NonZero, NonZeroUsize};
use crate::ptr::{self, invalid, invalid_mut, NonNull};

use super::{from_raw_parts, from_raw_parts_mut};
Expand Down Expand Up @@ -1305,12 +1305,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 d0afa6d

Please sign in to comment.