remove arena from Roots

we want to move ra_vfs to a new repo, so having fewer deps is useful.
Arena is a thin layer of sugar on top of Vec anyway.
This commit is contained in:
Aleksey Kladov 2019-02-18 14:13:13 +03:00
parent d151b2a655
commit 4c154c289e
2 changed files with 18 additions and 16 deletions

View File

@ -25,7 +25,7 @@ use std::{
};
use crossbeam_channel::Receiver;
use ra_arena::{impl_arena_id, Arena, RawId, map::ArenaMap};
use ra_arena::{impl_arena_id, Arena, RawId};
use relative_path::{RelativePath, RelativePathBuf};
use rustc_hash::{FxHashMap, FxHashSet};
@ -53,7 +53,7 @@ struct VfsFileData {
pub struct Vfs {
roots: Arc<Roots>,
files: Arena<VfsFile, VfsFileData>,
root2files: ArenaMap<VfsRoot, FxHashSet<VfsFile>>,
root2files: FxHashMap<VfsRoot, FxHashSet<VfsFile>>,
pending_changes: Vec<VfsChange>,
worker: Worker,
}
@ -72,7 +72,7 @@ impl Vfs {
pub fn new(roots: Vec<PathBuf>) -> (Vfs, Vec<VfsRoot>) {
let roots = Arc::new(Roots::new(roots));
let worker = io::start(Arc::clone(&roots));
let mut root2files = ArenaMap::default();
let mut root2files = FxHashMap::default();
for root in roots.iter() {
root2files.insert(root, Default::default());
@ -164,7 +164,7 @@ impl Vfs {
let mut cur_files = Vec::new();
// While we were scanning the root in the background, a file might have
// been open in the editor, so we need to account for that.
let existing = self.root2files[root]
let existing = self.root2files[&root]
.iter()
.map(|&file| (self.files[file].path.clone(), file))
.collect::<FxHashMap<_, _>>();
@ -241,7 +241,7 @@ impl Vfs {
) -> VfsFile {
let data = VfsFileData { root, path, text, is_overlayed };
let file = self.files.alloc(data);
self.root2files.get_mut(root).unwrap().insert(file);
self.root2files.get_mut(&root).unwrap().insert(file);
file
}
@ -256,7 +256,7 @@ impl Vfs {
self.files[file].text = Default::default();
self.files[file].path = Default::default();
let root = self.files[file].root;
let removed = self.root2files.get_mut(root).unwrap().remove(&file);
let removed = self.root2files.get_mut(&root).unwrap().remove(&file);
assert!(removed);
}
@ -267,7 +267,7 @@ impl Vfs {
}
fn find_file(&self, root: VfsRoot, path: &RelativePath) -> Option<VfsFile> {
self.root2files[root].iter().map(|&it| it).find(|&file| self.files[file].path == path)
self.root2files[&root].iter().map(|&it| it).find(|&file| self.files[file].path == path)
}
}

View File

@ -4,12 +4,10 @@ use std::{
};
use relative_path::{ RelativePath, RelativePathBuf};
use ra_arena::{impl_arena_id, Arena, RawId};
/// VfsRoot identifies a watched directory on the file system.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct VfsRoot(pub RawId);
impl_arena_id!(VfsRoot);
pub struct VfsRoot(pub u32);
/// Describes the contents of a single source root.
///
@ -24,12 +22,12 @@ struct RootData {
}
pub(crate) struct Roots {
roots: Arena<VfsRoot, RootData>,
roots: Vec<RootData>,
}
impl Roots {
pub(crate) fn new(mut paths: Vec<PathBuf>) -> Roots {
let mut roots = Arena::default();
let mut roots = Vec::new();
// A hack to make nesting work.
paths.sort_by_key(|it| std::cmp::Reverse(it.as_os_str().len()));
paths.dedup();
@ -37,7 +35,7 @@ impl Roots {
let nested_roots =
paths[..i].iter().filter_map(|it| rel_path(path, it)).collect::<Vec<_>>();
roots.alloc(RootData::new(path.clone(), nested_roots));
roots.push(RootData::new(path.clone(), nested_roots));
}
Roots { roots }
}
@ -51,20 +49,24 @@ impl Roots {
self.roots.len()
}
pub(crate) fn iter<'a>(&'a self) -> impl Iterator<Item = VfsRoot> + 'a {
self.roots.iter().map(|(id, _)| id)
(0..self.roots.len()).into_iter().map(|idx| VfsRoot(idx as u32))
}
pub(crate) fn path(&self, root: VfsRoot) -> &Path {
self.roots[root].path.as_path()
self.root(root).path.as_path()
}
/// Checks if root contains a path and returns a root-relative path.
pub(crate) fn contains(&self, root: VfsRoot, path: &Path) -> Option<RelativePathBuf> {
let data = &self.roots[root];
let data = self.root(root);
iter::once(&data.path)
.chain(data.canonical_path.as_ref().into_iter())
.find_map(|base| rel_path(base, path))
.filter(|path| !data.excluded_dirs.contains(path))
.filter(|path| !data.is_excluded(path))
}
fn root(&self, root: VfsRoot) -> &RootData {
&self.roots[root.0 as usize]
}
}
impl RootData {