1use core::num::{NonZero, Saturating, Wrapping};
23use crate::boxed::Box;
45#[rustc_specialization_trait]
6pub(super) unsafe trait IsZero {
7/// Whether this value's representation is all zeros,
8 /// or can be represented with all zeroes.
9fn is_zero(&self) -> bool;
10}
1112macro_rules! impl_is_zero {
13 ($t:ty, $is_zero:expr) => {
14unsafe impl IsZero for $t {
15#[inline]
16fn is_zero(&self) -> bool {
17$is_zero(*self)
18 }
19 }
20 };
21}
2223impl_is_zero!(i8, |x| x == 0); // It is needed to impl for arrays and tuples of i8.
24impl_is_zero!(i16, |x| x == 0);
25impl_is_zero!(i32, |x| x == 0);
26impl_is_zero!(i64, |x| x == 0);
27impl_is_zero!(i128, |x| x == 0);
28impl_is_zero!(isize, |x| x == 0);
2930impl_is_zero!(u8, |x| x == 0); // It is needed to impl for arrays and tuples of u8.
31impl_is_zero!(u16, |x| x == 0);
32impl_is_zero!(u32, |x| x == 0);
33impl_is_zero!(u64, |x| x == 0);
34impl_is_zero!(u128, |x| x == 0);
35impl_is_zero!(usize, |x| x == 0);
3637impl_is_zero!(bool, |x| x == false);
38impl_is_zero!(char, |x| x == '\0');
3940impl_is_zero!(f32, |x: f32| x.to_bits() == 0);
41impl_is_zero!(f64, |x: f64| x.to_bits() == 0);
4243// `IsZero` cannot be soundly implemented for pointers because of provenance
44// (see #135338).
4546unsafe impl<T: IsZero, const N: usize> IsZero for [T; N] {
47#[inline]
48fn is_zero(&self) -> bool {
49// Because this is generated as a runtime check, it's not obvious that
50 // it's worth doing if the array is really long. The threshold here
51 // is largely arbitrary, but was picked because as of 2022-07-01 LLVM
52 // fails to const-fold the check in `vec![[1; 32]; n]`
53 // See https://github.com/rust-lang/rust/pull/97581#issuecomment-1166628022
54 // Feel free to tweak if you have better evidence.
5556N <= 16 && self.iter().all(IsZero::is_zero)
57 }
58}
5960// This is recursive macro.
61macro_rules! impl_is_zero_tuples {
62// Stopper
63() => {
64// No use for implementing for empty tuple because it is ZST.
65};
66 ($first_arg:ident $(,$rest:ident)*) => {
67unsafe impl <$first_arg: IsZero, $($rest: IsZero,)*> IsZero for ($first_arg, $($rest,)*){
68#[inline]
69fn is_zero(&self) -> bool{
70// Destructure tuple to N references
71 // Rust allows to hide generic params by local variable names.
72#[allow(non_snake_case)]
73let ($first_arg, $($rest,)*) = self;
7475$first_arg.is_zero()
76 $( && $rest.is_zero() )*
77 }
78 }
7980impl_is_zero_tuples!($($rest),*);
81 }
82}
8384impl_is_zero_tuples!(A, B, C, D, E, F, G, H);
8586// `Option<&T>` and `Option<Box<T>>` are guaranteed to represent `None` as null.
87// For fat pointers, the bytes that would be the pointer metadata in the `Some`
88// variant are padding in the `None` variant, so ignoring them and
89// zero-initializing instead is ok.
90// `Option<&mut T>` never implements `Clone`, so there's no need for an impl of
91// `SpecFromElem`.
9293unsafe impl<T: ?Sized> IsZero for Option<&T> {
94#[inline]
95fn is_zero(&self) -> bool {
96self.is_none()
97 }
98}
99100unsafe impl<T: ?Sized> IsZero for Option<Box<T>> {
101#[inline]
102fn is_zero(&self) -> bool {
103self.is_none()
104 }
105}
106107// `Option<NonZero<u32>>` and similar have a representation guarantee that
108// they're the same size as the corresponding `u32` type, as well as a guarantee
109// that transmuting between `NonZero<u32>` and `Option<NonZero<u32>>` works.
110// While the documentation officially makes it UB to transmute from `None`,
111// we're the standard library so we can make extra inferences, and we know that
112// the only niche available to represent `None` is the one that's all zeros.
113macro_rules! impl_is_zero_option_of_nonzero_int {
114 ($($t:ty),+ $(,)?) => {$(
115unsafe impl IsZero for Option<NonZero<$t>> {
116#[inline]
117fn is_zero(&self) -> bool {
118self.is_none()
119 }
120 }
121 )+};
122}
123124impl_is_zero_option_of_nonzero_int!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
125126macro_rules! impl_is_zero_option_of_int {
127 ($($t:ty),+ $(,)?) => {$(
128unsafe impl IsZero for Option<$t> {
129#[inline]
130fn is_zero(&self) -> bool {
131const {
132let none: Self = unsafe { core::mem::MaybeUninit::zeroed().assume_init() };
133assert!(none.is_none());
134 }
135self.is_none()
136 }
137 }
138 )+};
139}
140141impl_is_zero_option_of_int!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, usize, isize);
142143unsafe impl<T: IsZero> IsZero for Wrapping<T> {
144#[inline]
145fn is_zero(&self) -> bool {
146self.0.is_zero()
147 }
148}
149150unsafe impl<T: IsZero> IsZero for Saturating<T> {
151#[inline]
152fn is_zero(&self) -> bool {
153self.0.is_zero()
154 }
155}
156157macro_rules! impl_is_zero_option_of_bool {
158 ($($t:ty),+ $(,)?) => {$(
159unsafe impl IsZero for $t {
160#[inline]
161fn is_zero(&self) -> bool {
162// SAFETY: This is *not* a stable layout guarantee, but
163 // inside `core` we're allowed to rely on the current rustc
164 // behavior that options of bools will be one byte with
165 // no padding, so long as they're nested less than 254 deep.
166let raw: u8 = unsafe { core::mem::transmute(*self) };
167 raw == 0
168}
169 }
170 )+};
171}
172173impl_is_zero_option_of_bool! {
174Option<bool>,
175Option<Option<bool>>,
176Option<Option<Option<bool>>>,
177// Could go further, but not worth the metadata overhead.
178}