diff options
| author | Markus Reiter <me@reitermark.us> | 2024-01-20 18:02:56 +0100 |
|---|---|---|
| committer | Markus Reiter <me@reitermark.us> | 2024-01-27 16:38:57 +0100 |
| commit | 4f0ce6fca2426427aab225cf1138a9171edb63b8 (patch) | |
| tree | bb3c6aeaf77b3c6d98e708eb5d14e1a4afe5672a | |
| parent | 8af70c7a189195363d1786fcb6578451f890db81 (diff) | |
| download | rust-4f0ce6fca2426427aab225cf1138a9171edb63b8.tar.gz rust-4f0ce6fca2426427aab225cf1138a9171edb63b8.zip | |
Switch `NonZero` alias direction.
| -rw-r--r-- | library/core/src/num/mod.rs | 10 | ||||
| -rw-r--r-- | library/core/src/num/nonzero.rs | 50 | ||||
| -rw-r--r-- | library/std/src/lib.rs | 1 | ||||
| -rw-r--r-- | library/std/src/num.rs | 10 |
4 files changed, 52 insertions, 19 deletions
diff --git a/library/core/src/num/mod.rs b/library/core/src/num/mod.rs index c2b646b9b3e..03c977abbbb 100644 --- a/library/core/src/num/mod.rs +++ b/library/core/src/num/mod.rs @@ -61,7 +61,15 @@ pub use dec2flt::ParseFloatError; #[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}; diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index 45ac544ceaa..1124719fc8d 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -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 @@ mod private { 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 @@ macro_rules! impl_zeroable_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_zeroable_primitive!(NonZeroI64(i64)); 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. @@ -544,6 +548,9 @@ macro_rules! nonzero_integer { } #[$stability] + impl Copy for $Ty {} + + #[$stability] impl PartialEq for $Ty { #[inline] fn eq(&self, other: &Self) -> bool { @@ -560,6 +567,13 @@ macro_rules! nonzero_integer { 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] fn partial_cmp(&self, other: &Self) -> Option<Ordering> { diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index 751e988a99b..2e458562744 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -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)] diff --git a/library/std/src/num.rs b/library/std/src/num.rs index 55f6ddcf77f..1343fdfd1df 100644 --- a/library/std/src/num.rs +++ b/library/std/src/num.rs @@ -16,6 +16,16 @@ pub use core::num::Wrapping; #[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")] |
