rustc_data_structures/sync/
vec.rs
1use std::marker::PhantomData;
2
3use rustc_index::Idx;
4
5#[derive(Default)]
6pub struct AppendOnlyIndexVec<I: Idx, T: Copy> {
7 vec: elsa::sync::LockFreeFrozenVec<T>,
8 _marker: PhantomData<fn(&I)>,
9}
10
11impl<I: Idx, T: Copy> AppendOnlyIndexVec<I, T> {
12 pub fn new() -> Self {
13 Self { vec: elsa::sync::LockFreeFrozenVec::new(), _marker: PhantomData }
14 }
15
16 pub fn push(&self, val: T) -> I {
17 let i = self.vec.push(val);
18 I::new(i)
19 }
20
21 pub fn get(&self, i: I) -> Option<T> {
22 let i = i.index();
23 self.vec.get(i)
24 }
25}
26
27#[derive(Default)]
28pub struct AppendOnlyVec<T: Copy> {
29 vec: parking_lot::RwLock<Vec<T>>,
30}
31
32impl<T: Copy> AppendOnlyVec<T> {
33 pub fn new() -> Self {
34 Self { vec: Default::default() }
35 }
36
37 pub fn push(&self, val: T) -> usize {
38 let mut v = self.vec.write();
39 let n = v.len();
40 v.push(val);
41 n
42 }
43
44 pub fn get(&self, i: usize) -> Option<T> {
45 self.vec.read().get(i).copied()
46 }
47
48 pub fn iter_enumerated(&self) -> impl Iterator<Item = (usize, T)> + '_ {
49 (0..)
50 .map(|i| (i, self.get(i)))
51 .take_while(|(_, o)| o.is_some())
52 .filter_map(|(i, o)| Some((i, o?)))
53 }
54
55 pub fn iter(&self) -> impl Iterator<Item = T> + '_ {
56 (0..).map(|i| self.get(i)).take_while(|o| o.is_some()).flatten()
57 }
58}
59
60impl<T: Copy + PartialEq> AppendOnlyVec<T> {
61 pub fn contains(&self, val: T) -> bool {
62 self.iter_enumerated().any(|(_, v)| v == val)
63 }
64}
65
66impl<A: Copy> FromIterator<A> for AppendOnlyVec<A> {
67 fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self {
68 let this = Self::new();
69 for val in iter {
70 this.push(val);
71 }
72 this
73 }
74}