about summary refs log tree commit diff
path: root/compiler/rustc_data_structures/src/sync
diff options
context:
space:
mode:
authorOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2023-03-14 12:30:16 +0000
committerOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2023-04-04 09:01:44 +0000
commita1d20cf7a20eb69b7a48c9897dfd8e9a3e4b6360 (patch)
tree2343cece877ce2843c69dcf8d112e518c0805cda /compiler/rustc_data_structures/src/sync
parent7edd1d8799aff9d4dfea72e37c500ec8fdb0afb8 (diff)
downloadrust-a1d20cf7a20eb69b7a48c9897dfd8e9a3e4b6360.tar.gz
rust-a1d20cf7a20eb69b7a48c9897dfd8e9a3e4b6360.zip
Another AppendOnlyVec
Diffstat (limited to 'compiler/rustc_data_structures/src/sync')
-rw-r--r--compiler/rustc_data_structures/src/sync/vec.rs31
1 files changed, 21 insertions, 10 deletions
diff --git a/compiler/rustc_data_structures/src/sync/vec.rs b/compiler/rustc_data_structures/src/sync/vec.rs
index 64b0aff6ca2..aefaa8519d5 100644
--- a/compiler/rustc_data_structures/src/sync/vec.rs
+++ b/compiler/rustc_data_structures/src/sync/vec.rs
@@ -75,20 +75,31 @@ impl<T: Copy> AppendOnlyVec<T> {
         #[cfg(parallel_compiler)]
         return self.vec.get(i);
     }
+
+    pub fn iter_enumerated(&self) -> impl Iterator<Item = (usize, T)> + '_ {
+        (0..)
+            .map(|i| (i, self.get(i)))
+            .take_while(|(_, o)| o.is_some())
+            .filter_map(|(i, o)| Some((i, o?)))
+    }
+
+    pub fn iter(&self) -> impl Iterator<Item = T> + '_ {
+        (0..).map(|i| self.get(i)).take_while(|o| o.is_some()).filter_map(|o| o)
+    }
 }
 
 impl<T: Copy + PartialEq> AppendOnlyVec<T> {
     pub fn contains(&self, val: T) -> bool {
-        for i in 0.. {
-            match self.get(i) {
-                None => return false,
-                Some(v) => {
-                    if val == v {
-                        return true;
-                    }
-                }
-            }
+        self.iter_enumerated().any(|(_, v)| v == val)
+    }
+}
+
+impl<A: Copy> FromIterator<A> for AppendOnlyVec<A> {
+    fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self {
+        let this = Self::new();
+        for val in iter {
+            this.push(val);
         }
-        false
+        this
     }
 }