diff options
| author | Simon Sapin <simon.sapin@exyr.org> | 2019-09-29 11:14:59 +0200 |
|---|---|---|
| committer | Simon Sapin <simon.sapin@exyr.org> | 2019-09-29 11:14:59 +0200 |
| commit | 6c01c0e9b5b069ee2a61b6078058c69880a14f19 (patch) | |
| tree | 14e82352b794976e17df0807a56661dd18f80d5b | |
| parent | 0bbab7d99dde8620604fb265706dc8bff20345a7 (diff) | |
| download | rust-6c01c0e9b5b069ee2a61b6078058c69880a14f19.tar.gz rust-6c01c0e9b5b069ee2a61b6078058c69880a14f19.zip | |
Zero-initialize `vec![None; n]` for `Option<&T>`, `Option<&mut T>` and `Option<Box<T>>`
| -rw-r--r-- | src/liballoc/vec.rs | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index e5672f8542f..d5089c5e2eb 100644 --- a/src/liballoc/vec.rs +++ b/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 |
