Switch NonZero alias direction.

This commit is contained in:
Markus Reiter 2024-01-20 18:02:56 +01:00
parent 8af70c7a18
commit 4f0ce6fca2
No known key found for this signature in database
GPG Key ID: 245293B51702655B
4 changed files with 52 additions and 19 deletions

View File

@ -61,7 +61,15 @@ macro_rules! unlikely {
#[stable(feature = "rust1", since = "1.0.0")]
pub use error::ParseIntError;
pub(crate) use nonzero::NonZero;
#[unstable(
feature = "nonzero_internals",
reason = "implementation detail which may disappear or be replaced at any time",
issue = "none"
)]
pub use nonzero::ZeroablePrimitive;
#[unstable(feature = "generic_nonzero", issue = "120257")]
pub use nonzero::NonZero;
#[stable(feature = "nonzero", since = "1.28.0")]
pub use nonzero::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};

View File

@ -3,6 +3,8 @@
use crate::cmp::Ordering;
use crate::fmt;
use crate::hash::{Hash, Hasher};
#[cfg(bootstrap)]
use crate::marker::StructuralEq;
use crate::marker::StructuralPartialEq;
use crate::ops::{BitOr, BitOrAssign, Div, Neg, Rem};
use crate::str::FromStr;
@ -30,9 +32,7 @@ pub trait Sealed {}
issue = "none"
)]
#[const_trait]
pub trait ZeroablePrimitive: Sized + Copy + private::Sealed {
type NonZero;
}
pub trait ZeroablePrimitive: Sized + Copy + private::Sealed {}
macro_rules! impl_zeroable_primitive {
($NonZero:ident ( $primitive:ty )) => {
@ -48,9 +48,7 @@ impl const private::Sealed for $primitive {}
reason = "implementation detail which may disappear or be replaced at any time",
issue = "none"
)]
impl const ZeroablePrimitive for $primitive {
type NonZero = $NonZero;
}
impl const ZeroablePrimitive for $primitive {}
};
}
@ -67,12 +65,23 @@ impl const ZeroablePrimitive for $primitive {
impl_zeroable_primitive!(NonZeroI128(i128));
impl_zeroable_primitive!(NonZeroIsize(isize));
#[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;
/// A value that is known not to equal zero.
///
/// This enables some memory layout optimization.
/// For example, `Option<NonZero<u32>>` is the same size as `u32`:
///
/// ```
/// #![feature(generic_nonzero)]
/// use core::mem::size_of;
///
/// assert_eq!(size_of::<Option<core::num::NonZero<u32>>>(), size_of::<u32>());
/// ```
#[unstable(feature = "generic_nonzero", issue = "120257")]
#[repr(transparent)]
#[rustc_layout_scalar_valid_range_start(1)]
#[rustc_nonnull_optimization_guaranteed]
#[rustc_diagnostic_item = "NonZero"]
pub struct NonZero<T: ZeroablePrimitive>(T);
macro_rules! impl_nonzero_fmt {
( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
@ -131,12 +140,7 @@ macro_rules! nonzero_integer {
///
/// [null pointer optimization]: crate::option#representation
#[$stability]
#[derive(Copy, Eq)]
#[repr(transparent)]
#[rustc_layout_scalar_valid_range_start(1)]
#[rustc_nonnull_optimization_guaranteed]
#[rustc_diagnostic_item = stringify!($Ty)]
pub struct $Ty($Int);
pub type $Ty = NonZero<$Int>;
impl $Ty {
/// Creates a non-zero without checking whether the value is non-zero.
@ -543,6 +547,9 @@ fn clone(&self) -> Self {
}
}
#[$stability]
impl Copy for $Ty {}
#[$stability]
impl PartialEq for $Ty {
#[inline]
@ -559,6 +566,13 @@ fn ne(&self, other: &Self) -> bool {
#[unstable(feature = "structural_match", issue = "31434")]
impl StructuralPartialEq for $Ty {}
#[$stability]
impl Eq for $Ty {}
#[unstable(feature = "structural_match", issue = "31434")]
#[cfg(bootstrap)]
impl StructuralEq for $Ty {}
#[$stability]
impl PartialOrd for $Ty {
#[inline]

View File

@ -323,6 +323,7 @@
#![feature(float_gamma)]
#![feature(float_minimum_maximum)]
#![feature(float_next_up_down)]
#![feature(generic_nonzero)]
#![feature(hasher_prefixfree_extras)]
#![feature(hashmap_internals)]
#![feature(hint_assert_unchecked)]

View File

@ -16,6 +16,16 @@
#[stable(feature = "rust1", since = "1.0.0")]
pub use core::num::{FpCategory, ParseFloatError, ParseIntError, TryFromIntError};
#[unstable(
feature = "nonzero_internals",
reason = "implementation detail which may disappear or be replaced at any time",
issue = "none"
)]
pub use core::num::ZeroablePrimitive;
#[unstable(feature = "generic_nonzero", issue = "120257")]
pub use core::num::NonZero;
#[stable(feature = "signed_nonzero", since = "1.34.0")]
pub use core::num::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize};
#[stable(feature = "nonzero", since = "1.28.0")]