Skip to content

Commit 6c01c0e

Browse files
committedSep 29, 2019
Zero-initialize vec![None; n] for Option<&T>, Option<&mut T> and Option<Box<T>>
1 parent 0bbab7d commit 6c01c0e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
 

‎src/liballoc/vec.rs

+25
Original file line numberDiff line numberDiff line change
@@ -1748,6 +1748,31 @@ unsafe impl<T: ?Sized> IsZero for *mut T {
17481748
}
17491749
}
17501750

1751+
// `Option<&T>`, `Option<&mut T>` and `Option<Box<T>>` are guaranteed to represent `None` as null.
1752+
// For fat pointers, the bytes that would be the pointer metadata in the `Some` variant
1753+
// are padding in the `None` variant, so ignoring them and zero-initializing instead is ok.
1754+
1755+
unsafe impl<T: ?Sized> IsZero for Option<&T> {
1756+
#[inline]
1757+
fn is_zero(&self) -> bool {
1758+
self.is_none()
1759+
}
1760+
}
1761+
1762+
unsafe impl<T: ?Sized> IsZero for Option<&mut T> {
1763+
#[inline]
1764+
fn is_zero(&self) -> bool {
1765+
self.is_none()
1766+
}
1767+
}
1768+
1769+
unsafe impl<T: ?Sized> IsZero for Option<Box<T>> {
1770+
#[inline]
1771+
fn is_zero(&self) -> bool {
1772+
self.is_none()
1773+
}
1774+
}
1775+
17511776

17521777
////////////////////////////////////////////////////////////////////////////////
17531778
// Common trait implementations for Vec

0 commit comments

Comments
 (0)
Please sign in to comment.