]> git.proxmox.com Git - rustc.git/blobdiff - library/alloc/src/vec/is_zero.rs
New upstream version 1.62.1+dfsg1
[rustc.git] / library / alloc / src / vec / is_zero.rs
index 0efc4893c3c42847a696197a1a223687cd9f0741..edf270db81d4d8fb52339f8ac23162e15cb855b7 100644 (file)
@@ -2,7 +2,7 @@ use crate::boxed::Box;
 
 #[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,20 @@ unsafe impl<T> IsZero for *mut T {
     }
 }
 
+unsafe impl<T: IsZero, const N: usize> IsZero for [T; N] {
+    #[inline]
+    fn is_zero(&self) -> bool {
+        // Because this is generated as a runtime check, it's not obvious that
+        // it's worth doing if the array is really long.  The threshold here
+        // is largely arbitrary, but was picked because as of 2022-05-01 LLVM
+        // can const-fold the check in `vec![[0; 32]; n]` but not in
+        // `vec![[0; 64]; n]`: https://godbolt.org/z/WTzjzfs5b
+        // Feel free to tweak if you have better evidence.
+
+        N <= 32 && 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