33use crate :: cmp:: Ordering ;
44use crate :: fmt;
55use crate :: hash:: { Hash , Hasher } ;
6- use crate :: marker:: StructuralPartialEq ;
6+ use crate :: marker:: { StructuralEq , StructuralPartialEq } ;
77use crate :: ops:: { BitOr , BitOrAssign , Div , Neg , Rem } ;
88use crate :: str:: FromStr ;
99
@@ -67,12 +67,23 @@ impl_zeroable_primitive!(NonZeroI64(i64));
6767impl_zeroable_primitive ! ( NonZeroI128 ( i128 ) ) ;
6868impl_zeroable_primitive ! ( NonZeroIsize ( isize ) ) ;
6969
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 ;
70+ /// A value that is known not to equal zero.
71+ ///
72+ /// This enables some memory layout optimization.
73+ /// For example, `Option<NonZero<u32>>` is the same size as `u32`:
74+ ///
75+ /// ```rust
76+ /// #![feature(generic_nonzero)]
77+ ///
78+ /// use core::mem::size_of;
79+ /// assert_eq!(size_of::<Option<core::num::NonZero<u32>>>(), size_of::<u32>());
80+ /// ```
81+ #[ unstable( feature = "generic_nonzero" , issue = "82363" ) ]
82+ #[ repr( transparent) ]
83+ #[ rustc_layout_scalar_valid_range_start( 1 ) ]
84+ #[ rustc_nonnull_optimization_guaranteed]
85+ #[ rustc_diagnostic_item = "NonZero" ]
86+ pub struct NonZero < T : ZeroablePrimitive > ( T ) ;
7687
7788macro_rules! impl_nonzero_traits {
7889 ( #[ $stability: meta] $Ty: ty) => {
@@ -85,6 +96,9 @@ macro_rules! impl_nonzero_traits {
8596 }
8697 }
8798
99+ #[ $stability]
100+ impl Copy for $Ty { }
101+
88102 #[ $stability]
89103 impl PartialEq for $Ty {
90104 #[ inline]
@@ -101,6 +115,12 @@ macro_rules! impl_nonzero_traits {
101115 #[ unstable( feature = "structural_match" , issue = "31434" ) ]
102116 impl StructuralPartialEq for $Ty { }
103117
118+ #[ $stability]
119+ impl Eq for $Ty { }
120+
121+ #[ unstable( feature = "structural_match" , issue = "31434" ) ]
122+ impl StructuralEq for $Ty { }
123+
104124 #[ $stability]
105125 impl PartialOrd for $Ty {
106126 #[ inline]
@@ -225,12 +245,7 @@ macro_rules! nonzero_integer {
225245 ///
226246 /// [null pointer optimization]: crate::option#representation
227247 #[ $stability]
228- #[ derive( Copy , Eq ) ]
229- #[ repr( transparent) ]
230- #[ rustc_layout_scalar_valid_range_start( 1 ) ]
231- #[ rustc_nonnull_optimization_guaranteed]
232- #[ rustc_diagnostic_item = stringify!( $Ty) ]
233- pub struct $Ty( $Int) ;
248+ pub type $Ty = NonZero <$Int>;
234249
235250 impl_nonzero_traits!( #[ $stability] $Ty) ;
236251
0 commit comments