1
Fork 0

Rollup merge of #64893 - SimonSapin:vec-of-option-box, r=sfackler

Zero-initialize `vec![None; n]` for `Option<&T>`, `Option<&mut T>` and `Option<Box<T>>`
This commit is contained in:
Tyler Mandry 2019-09-30 14:38:27 -07:00 committed by GitHub
commit a8ed9bfe02
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1748,6 +1748,31 @@ unsafe impl<T> IsZero for *mut T {
}
}
// `Option<&T>`, `Option<&mut 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 zero-initializing instead is ok.
unsafe impl<T: ?Sized> IsZero for Option<&T> {
#[inline]
fn is_zero(&self) -> bool {
self.is_none()
}
}
unsafe impl<T: ?Sized> IsZero for Option<&mut T> {
#[inline]
fn is_zero(&self) -> bool {
self.is_none()
}
}
unsafe impl<T: ?Sized> IsZero for Option<Box<T>> {
#[inline]
fn is_zero(&self) -> bool {
self.is_none()
}
}
////////////////////////////////////////////////////////////////////////////////
// Common trait implementations for Vec