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 fn is_zero(&self) -> bool;
11 macro_rules! impl_is_zero {
12 ($t:ty, $is_zero:expr) => {
13 unsafe impl IsZero for $t {
15 fn is_zero(&self) -> bool {
22 impl_is_zero!(i8, |x| x == 0); // It is needed to impl for arrays and tuples of i8.
23 impl_is_zero!(i16, |x| x == 0);
24 impl_is_zero!(i32, |x| x == 0);
25 impl_is_zero!(i64, |x| x == 0);
26 impl_is_zero!(i128, |x| x == 0);
27 impl_is_zero!(isize, |x| x == 0);
29 impl_is_zero!(u8, |x| x == 0); // It is needed to impl for arrays and tuples of u8.
30 impl_is_zero!(u16, |x| x == 0);
31 impl_is_zero!(u32, |x| x == 0);
32 impl_is_zero!(u64, |x| x == 0);
33 impl_is_zero!(u128, |x| x == 0);
34 impl_is_zero!(usize, |x| x == 0);
36 impl_is_zero!(bool, |x| x == false);
37 impl_is_zero!(char, |x| x == '\0');
39 impl_is_zero!(f32, |x: f32| x.to_bits() == 0);
40 impl_is_zero!(f64, |x: f64| x.to_bits() == 0);
42 unsafe impl<T> IsZero for *const T {
44 fn is_zero(&self) -> bool {
49 unsafe impl<T> IsZero for *mut T {
51 fn is_zero(&self) -> bool {
56 unsafe impl<T: IsZero, const N: usize> IsZero for [T; N] {
58 fn is_zero(&self) -> bool {
59 // Because this is generated as a runtime check, it's not obvious that
60 // it's worth doing if the array is really long. The threshold here
61 // is largely arbitrary, but was picked because as of 2022-07-01 LLVM
62 // fails to const-fold the check in `vec![[1; 32]; n]`
63 // See https://github.com/rust-lang/rust/pull/97581#issuecomment-1166628022
64 // Feel free to tweak if you have better evidence.
66 N <= 16 && self.iter().all(IsZero::is_zero)
70 // This is recursive macro.
71 macro_rules! impl_for_tuples {
74 // No use for implementing for empty tuple because it is ZST.
76 ($first_arg:ident $(,$rest:ident)*) => {
77 unsafe impl <$first_arg: IsZero, $($rest: IsZero,)*> IsZero for ($first_arg, $($rest,)*){
79 fn is_zero(&self) -> bool{
80 // Destructure tuple to N references
81 // Rust allows to hide generic params by local variable names.
82 #[allow(non_snake_case)]
83 let ($first_arg, $($rest,)*) = self;
86 $( && $rest.is_zero() )*
90 impl_for_tuples!($($rest),*);
94 impl_for_tuples!(A, B, C, D, E, F, G, H);
96 // `Option<&T>` and `Option<Box<T>>` are guaranteed to represent `None` as null.
97 // For fat pointers, the bytes that would be the pointer metadata in the `Some`
98 // variant are padding in the `None` variant, so ignoring them and
99 // zero-initializing instead is ok.
100 // `Option<&mut T>` never implements `Clone`, so there's no need for an impl of
103 unsafe impl<T: ?Sized> IsZero for Option<&T> {
105 fn is_zero(&self) -> bool {
110 unsafe impl<T: ?Sized> IsZero for Option<Box<T>> {
112 fn is_zero(&self) -> bool {
117 // `Option<num::NonZeroU32>` and similar have a representation guarantee that
118 // they're the same size as the corresponding `u32` type, as well as a guarantee
119 // that transmuting between `NonZeroU32` and `Option<num::NonZeroU32>` works.
120 // While the documentation officially makes it UB to transmute from `None`,
121 // we're the standard library so we can make extra inferences, and we know that
122 // the only niche available to represent `None` is the one that's all zeros.
124 macro_rules! impl_is_zero_option_of_nonzero {
125 ($($t:ident,)+) => {$(
126 unsafe impl IsZero for Option<core::num::$t> {
128 fn is_zero(&self) -> bool {
135 impl_is_zero_option_of_nonzero!(
150 unsafe impl<T: IsZero> IsZero for Wrapping<T> {
152 fn is_zero(&self) -> bool {
157 unsafe impl<T: IsZero> IsZero for Saturating<T> {
159 fn is_zero(&self) -> bool {
164 macro_rules! impl_for_optional_bool {
166 unsafe impl IsZero for $t {
168 fn is_zero(&self) -> bool {
169 // SAFETY: This is *not* a stable layout guarantee, but
170 // inside `core` we're allowed to rely on the current rustc
171 // behaviour that options of bools will be one byte with
172 // no padding, so long as they're nested less than 254 deep.
173 let raw: u8 = unsafe { core::mem::transmute(*self) };
179 impl_for_optional_bool! {
181 Option<Option<bool>>,
182 Option<Option<Option<bool>>>,
183 // Could go further, but not worth the metadata overhead