diff options
Diffstat (limited to 'crates/vfs/src/path_interner.rs')
-rw-r--r-- | crates/vfs/src/path_interner.rs | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/crates/vfs/src/path_interner.rs b/crates/vfs/src/path_interner.rs index 4f70d61e8..2189e5e25 100644 --- a/crates/vfs/src/path_interner.rs +++ b/crates/vfs/src/path_interner.rs | |||
@@ -5,6 +5,7 @@ use rustc_hash::FxHashMap; | |||
5 | 5 | ||
6 | use crate::{FileId, VfsPath}; | 6 | use crate::{FileId, VfsPath}; |
7 | 7 | ||
8 | /// Structure to map between [`VfsPath`] and [`FileId`]. | ||
8 | #[derive(Default)] | 9 | #[derive(Default)] |
9 | pub(crate) struct PathInterner { | 10 | pub(crate) struct PathInterner { |
10 | map: FxHashMap<VfsPath, FileId>, | 11 | map: FxHashMap<VfsPath, FileId>, |
@@ -12,9 +13,17 @@ pub(crate) struct PathInterner { | |||
12 | } | 13 | } |
13 | 14 | ||
14 | impl PathInterner { | 15 | impl PathInterner { |
16 | /// Get the id corresponding to `path`. | ||
17 | /// | ||
18 | /// If `path` does not exists in `self`, returns [`None`]. | ||
15 | pub(crate) fn get(&self, path: &VfsPath) -> Option<FileId> { | 19 | pub(crate) fn get(&self, path: &VfsPath) -> Option<FileId> { |
16 | self.map.get(path).copied() | 20 | self.map.get(path).copied() |
17 | } | 21 | } |
22 | |||
23 | /// Insert `path` in `self`. | ||
24 | /// | ||
25 | /// - If `path` already exists in `self`, returns its associated id; | ||
26 | /// - Else, returns a newly allocated id. | ||
18 | pub(crate) fn intern(&mut self, path: VfsPath) -> FileId { | 27 | pub(crate) fn intern(&mut self, path: VfsPath) -> FileId { |
19 | if let Some(id) = self.get(&path) { | 28 | if let Some(id) = self.get(&path) { |
20 | return id; | 29 | return id; |
@@ -25,6 +34,11 @@ impl PathInterner { | |||
25 | id | 34 | id |
26 | } | 35 | } |
27 | 36 | ||
37 | /// Returns the path corresponding to `id`. | ||
38 | /// | ||
39 | /// # Panics | ||
40 | /// | ||
41 | /// Panics if `id` does not exists in `self`. | ||
28 | pub(crate) fn lookup(&self, id: FileId) -> &VfsPath { | 42 | pub(crate) fn lookup(&self, id: FileId) -> &VfsPath { |
29 | &self.vec[id.0 as usize] | 43 | &self.vec[id.0 as usize] |
30 | } | 44 | } |