From 8852408bfb358766d59b83f294148fb5eeae26a0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 10 Jan 2019 22:47:05 +0300 Subject: use arena for sysroot --- crates/ra_lsp_server/src/project_model/sysroot.rs | 129 ++++++++++++++-------- 1 file changed, 80 insertions(+), 49 deletions(-) (limited to 'crates/ra_lsp_server/src/project_model') diff --git a/crates/ra_lsp_server/src/project_model/sysroot.rs b/crates/ra_lsp_server/src/project_model/sysroot.rs index ae72c9c17..6c1a1a2a3 100644 --- a/crates/ra_lsp_server/src/project_model/sysroot.rs +++ b/crates/ra_lsp_server/src/project_model/sysroot.rs @@ -4,13 +4,24 @@ use std::{ }; use ra_syntax::SmolStr; -use rustc_hash::FxHashMap; +use ra_arena::{Arena, RawId, impl_arena_id}; use crate::Result; #[derive(Debug, Clone)] pub struct Sysroot { - crates: FxHashMap, + crates: Arena, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct SysrootCrate(RawId); +impl_arena_id!(SysrootCrate); + +#[derive(Debug, Clone)] +struct SysrootCrateData { + name: SmolStr, + path: PathBuf, + deps: Vec, } impl Sysroot { @@ -26,53 +37,73 @@ impl Sysroot { let sysroot_path = Path::new(stdout.trim()); let src = sysroot_path.join("lib/rustlib/src/rust/src"); - let crates: &[(&str, &[&str])] = &[ - ( - "std", - &[ - "alloc_jemalloc", - "alloc_system", - "panic_abort", - "rand", - "compiler_builtins", - "unwind", - "rustc_asan", - "rustc_lsan", - "rustc_msan", - "rustc_tsan", - "build_helper", - ], - ), - ("core", &[]), - ("alloc", &[]), - ("collections", &[]), - ("libc", &[]), - ("panic_unwind", &[]), - ("proc_macro", &[]), - ("rustc_unicode", &[]), - ("std_unicode", &[]), - ("test", &[]), - // Feature gated - ("alloc_jemalloc", &[]), - ("alloc_system", &[]), - ("compiler_builtins", &[]), - ("getopts", &[]), - ("panic_unwind", &[]), - ("panic_abort", &[]), - ("rand", &[]), - ("term", &[]), - ("unwind", &[]), - // Dependencies - ("build_helper", &[]), - ("rustc_asan", &[]), - ("rustc_lsan", &[]), - ("rustc_msan", &[]), - ("rustc_tsan", &[]), - ("syntax", &[]), - ]; + let mut sysroot = Sysroot { + crates: Arena::default(), + }; + for name in SYSROOT_CRATES.trim().lines() { + let path = src.join(format!("lib{}", name)).join("lib.rs"); + if path.exists() { + sysroot.crates.alloc(SysrootCrateData { + name: name.into(), + path, + deps: Vec::new(), + }); + } + } + if let Some(std) = sysroot.by_name("std") { + for dep in STD_DEPS.trim().lines() { + if let Some(dep) = sysroot.by_name(dep) { + sysroot.crates[std].deps.push(dep) + } + } + } + Ok(sysroot) + } - Ok(Sysroot { - crates: FxHashMap::default(), - }) + fn by_name(&self, name: &str) -> Option { + self.crates + .iter() + .find(|(_id, data)| data.name == name) + .map(|(id, _data)| id) } } + +const SYSROOT_CRATES: &str = " +std +core +alloc +collections +libc +panic_unwind +proc_macro +rustc_unicode +std_unicode +test +alloc_jemalloc +alloc_system +compiler_builtins +getopts +panic_unwind +panic_abort +rand +term +unwind +build_helper +rustc_asan +rustc_lsan +rustc_msan +rustc_tsan +syntax"; + +const STD_DEPS: &str = " +alloc_jemalloc +alloc_system +panic_abort +rand +compiler_builtins +unwind +rustc_asan +rustc_lsan +rustc_msan +rustc_tsan +build_helper"; -- cgit v1.2.3