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
@@ -30,9 +30,7 @@ mod private {
30
30
issue = "none"
31
31
) ]
32
32
#[ const_trait]
33
- pub trait ZeroablePrimitive : Sized + Copy + private:: Sealed {
34
- type NonZero ;
35
- }
33
+ pub trait ZeroablePrimitive : Sized + Copy + private:: Sealed { }
36
34
37
35
macro_rules! impl_zeroable_primitive {
38
36
( $NonZero: ident ( $primitive: ty ) ) => {
@@ -48,9 +46,7 @@ macro_rules! impl_zeroable_primitive {
48
46
reason = "implementation detail which may disappear or be replaced at any time" ,
49
47
issue = "none"
50
48
) ]
51
- impl const ZeroablePrimitive for $primitive {
52
- type NonZero = $NonZero;
53
- }
49
+ impl const ZeroablePrimitive for $primitive { }
54
50
} ;
55
51
}
56
52
@@ -67,12 +63,23 @@ impl_zeroable_primitive!(NonZeroI64(i64));
67
63
impl_zeroable_primitive ! ( NonZeroI128 ( i128 ) ) ;
68
64
impl_zeroable_primitive ! ( NonZeroIsize ( isize ) ) ;
69
65
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 ;
66
+ /// A value that is known not to equal zero.
67
+ ///
68
+ /// This enables some memory layout optimization.
69
+ /// For example, `Option<NonZero<u32>>` is the same size as `u32`:
70
+ ///
71
+ /// ```rust
72
+ /// #![feature(generic_nonzero)]
73
+ ///
74
+ /// use core::mem::size_of;
75
+ /// assert_eq!(size_of::<Option<core::num::NonZero<u32>>>(), size_of::<u32>());
76
+ /// ```
77
+ #[ unstable( feature = "generic_nonzero" , issue = "82363" ) ]
78
+ #[ repr( transparent) ]
79
+ #[ rustc_layout_scalar_valid_range_start( 1 ) ]
80
+ #[ rustc_nonnull_optimization_guaranteed]
81
+ #[ rustc_diagnostic_item = "NonZero" ]
82
+ pub struct NonZero < T : ZeroablePrimitive > ( T ) ;
76
83
77
84
macro_rules! impl_nonzero_traits {
78
85
( #[ $stability: meta] $Ty: ty) => {
@@ -85,6 +92,9 @@ macro_rules! impl_nonzero_traits {
85
92
}
86
93
}
87
94
95
+ #[ $stability]
96
+ impl Copy for $Ty { }
97
+
88
98
#[ $stability]
89
99
impl PartialEq for $Ty {
90
100
#[ inline]
@@ -101,6 +111,12 @@ macro_rules! impl_nonzero_traits {
101
111
#[ unstable( feature = "structural_match" , issue = "31434" ) ]
102
112
impl StructuralPartialEq for $Ty { }
103
113
114
+ #[ $stability]
115
+ impl Eq for $Ty { }
116
+
117
+ #[ unstable( feature = "structural_match" , issue = "31434" ) ]
118
+ impl StructuralEq for $Ty { }
119
+
104
120
#[ $stability]
105
121
impl PartialOrd for $Ty {
106
122
#[ inline]
@@ -225,12 +241,7 @@ macro_rules! nonzero_integer {
225
241
///
226
242
/// [null pointer optimization]: crate::option#representation
227
243
#[ $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) ;
244
+ pub type $Ty = NonZero <$Int>;
234
245
235
246
impl_nonzero_traits!( #[ $stability] $Ty) ;
236
247
0 commit comments