3
3
use crate :: cmp:: Ordering ;
4
4
use crate :: fmt;
5
5
use crate :: hash:: { Hash , Hasher } ;
6
+ #[ cfg( bootstrap) ]
7
+ use crate :: marker:: StructuralEq ;
6
8
use crate :: marker:: StructuralPartialEq ;
7
9
use crate :: ops:: { BitOr , BitOrAssign , Div , Neg , Rem } ;
8
10
use crate :: str:: FromStr ;
@@ -30,9 +32,7 @@ mod private {
30
32
issue = "none"
31
33
) ]
32
34
#[ const_trait]
33
- pub trait ZeroablePrimitive : Sized + Copy + private:: Sealed {
34
- type NonZero ;
35
- }
35
+ pub trait ZeroablePrimitive : Sized + Copy + private:: Sealed { }
36
36
37
37
macro_rules! impl_zeroable_primitive {
38
38
( $NonZero: ident ( $primitive: ty ) ) => {
@@ -48,9 +48,7 @@ macro_rules! impl_zeroable_primitive {
48
48
reason = "implementation detail which may disappear or be replaced at any time" ,
49
49
issue = "none"
50
50
) ]
51
- impl const ZeroablePrimitive for $primitive {
52
- type NonZero = $NonZero;
53
- }
51
+ impl const ZeroablePrimitive for $primitive { }
54
52
} ;
55
53
}
56
54
@@ -67,12 +65,23 @@ impl_zeroable_primitive!(NonZeroI64(i64));
67
65
impl_zeroable_primitive ! ( NonZeroI128 ( i128 ) ) ;
68
66
impl_zeroable_primitive ! ( NonZeroIsize ( isize ) ) ;
69
67
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 ) ;
76
85
77
86
macro_rules! impl_nonzero_fmt {
78
87
( #[ $stability: meta] ( $( $Trait: ident ) ,+ ) for $Ty: ident ) => {
@@ -131,12 +140,7 @@ macro_rules! nonzero_integer {
131
140
///
132
141
/// [null pointer optimization]: crate::option#representation
133
142
#[ $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>;
140
144
141
145
impl $Ty {
142
146
/// Creates a non-zero without checking whether the value is non-zero.
@@ -543,6 +547,9 @@ macro_rules! nonzero_integer {
543
547
}
544
548
}
545
549
550
+ #[ $stability]
551
+ impl Copy for $Ty { }
552
+
546
553
#[ $stability]
547
554
impl PartialEq for $Ty {
548
555
#[ inline]
@@ -559,6 +566,13 @@ macro_rules! nonzero_integer {
559
566
#[ unstable( feature = "structural_match" , issue = "31434" ) ]
560
567
impl StructuralPartialEq for $Ty { }
561
568
569
+ #[ $stability]
570
+ impl Eq for $Ty { }
571
+
572
+ #[ unstable( feature = "structural_match" , issue = "31434" ) ]
573
+ #[ cfg( bootstrap) ]
574
+ impl StructuralEq for $Ty { }
575
+
562
576
#[ $stability]
563
577
impl PartialOrd for $Ty {
564
578
#[ inline]
0 commit comments