Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support arrays of zeros in Vec's __rust_alloc_zeroed optimization #95362

Merged
merged 1 commit into from
May 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion library/alloc/src/vec/is_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::boxed::Box;

#[rustc_specialization_trait]
pub(super) unsafe trait IsZero {
/// Whether this value is zero
/// Whether this value's representation is all zeros
fn is_zero(&self) -> bool;
}

Expand Down Expand Up @@ -49,6 +49,13 @@ unsafe impl<T> IsZero for *mut T {
}
}

unsafe impl<T: IsZero, const N: usize> IsZero for [T; N] {
#[inline]
fn is_zero(&self) -> bool {
self.iter().all(IsZero::is_zero)
}
}

// `Option<&T>` and `Option<Box<T>>` are guaranteed to represent `None` as null.
// For fat pointers, the bytes that would be the pointer metadata in the `Some`
// variant are padding in the `None` variant, so ignoring them and
Expand Down