1
- use core:: num:: { Saturating , Wrapping } ;
1
+ use core:: num:: { NonZero , Saturating , Wrapping } ;
2
2
3
3
use crate :: boxed:: Box ;
4
4
@@ -69,7 +69,7 @@ unsafe impl<T: IsZero, const N: usize> IsZero for [T; N] {
69
69
}
70
70
71
71
// This is recursive macro.
72
- macro_rules! impl_for_tuples {
72
+ macro_rules! impl_is_zero_tuples {
73
73
// Stopper
74
74
( ) => {
75
75
// No use for implementing for empty tuple because it is ZST.
@@ -88,11 +88,11 @@ macro_rules! impl_for_tuples {
88
88
}
89
89
}
90
90
91
- impl_for_tuples !( $( $rest) ,* ) ;
91
+ impl_is_zero_tuples !( $( $rest) ,* ) ;
92
92
}
93
93
}
94
94
95
- impl_for_tuples ! ( A , B , C , D , E , F , G , H ) ;
95
+ impl_is_zero_tuples ! ( A , B , C , D , E , F , G , H ) ;
96
96
97
97
// `Option<&T>` and `Option<Box<T>>` are guaranteed to represent `None` as null.
98
98
// For fat pointers, the bytes that would be the pointer metadata in the `Some`
@@ -115,16 +115,15 @@ unsafe impl<T: ?Sized> IsZero for Option<Box<T>> {
115
115
}
116
116
}
117
117
118
- // `Option<num::NonZeroU32 >` and similar have a representation guarantee that
118
+ // `Option<NonZero<u32> >` and similar have a representation guarantee that
119
119
// they're the same size as the corresponding `u32` type, as well as a guarantee
120
- // that transmuting between `NonZeroU32 ` and `Option<num::NonZeroU32 >` works.
120
+ // that transmuting between `NonZero<u32> ` and `Option<NonZero<u32> >` works.
121
121
// While the documentation officially makes it UB to transmute from `None`,
122
122
// we're the standard library so we can make extra inferences, and we know that
123
123
// the only niche available to represent `None` is the one that's all zeros.
124
-
125
- macro_rules! impl_is_zero_option_of_nonzero {
126
- ( $( $t: ident, ) +) => { $(
127
- unsafe impl IsZero for Option <core:: num:: $t> {
124
+ macro_rules! impl_is_zero_option_of_nonzero_int {
125
+ ( $( $t: ty) ,+ $( , ) ?) => { $(
126
+ unsafe impl IsZero for Option <NonZero <$t>> {
128
127
#[ inline]
129
128
fn is_zero( & self ) -> bool {
130
129
self . is_none( )
@@ -133,23 +132,10 @@ macro_rules! impl_is_zero_option_of_nonzero {
133
132
) +} ;
134
133
}
135
134
136
- impl_is_zero_option_of_nonzero ! (
137
- NonZeroU8 ,
138
- NonZeroU16 ,
139
- NonZeroU32 ,
140
- NonZeroU64 ,
141
- NonZeroU128 ,
142
- NonZeroI8 ,
143
- NonZeroI16 ,
144
- NonZeroI32 ,
145
- NonZeroI64 ,
146
- NonZeroI128 ,
147
- NonZeroUsize ,
148
- NonZeroIsize ,
149
- ) ;
150
-
151
- macro_rules! impl_is_zero_option_of_num {
152
- ( $( $t: ty, ) +) => { $(
135
+ impl_is_zero_option_of_nonzero_int ! ( u8 , u16 , u32 , u64 , u128 , usize , i8 , i16 , i32 , i64 , i128 , isize ) ;
136
+
137
+ macro_rules! impl_is_zero_option_of_int {
138
+ ( $( $t: ty) ,+ $( , ) ?) => { $(
153
139
unsafe impl IsZero for Option <$t> {
154
140
#[ inline]
155
141
fn is_zero( & self ) -> bool {
@@ -163,7 +149,7 @@ macro_rules! impl_is_zero_option_of_num {
163
149
) +} ;
164
150
}
165
151
166
- impl_is_zero_option_of_num ! ( u8 , u16 , u32 , u64 , u128 , i8 , i16 , i32 , i64 , i128 , usize , isize , ) ;
152
+ impl_is_zero_option_of_int ! ( u8 , u16 , u32 , u64 , u128 , i8 , i16 , i32 , i64 , i128 , usize , isize ) ;
167
153
168
154
unsafe impl < T : IsZero > IsZero for Wrapping < T > {
169
155
#[ inline]
@@ -179,8 +165,8 @@ unsafe impl<T: IsZero> IsZero for Saturating<T> {
179
165
}
180
166
}
181
167
182
- macro_rules! impl_for_optional_bool {
183
- ( $( $t: ty, ) + ) => { $(
168
+ macro_rules! impl_is_zero_option_of_bool {
169
+ ( $( $t: ty) ,+ $ ( , ) ? ) => { $(
184
170
unsafe impl IsZero for $t {
185
171
#[ inline]
186
172
fn is_zero( & self ) -> bool {
@@ -194,9 +180,10 @@ macro_rules! impl_for_optional_bool {
194
180
}
195
181
) +} ;
196
182
}
197
- impl_for_optional_bool ! {
183
+
184
+ impl_is_zero_option_of_bool ! {
198
185
Option <bool >,
199
186
Option <Option <bool >>,
200
187
Option <Option <Option <bool >>>,
201
- // Could go further, but not worth the metadata overhead
188
+ // Could go further, but not worth the metadata overhead.
202
189
}
0 commit comments