File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -69,3 +69,36 @@ unsafe impl<T: ?Sized> IsZero for Option<Box<T>> {
6969 self . is_none ( )
7070 }
7171}
72+
73+ // `Option<num::NonZeroU32>` and similar have a representation guarantee that
74+ // they're the same size as the corresponding `u32` type, as well as a guarantee
75+ // that transmuting between `NonZeroU32` and `Option<num::NonZeroU32>` works.
76+ // While the documentation officially makes in UB to transmute from `None`,
77+ // we're the standard library so we can make extra inferences, and we know that
78+ // the only niche available to represent `None` is the one that's all zeros.
79+
80+ macro_rules! impl_is_zero_option_of_nonzero {
81+ ( $( $t: ident, ) +) => { $(
82+ unsafe impl IsZero for Option <core:: num:: $t> {
83+ #[ inline]
84+ fn is_zero( & self ) -> bool {
85+ self . is_none( )
86+ }
87+ }
88+ ) +} ;
89+ }
90+
91+ impl_is_zero_option_of_nonzero ! (
92+ NonZeroU8 ,
93+ NonZeroU16 ,
94+ NonZeroU32 ,
95+ NonZeroU64 ,
96+ NonZeroU128 ,
97+ NonZeroI8 ,
98+ NonZeroI16 ,
99+ NonZeroI32 ,
100+ NonZeroI64 ,
101+ NonZeroI128 ,
102+ NonZeroUsize ,
103+ NonZeroIsize ,
104+ ) ;
You can’t perform that action at this time.
0 commit comments