about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorTyler Mandry <tmandry@gmail.com>2019-09-30 14:38:27 -0700
committerGitHub <noreply@github.com>2019-09-30 14:38:27 -0700
commita8ed9bfe0234b2389c3135ba0c539de198732e20 (patch)
treedde3ccbfc8323227a0e16f74493d2efa1bbfbdef /src/liballoc
parent1cd9b4bc8535da94c8e9288f0eaf69ad7027e539 (diff)
parent6c01c0e9b5b069ee2a61b6078058c69880a14f19 (diff)
downloadrust-a8ed9bfe0234b2389c3135ba0c539de198732e20.tar.gz
rust-a8ed9bfe0234b2389c3135ba0c539de198732e20.zip
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>>`
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/vec.rs25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs
index f6f59ae4082..7c6ded08bba 100644
--- a/src/liballoc/vec.rs
+++ b/src/liballoc/vec.rs
@@ -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