Skip to content
Permalink
Browse files

Zero-initialize `vec![None; n]` for `Option<&T>`, `Option<&mut T>` an…

…d `Option<Box<T>>`
  • Loading branch information...
SimonSapin committed Sep 29, 2019
1 parent 0bbab7d commit 6c01c0e9b5b069ee2a61b6078058c69880a14f19
Showing with 25 additions and 0 deletions.
  1. +25 −0 src/liballoc/vec.rs
@@ -1748,6 +1748,31 @@ unsafe impl<T: ?Sized> 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

0 comments on commit 6c01c0e

Please sign in to comment.
You can’t perform that action at this time.