diff options
| author | Ralf Jung <post@ralfj.de> | 2023-02-26 18:13:57 +0100 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2023-02-26 18:13:57 +0100 |
| commit | cb45103358c0fef99d7659e0949b3c65b3eabdd6 (patch) | |
| tree | e430c91fb4e29070945f79df7a47639e5cf4f9b1 /compiler/rustc_data_structures/src/sync | |
| parent | 9fb185210eaa9d2de7e8ab1161897edf2eaae758 (diff) | |
| parent | c4e0cd966062ca67daed20775f4e8a60c28e57df (diff) | |
| download | rust-cb45103358c0fef99d7659e0949b3c65b3eabdd6.tar.gz rust-cb45103358c0fef99d7659e0949b3c65b3eabdd6.zip | |
Merge from rustc
Diffstat (limited to 'compiler/rustc_data_structures/src/sync')
| -rw-r--r-- | compiler/rustc_data_structures/src/sync/vec.rs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/compiler/rustc_data_structures/src/sync/vec.rs b/compiler/rustc_data_structures/src/sync/vec.rs new file mode 100644 index 00000000000..cbea4f05999 --- /dev/null +++ b/compiler/rustc_data_structures/src/sync/vec.rs @@ -0,0 +1,41 @@ +use std::marker::PhantomData; + +use rustc_index::vec::Idx; + +pub struct AppendOnlyVec<I: Idx, T: Copy> { + #[cfg(not(parallel_compiler))] + vec: elsa::vec::FrozenVec<T>, + #[cfg(parallel_compiler)] + vec: elsa::sync::LockFreeFrozenVec<T>, + _marker: PhantomData<fn(&I)>, +} + +impl<I: Idx, T: Copy> AppendOnlyVec<I, T> { + pub fn new() -> Self { + Self { + #[cfg(not(parallel_compiler))] + vec: elsa::vec::FrozenVec::new(), + #[cfg(parallel_compiler)] + vec: elsa::sync::LockFreeFrozenVec::new(), + _marker: PhantomData, + } + } + + pub fn push(&self, val: T) -> I { + #[cfg(not(parallel_compiler))] + let i = self.vec.len(); + #[cfg(not(parallel_compiler))] + self.vec.push(val); + #[cfg(parallel_compiler)] + let i = self.vec.push(val); + I::new(i) + } + + pub fn get(&self, i: I) -> Option<T> { + let i = i.index(); + #[cfg(not(parallel_compiler))] + return self.vec.get_copy(i); + #[cfg(parallel_compiler)] + return self.vec.get(i); + } +} |
