Skip to content

Commit 4f0ce6f

Browse files
committed
Switch NonZero alias direction.
1 parent 8af70c7 commit 4f0ce6f

File tree

4 files changed

+52
-19
lines changed

4 files changed

+52
-19
lines changed

library/core/src/num/mod.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,15 @@ pub use dec2flt::ParseFloatError;
6161
#[stable(feature = "rust1", since = "1.0.0")]
6262
pub use error::ParseIntError;
6363

64-
pub(crate) use nonzero::NonZero;
64+
#[unstable(
65+
feature = "nonzero_internals",
66+
reason = "implementation detail which may disappear or be replaced at any time",
67+
issue = "none"
68+
)]
69+
pub use nonzero::ZeroablePrimitive;
70+
71+
#[unstable(feature = "generic_nonzero", issue = "120257")]
72+
pub use nonzero::NonZero;
6573

6674
#[stable(feature = "nonzero", since = "1.28.0")]
6775
pub use nonzero::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};

library/core/src/num/nonzero.rs

+32-18
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use crate::cmp::Ordering;
44
use crate::fmt;
55
use crate::hash::{Hash, Hasher};
6+
#[cfg(bootstrap)]
7+
use crate::marker::StructuralEq;
68
use crate::marker::StructuralPartialEq;
79
use crate::ops::{BitOr, BitOrAssign, Div, Neg, Rem};
810
use crate::str::FromStr;
@@ -30,9 +32,7 @@ mod private {
3032
issue = "none"
3133
)]
3234
#[const_trait]
33-
pub trait ZeroablePrimitive: Sized + Copy + private::Sealed {
34-
type NonZero;
35-
}
35+
pub trait ZeroablePrimitive: Sized + Copy + private::Sealed {}
3636

3737
macro_rules! impl_zeroable_primitive {
3838
($NonZero:ident ( $primitive:ty )) => {
@@ -48,9 +48,7 @@ macro_rules! impl_zeroable_primitive {
4848
reason = "implementation detail which may disappear or be replaced at any time",
4949
issue = "none"
5050
)]
51-
impl const ZeroablePrimitive for $primitive {
52-
type NonZero = $NonZero;
53-
}
51+
impl const ZeroablePrimitive for $primitive {}
5452
};
5553
}
5654

@@ -67,12 +65,23 @@ impl_zeroable_primitive!(NonZeroI64(i64));
6765
impl_zeroable_primitive!(NonZeroI128(i128));
6866
impl_zeroable_primitive!(NonZeroIsize(isize));
6967

70-
#[unstable(
71-
feature = "nonzero_internals",
72-
reason = "implementation detail which may disappear or be replaced at any time",
73-
issue = "none"
74-
)]
75-
pub(crate) type NonZero<T> = <T as ZeroablePrimitive>::NonZero;
68+
/// A value that is known not to equal zero.
69+
///
70+
/// This enables some memory layout optimization.
71+
/// For example, `Option<NonZero<u32>>` is the same size as `u32`:
72+
///
73+
/// ```
74+
/// #![feature(generic_nonzero)]
75+
/// use core::mem::size_of;
76+
///
77+
/// assert_eq!(size_of::<Option<core::num::NonZero<u32>>>(), size_of::<u32>());
78+
/// ```
79+
#[unstable(feature = "generic_nonzero", issue = "120257")]
80+
#[repr(transparent)]
81+
#[rustc_layout_scalar_valid_range_start(1)]
82+
#[rustc_nonnull_optimization_guaranteed]
83+
#[rustc_diagnostic_item = "NonZero"]
84+
pub struct NonZero<T: ZeroablePrimitive>(T);
7685

7786
macro_rules! impl_nonzero_fmt {
7887
( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
@@ -131,12 +140,7 @@ macro_rules! nonzero_integer {
131140
///
132141
/// [null pointer optimization]: crate::option#representation
133142
#[$stability]
134-
#[derive(Copy, Eq)]
135-
#[repr(transparent)]
136-
#[rustc_layout_scalar_valid_range_start(1)]
137-
#[rustc_nonnull_optimization_guaranteed]
138-
#[rustc_diagnostic_item = stringify!($Ty)]
139-
pub struct $Ty($Int);
143+
pub type $Ty = NonZero<$Int>;
140144

141145
impl $Ty {
142146
/// Creates a non-zero without checking whether the value is non-zero.
@@ -543,6 +547,9 @@ macro_rules! nonzero_integer {
543547
}
544548
}
545549

550+
#[$stability]
551+
impl Copy for $Ty {}
552+
546553
#[$stability]
547554
impl PartialEq for $Ty {
548555
#[inline]
@@ -559,6 +566,13 @@ macro_rules! nonzero_integer {
559566
#[unstable(feature = "structural_match", issue = "31434")]
560567
impl StructuralPartialEq for $Ty {}
561568

569+
#[$stability]
570+
impl Eq for $Ty {}
571+
572+
#[unstable(feature = "structural_match", issue = "31434")]
573+
#[cfg(bootstrap)]
574+
impl StructuralEq for $Ty {}
575+
562576
#[$stability]
563577
impl PartialOrd for $Ty {
564578
#[inline]

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@
323323
#![feature(float_gamma)]
324324
#![feature(float_minimum_maximum)]
325325
#![feature(float_next_up_down)]
326+
#![feature(generic_nonzero)]
326327
#![feature(hasher_prefixfree_extras)]
327328
#![feature(hashmap_internals)]
328329
#![feature(hint_assert_unchecked)]

library/std/src/num.rs

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ pub use core::num::Wrapping;
1616
#[stable(feature = "rust1", since = "1.0.0")]
1717
pub use core::num::{FpCategory, ParseFloatError, ParseIntError, TryFromIntError};
1818

19+
#[unstable(
20+
feature = "nonzero_internals",
21+
reason = "implementation detail which may disappear or be replaced at any time",
22+
issue = "none"
23+
)]
24+
pub use core::num::ZeroablePrimitive;
25+
26+
#[unstable(feature = "generic_nonzero", issue = "120257")]
27+
pub use core::num::NonZero;
28+
1929
#[stable(feature = "signed_nonzero", since = "1.34.0")]
2030
pub use core::num::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize};
2131
#[stable(feature = "nonzero", since = "1.28.0")]

0 commit comments

Comments
 (0)