Rollup merge of #119990 - reitermarkus:nonzero-type-alias, r=dtolnay
Add private `NonZero<T>` type alias. According to step 2 suggested in https://github.com/rust-lang/rust/pull/100428#pullrequestreview-1767139731. This adds a private type alias for `NonZero<T>` so that some parts of the code can already start using `NonZero<T>` syntax. Using `NonZero<T>` for `convert` and other parts which implement `From` doesn't work while it is a type alias, since this results in conflicting implementations.
This commit is contained in:
commit
ac3108f208
@ -59,6 +59,8 @@ pub use dec2flt::ParseFloatError;
|
|||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub use error::ParseIntError;
|
pub use error::ParseIntError;
|
||||||
|
|
||||||
|
pub(crate) use nonzero::NonZero;
|
||||||
|
|
||||||
#[stable(feature = "nonzero", since = "1.28.0")]
|
#[stable(feature = "nonzero", since = "1.28.0")]
|
||||||
pub use nonzero::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};
|
pub use nonzero::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};
|
||||||
|
|
||||||
@ -482,7 +484,7 @@ impl u8 {
|
|||||||
Self = u8,
|
Self = u8,
|
||||||
ActualT = u8,
|
ActualT = u8,
|
||||||
SignedT = i8,
|
SignedT = i8,
|
||||||
NonZeroT = NonZeroU8,
|
NonZeroT = NonZero<u8>,
|
||||||
BITS = 8,
|
BITS = 8,
|
||||||
MAX = 255,
|
MAX = 255,
|
||||||
rot = 2,
|
rot = 2,
|
||||||
@ -1097,7 +1099,7 @@ impl u16 {
|
|||||||
Self = u16,
|
Self = u16,
|
||||||
ActualT = u16,
|
ActualT = u16,
|
||||||
SignedT = i16,
|
SignedT = i16,
|
||||||
NonZeroT = NonZeroU16,
|
NonZeroT = NonZero<u16>,
|
||||||
BITS = 16,
|
BITS = 16,
|
||||||
MAX = 65535,
|
MAX = 65535,
|
||||||
rot = 4,
|
rot = 4,
|
||||||
@ -1146,7 +1148,7 @@ impl u32 {
|
|||||||
Self = u32,
|
Self = u32,
|
||||||
ActualT = u32,
|
ActualT = u32,
|
||||||
SignedT = i32,
|
SignedT = i32,
|
||||||
NonZeroT = NonZeroU32,
|
NonZeroT = NonZero<u32>,
|
||||||
BITS = 32,
|
BITS = 32,
|
||||||
MAX = 4294967295,
|
MAX = 4294967295,
|
||||||
rot = 8,
|
rot = 8,
|
||||||
@ -1170,7 +1172,7 @@ impl u64 {
|
|||||||
Self = u64,
|
Self = u64,
|
||||||
ActualT = u64,
|
ActualT = u64,
|
||||||
SignedT = i64,
|
SignedT = i64,
|
||||||
NonZeroT = NonZeroU64,
|
NonZeroT = NonZero<u64>,
|
||||||
BITS = 64,
|
BITS = 64,
|
||||||
MAX = 18446744073709551615,
|
MAX = 18446744073709551615,
|
||||||
rot = 12,
|
rot = 12,
|
||||||
@ -1194,7 +1196,7 @@ impl u128 {
|
|||||||
Self = u128,
|
Self = u128,
|
||||||
ActualT = u128,
|
ActualT = u128,
|
||||||
SignedT = i128,
|
SignedT = i128,
|
||||||
NonZeroT = NonZeroU128,
|
NonZeroT = NonZero<u128>,
|
||||||
BITS = 128,
|
BITS = 128,
|
||||||
MAX = 340282366920938463463374607431768211455,
|
MAX = 340282366920938463463374607431768211455,
|
||||||
rot = 16,
|
rot = 16,
|
||||||
@ -1220,7 +1222,7 @@ impl usize {
|
|||||||
Self = usize,
|
Self = usize,
|
||||||
ActualT = u16,
|
ActualT = u16,
|
||||||
SignedT = isize,
|
SignedT = isize,
|
||||||
NonZeroT = NonZeroUsize,
|
NonZeroT = NonZero<usize>,
|
||||||
BITS = 16,
|
BITS = 16,
|
||||||
MAX = 65535,
|
MAX = 65535,
|
||||||
rot = 4,
|
rot = 4,
|
||||||
@ -1245,7 +1247,7 @@ impl usize {
|
|||||||
Self = usize,
|
Self = usize,
|
||||||
ActualT = u32,
|
ActualT = u32,
|
||||||
SignedT = isize,
|
SignedT = isize,
|
||||||
NonZeroT = NonZeroUsize,
|
NonZeroT = NonZero<usize>,
|
||||||
BITS = 32,
|
BITS = 32,
|
||||||
MAX = 4294967295,
|
MAX = 4294967295,
|
||||||
rot = 8,
|
rot = 8,
|
||||||
@ -1270,7 +1272,7 @@ impl usize {
|
|||||||
Self = usize,
|
Self = usize,
|
||||||
ActualT = u64,
|
ActualT = u64,
|
||||||
SignedT = isize,
|
SignedT = isize,
|
||||||
NonZeroT = NonZeroUsize,
|
NonZeroT = NonZero<usize>,
|
||||||
BITS = 64,
|
BITS = 64,
|
||||||
MAX = 18446744073709551615,
|
MAX = 18446744073709551615,
|
||||||
rot = 12,
|
rot = 12,
|
||||||
|
@ -8,6 +8,69 @@ use super::from_str_radix;
|
|||||||
use super::{IntErrorKind, ParseIntError};
|
use super::{IntErrorKind, ParseIntError};
|
||||||
use crate::intrinsics;
|
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 {
|
macro_rules! impl_nonzero_fmt {
|
||||||
( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
|
( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
|
||||||
$(
|
$(
|
||||||
|
@ -3,7 +3,7 @@ macro_rules! uint_impl {
|
|||||||
Self = $SelfT:ty,
|
Self = $SelfT:ty,
|
||||||
ActualT = $ActualT:ident,
|
ActualT = $ActualT:ident,
|
||||||
SignedT = $SignedT:ident,
|
SignedT = $SignedT:ident,
|
||||||
NonZeroT = $NonZeroT:ident,
|
NonZeroT = $NonZeroT:ty,
|
||||||
|
|
||||||
// There are all for use *only* in doc comments.
|
// There are all for use *only* in doc comments.
|
||||||
// As such, they're all passed as literals -- passing them as a string
|
// As such, they're all passed as literals -- passing them as a string
|
||||||
@ -842,6 +842,7 @@ macro_rules! uint_impl {
|
|||||||
without modifying the original"]
|
without modifying the original"]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub const fn checked_ilog2(self) -> Option<u32> {
|
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) {
|
if let Some(x) = <$NonZeroT>::new(self) {
|
||||||
Some(x.ilog2())
|
Some(x.ilog2())
|
||||||
} else {
|
} else {
|
||||||
@ -864,6 +865,7 @@ macro_rules! uint_impl {
|
|||||||
without modifying the original"]
|
without modifying the original"]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub const fn checked_ilog10(self) -> Option<u32> {
|
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) {
|
if let Some(x) = <$NonZeroT>::new(self) {
|
||||||
Some(x.ilog10())
|
Some(x.ilog10())
|
||||||
} else {
|
} else {
|
||||||
|
@ -12,7 +12,7 @@ use crate::iter::{
|
|||||||
};
|
};
|
||||||
use crate::marker::PhantomData;
|
use crate::marker::PhantomData;
|
||||||
use crate::mem::{self, SizedTypeProperties};
|
use crate::mem::{self, SizedTypeProperties};
|
||||||
use crate::num::NonZeroUsize;
|
use crate::num::{NonZero, NonZeroUsize};
|
||||||
use crate::ptr::{self, invalid, invalid_mut, NonNull};
|
use crate::ptr::{self, invalid, invalid_mut, NonNull};
|
||||||
|
|
||||||
use super::{from_raw_parts, from_raw_parts_mut};
|
use super::{from_raw_parts, from_raw_parts_mut};
|
||||||
@ -1305,12 +1305,12 @@ forward_iterator! { RSplitNMut: T, &'a mut [T] }
|
|||||||
#[must_use = "iterators are lazy and do nothing unless consumed"]
|
#[must_use = "iterators are lazy and do nothing unless consumed"]
|
||||||
pub struct Windows<'a, T: 'a> {
|
pub struct Windows<'a, T: 'a> {
|
||||||
v: &'a [T],
|
v: &'a [T],
|
||||||
size: NonZeroUsize,
|
size: NonZero<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T: 'a> Windows<'a, T> {
|
impl<'a, T: 'a> Windows<'a, T> {
|
||||||
#[inline]
|
#[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 }
|
Self { v: slice, size }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user