3
3
use crate :: cmp:: Ordering ;
4
4
use crate :: fmt;
5
5
use crate :: hash:: { Hash , Hasher } ;
6
- use crate :: marker:: StructuralPartialEq ;
6
+ use crate :: marker:: { StructuralEq , StructuralPartialEq } ;
7
7
use crate :: ops:: { BitOr , BitOrAssign , Div , Neg , Rem } ;
8
8
use crate :: str:: FromStr ;
9
9
@@ -67,12 +67,23 @@ impl_zeroable_primitive!(NonZeroI64(i64));
67
67
impl_zeroable_primitive ! ( NonZeroI128 ( i128 ) ) ;
68
68
impl_zeroable_primitive ! ( NonZeroIsize ( isize ) ) ;
69
69
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 ) ;
76
87
77
88
macro_rules! impl_nonzero_traits {
78
89
( #[ $stability: meta] $Ty: ty) => {
@@ -85,6 +96,9 @@ macro_rules! impl_nonzero_traits {
85
96
}
86
97
}
87
98
99
+ #[ $stability]
100
+ impl Copy for $Ty { }
101
+
88
102
#[ $stability]
89
103
impl PartialEq for $Ty {
90
104
#[ inline]
@@ -101,6 +115,12 @@ macro_rules! impl_nonzero_traits {
101
115
#[ unstable( feature = "structural_match" , issue = "31434" ) ]
102
116
impl StructuralPartialEq for $Ty { }
103
117
118
+ #[ $stability]
119
+ impl Eq for $Ty { }
120
+
121
+ #[ unstable( feature = "structural_match" , issue = "31434" ) ]
122
+ impl StructuralEq for $Ty { }
123
+
104
124
#[ $stability]
105
125
impl PartialOrd for $Ty {
106
126
#[ inline]
@@ -225,12 +245,7 @@ macro_rules! nonzero_integer {
225
245
///
226
246
/// [null pointer optimization]: crate::option#representation
227
247
#[ $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>;
234
249
235
250
impl_nonzero_traits!( #[ $stability] $Ty) ;
236
251
0 commit comments