]> git.lizzy.rs Git - rust.git/commitdiff
Support arrays of zeros in Vec's __rust_alloc_zeroed optimization
authorScott McMurray <scottmcm@users.noreply.github.com>
Sun, 27 Mar 2022 08:03:10 +0000 (01:03 -0700)
committerScott McMurray <scottmcm@users.noreply.github.com>
Sun, 27 Mar 2022 08:50:07 +0000 (01:50 -0700)
library/alloc/src/vec/is_zero.rs

index 0efc4893c3c42847a696197a1a223687cd9f0741..868f2f1e3231b089fd906854216871a7feb63a84 100644 (file)
@@ -2,7 +2,7 @@
 
 #[rustc_specialization_trait]
 pub(super) unsafe trait IsZero {
-    /// Whether this value is zero
+    /// Whether this value's representation is all zeros
     fn is_zero(&self) -> bool;
 }
 
@@ -49,6 +49,13 @@ fn is_zero(&self) -> bool {
     }
 }
 
+unsafe impl<T: IsZero, const N: usize> IsZero for [T; N] {
+    #[inline]
+    fn is_zero(&self) -> bool {
+        self.iter().all(IsZero::is_zero)
+    }
+}
+
 // `Option<&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