1- use core:: num:: { Saturating , Wrapping } ;
1+ use core:: num:: { NonZero , Saturating , Wrapping } ;
22
33use crate :: boxed:: Box ;
44
@@ -69,7 +69,7 @@ unsafe impl<T: IsZero, const N: usize> IsZero for [T; N] {
6969}
7070
7171// This is recursive macro.
72- macro_rules! impl_for_tuples {
72+ macro_rules! impl_is_zero_tuples {
7373 // Stopper
7474 ( ) => {
7575 // No use for implementing for empty tuple because it is ZST.
@@ -88,11 +88,11 @@ macro_rules! impl_for_tuples {
8888 }
8989 }
9090
91- impl_for_tuples !( $( $rest) ,* ) ;
91+ impl_is_zero_tuples !( $( $rest) ,* ) ;
9292 }
9393}
9494
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 ) ;
9696
9797// `Option<&T>` and `Option<Box<T>>` are guaranteed to represent `None` as null.
9898// 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>> {
115115 }
116116}
117117
118- // `Option<num::NonZeroU32 >` and similar have a representation guarantee that
118+ // `Option<NonZero<u32> >` and similar have a representation guarantee that
119119// 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.
121121// While the documentation officially makes it UB to transmute from `None`,
122122// we're the standard library so we can make extra inferences, and we know that
123123// 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>> {
128127 #[ inline]
129128 fn is_zero( & self ) -> bool {
130129 self . is_none( )
@@ -133,23 +132,10 @@ macro_rules! impl_is_zero_option_of_nonzero {
133132 ) +} ;
134133}
135134
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) ,+ $( , ) ?) => { $(
153139 unsafe impl IsZero for Option <$t> {
154140 #[ inline]
155141 fn is_zero( & self ) -> bool {
@@ -163,7 +149,7 @@ macro_rules! impl_is_zero_option_of_num {
163149 ) +} ;
164150}
165151
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 ) ;
167153
168154unsafe impl < T : IsZero > IsZero for Wrapping < T > {
169155 #[ inline]
@@ -179,8 +165,8 @@ unsafe impl<T: IsZero> IsZero for Saturating<T> {
179165 }
180166}
181167
182- macro_rules! impl_for_optional_bool {
183- ( $( $t: ty, ) + ) => { $(
168+ macro_rules! impl_is_zero_option_of_bool {
169+ ( $( $t: ty) ,+ $ ( , ) ? ) => { $(
184170 unsafe impl IsZero for $t {
185171 #[ inline]
186172 fn is_zero( & self ) -> bool {
@@ -194,9 +180,10 @@ macro_rules! impl_for_optional_bool {
194180 }
195181 ) +} ;
196182}
197- impl_for_optional_bool ! {
183+
184+ impl_is_zero_option_of_bool ! {
198185 Option <bool >,
199186 Option <Option <bool >>,
200187 Option <Option <Option <bool >>>,
201- // Could go further, but not worth the metadata overhead
188+ // Could go further, but not worth the metadata overhead.
202189}
0 commit comments