mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
New VFS API
This commit is contained in:
parent
db6100dbaa
commit
c002322bde
8 changed files with 520 additions and 0 deletions
31
crates/vfs/src/path_interner.rs
Normal file
31
crates/vfs/src/path_interner.rs
Normal file
|
@ -0,0 +1,31 @@
|
|||
//! Maps paths to compact integer ids. We don't care about clearings paths which
|
||||
//! no longer exist -- the assumption is total size of paths we ever look at is
|
||||
//! not too big.
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
use crate::{FileId, VfsPath};
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct PathInterner {
|
||||
map: FxHashMap<VfsPath, FileId>,
|
||||
vec: Vec<VfsPath>,
|
||||
}
|
||||
|
||||
impl PathInterner {
|
||||
pub(crate) fn get(&self, path: &VfsPath) -> Option<FileId> {
|
||||
self.map.get(path).copied()
|
||||
}
|
||||
pub(crate) fn intern(&mut self, path: VfsPath) -> FileId {
|
||||
if let Some(id) = self.get(&path) {
|
||||
return id;
|
||||
}
|
||||
let id = FileId(self.vec.len() as u32);
|
||||
self.map.insert(path.clone(), id);
|
||||
self.vec.push(path);
|
||||
id
|
||||
}
|
||||
|
||||
pub(crate) fn lookup(&self, id: FileId) -> &VfsPath {
|
||||
&self.vec[id.0 as usize]
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue