From a1d20cf7a20eb69b7a48c9897dfd8e9a3e4b6360 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 14 Mar 2023 12:30:16 +0000 Subject: Another AppendOnlyVec --- compiler/rustc_data_structures/src/sync/vec.rs | 31 +++++++++++++++++--------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'compiler/rustc_data_structures/src/sync') 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 AppendOnlyVec { #[cfg(parallel_compiler)] return self.vec.get(i); } + + pub fn iter_enumerated(&self) -> impl Iterator + '_ { + (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 + '_ { + (0..).map(|i| self.get(i)).take_while(|o| o.is_some()).filter_map(|o| o) + } } impl AppendOnlyVec { 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 FromIterator for AppendOnlyVec { + fn from_iter>(iter: T) -> Self { + let this = Self::new(); + for val in iter { + this.push(val); } - false + this } } -- cgit 1.4.1-3-g733a5