1 use core::num::{Saturating, Wrapping};
5 #[rustc_specialization_trait]
6 pub(super) unsafe trait IsZero {
7 /// Whether this value's representation is all zeros,
8 /// or can be represented with all zeroes.
9 fn is_zero(&self) -> bool;
12 macro_rules! impl_is_zero {
13 ($t:ty, $is_zero:expr) => {
14 unsafe impl IsZero for $t {
16 fn is_zero(&self) -> bool {
23 impl_is_zero!(i8, |x| x == 0); // It is needed to impl for arrays and tuples of i8.
24 impl_is_zero!(i16, |x| x == 0);
25 impl_is_zero!(i32, |x| x == 0);
26 impl_is_zero!(i64, |x| x == 0);
27 impl_is_zero!(i128, |x| x == 0);
28 impl_is_zero!(isize, |x| x == 0);
30 impl_is_zero!(u8, |x| x == 0); // It is needed to impl for arrays and tuples of u8.
31 impl_is_zero!(u16, |x| x == 0);
32 impl_is_zero!(u32, |x| x == 0);
33 impl_is_zero!(u64, |x| x == 0);
34 impl_is_zero!(u128, |x| x == 0);
35 impl_is_zero!(usize, |x| x == 0);
37 impl_is_zero!(bool, |x| x == false);
38 impl_is_zero!(char, |x| x == '\0');
40 impl_is_zero!(f32, |x: f32| x.to_bits() == 0);
41 impl_is_zero!(f64, |x: f64| x.to_bits() == 0);
43 unsafe impl<T> IsZero for *const T {
45 fn is_zero(&self) -> bool {
50 unsafe impl<T> IsZero for *mut T {
52 fn is_zero(&self) -> bool {
57 unsafe impl<T: IsZero, const N: usize> IsZero for [T; N] {
59 fn is_zero(&self) -> bool {
60 // Because this is generated as a runtime check, it's not obvious that
61 // it's worth doing if the array is really long. The threshold here
62 // is largely arbitrary, but was picked because as of 2022-07-01 LLVM
63 // fails to const-fold the check in `vec![[1; 32]; n]`
64 // See https://github.com/rust-lang/rust/pull/97581#issuecomment-1166628022
65 // Feel free to tweak if you have better evidence.
67 N <= 16 && self.iter().all(IsZero::is_zero)
71 // This is recursive macro.
72 macro_rules! impl_for_tuples {
75 // No use for implementing for empty tuple because it is ZST.
77 ($first_arg:ident $(,$rest:ident)*) => {
78 unsafe impl <$first_arg: IsZero, $($rest: IsZero,)*> IsZero for ($first_arg, $($rest,)*){
80 fn is_zero(&self) -> bool{
81 // Destructure tuple to N references
82 // Rust allows to hide generic params by local variable names.
83 #[allow(non_snake_case)]
84 let ($first_arg, $($rest,)*) = self;
87 $( && $rest.is_zero() )*
91 impl_for_tuples!($($rest),*);
95 impl_for_tuples!(A, B, C, D, E, F, G, H);
97 // `Option<&T>` and `Option<Box<T>>` are guaranteed to represent `None` as null.
98 // For fat pointers, the bytes that would be the pointer metadata in the `Some`
99 // variant are padding in the `None` variant, so ignoring them and
100 // zero-initializing instead is ok.
101 // `Option<&mut T>` never implements `Clone`, so there's no need for an impl of
104 unsafe impl<T: ?Sized> IsZero for Option<&T> {
106 fn is_zero(&self) -> bool {
111 unsafe impl<T: ?Sized> IsZero for Option<Box<T>> {
113 fn is_zero(&self) -> bool {
118 // `Option<num::NonZeroU32>` and similar have a representation guarantee that
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.
121 // While the documentation officially makes it UB to transmute from `None`,
122 // we're the standard library so we can make extra inferences, and we know that
123 // the only niche available to represent `None` is the one that's all zeros.
125 macro_rules! impl_is_zero_option_of_nonzero {
126 ($($t:ident,)+) => {$(
127 unsafe impl IsZero for Option<core::num::$t> {
129 fn is_zero(&self) -> bool {
136 impl_is_zero_option_of_nonzero!(
151 macro_rules! impl_is_zero_option_of_num {
153 unsafe impl IsZero for Option<$t> {
155 fn is_zero(&self) -> bool {
157 let none: Self = unsafe { core::mem::MaybeUninit::zeroed().assume_init() };
158 assert!(none.is_none());
166 impl_is_zero_option_of_num!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, usize, isize,);
168 unsafe impl<T: IsZero> IsZero for Wrapping<T> {
170 fn is_zero(&self) -> bool {
175 unsafe impl<T: IsZero> IsZero for Saturating<T> {
177 fn is_zero(&self) -> bool {
182 macro_rules! impl_for_optional_bool {
184 unsafe impl IsZero for $t {
186 fn is_zero(&self) -> bool {
187 // SAFETY: This is *not* a stable layout guarantee, but
188 // inside `core` we're allowed to rely on the current rustc
189 // behaviour that options of bools will be one byte with
190 // no padding, so long as they're nested less than 254 deep.
191 let raw: u8 = unsafe { core::mem::transmute(*self) };
197 impl_for_optional_bool! {
199 Option<Option<bool>>,
200 Option<Option<Option<bool>>>,
201 // Could go further, but not worth the metadata overhead