]> git.lizzy.rs Git - rust.git/commitdiff
use arena for sysroot
authorAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 10 Jan 2019 19:47:05 +0000 (22:47 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 10 Jan 2019 21:51:34 +0000 (00:51 +0300)
crates/ra_arena/src/lib.rs
crates/ra_lsp_server/src/project_model/sysroot.rs

index 43bfa925a7c32a3fac320fd359873d7ebd670fc6..d7d5d5265c575dce3f9f7ec2cacf435d4b97d920 100644 (file)
@@ -35,12 +35,21 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
     }
 }
 
-#[derive(Clone, Debug, PartialEq, Eq)]
+#[derive(Clone, PartialEq, Eq)]
 pub struct Arena<ID: ArenaId, T> {
     data: Vec<T>,
     _ty: PhantomData<ID>,
 }
 
+impl<ID: ArenaId, T: fmt::Debug> fmt::Debug for Arena<ID, T> {
+    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
+        fmt.debug_struct("Arena")
+            .field("len", &self.len())
+            .field("data", &self.data)
+            .finish()
+    }
+}
+
 #[macro_export]
 macro_rules! impl_arena_id {
     ($name:ident) => {
index ae72c9c17a94ffeb8c40afcc882be068040a8ed0..6c1a1a2a392c40be60640b4b886186b37f2d5b30 100644 (file)
@@ -4,13 +4,24 @@
 };
 
 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<SmolStr, PathBuf>,
+    crates: Arena<SysrootCrate, SysrootCrateData>,
+}
+
+#[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<SysrootCrate>,
 }
 
 impl Sysroot {
@@ -26,53 +37,73 @@ pub(crate) fn discover(cargo_toml: &Path) -> Result<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<SysrootCrate> {
+        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";