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,24 +67,40 @@ 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) => {
79
90
#[ $stability]
80
91
impl Clone for $Ty {
81
92
#[ inline]
82
93
fn clone( & self ) -> Self {
94
+ let value = self . 0 ;
95
+
83
96
// SAFETY: The contained value is non-zero.
84
- unsafe { Self ( self . 0 ) }
97
+ unsafe { Self ( value ) }
85
98
}
86
99
}
87
100
101
+ #[ $stability]
102
+ impl Copy for $Ty { }
103
+
88
104
#[ $stability]
89
105
impl PartialEq for $Ty {
90
106
#[ inline]
@@ -101,6 +117,12 @@ macro_rules! impl_nonzero_traits {
101
117
#[ unstable( feature = "structural_match" , issue = "31434" ) ]
102
118
impl StructuralPartialEq for $Ty { }
103
119
120
+ #[ $stability]
121
+ impl Eq for $Ty { }
122
+
123
+ #[ unstable( feature = "structural_match" , issue = "31434" ) ]
124
+ impl StructuralEq for $Ty { }
125
+
104
126
#[ $stability]
105
127
impl PartialOrd for $Ty {
106
128
#[ inline]
@@ -225,12 +247,7 @@ macro_rules! nonzero_integer {
225
247
///
226
248
/// [null pointer optimization]: crate::option#representation
227
249
#[ $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) ;
250
+ pub type $Ty = NonZero <$Int>;
234
251
235
252
impl_nonzero_traits!( #[ $stability] $Ty) ;
236
253
0 commit comments