From fc230b943b1079556e6d844bc22790ffb969da9c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 19 Mar 2020 17:59:31 +0100 Subject: Simplify Sysroot --- crates/ra_project_model/src/lib.rs | 10 +++++----- crates/ra_project_model/src/sysroot.rs | 32 +++++++++++++++----------------- 2 files changed, 20 insertions(+), 22 deletions(-) (limited to 'crates') diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 3c294e065..b500a74fb 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -143,7 +143,7 @@ impl ProjectWorkspace { roots.push(PackageRoot::new(root, member)); } for krate in sysroot.crates() { - roots.push(PackageRoot::new(krate.root_dir(&sysroot).to_path_buf(), false)) + roots.push(PackageRoot::new(sysroot[krate].root_dir().to_path_buf(), false)) } roots } @@ -260,7 +260,7 @@ impl ProjectWorkspace { ProjectWorkspace::Cargo { cargo, sysroot } => { let mut sysroot_crates = FxHashMap::default(); for krate in sysroot.crates() { - if let Some(file_id) = load(krate.root(&sysroot)) { + if let Some(file_id) = load(&sysroot[krate].root) { // Crates from sysroot have `cfg(test)` disabled let cfg_options = { let mut opts = default_cfg_options.clone(); @@ -274,7 +274,7 @@ impl ProjectWorkspace { file_id, Edition::Edition2018, Some( - CrateName::new(krate.name(&sysroot)) + CrateName::new(&sysroot[krate].name) .expect("Sysroot crate names should not contain dashes"), ), cfg_options, @@ -285,8 +285,8 @@ impl ProjectWorkspace { } } for from in sysroot.crates() { - for to in from.deps(&sysroot) { - let name = to.name(&sysroot); + for &to in sysroot[from].deps.iter() { + let name = &sysroot[to].name; if let (Some(&from), Some(&to)) = (sysroot_crates.get(&from), sysroot_crates.get(&to)) { diff --git a/crates/ra_project_model/src/sysroot.rs b/crates/ra_project_model/src/sysroot.rs index c51957c1f..4ac4fa14d 100644 --- a/crates/ra_project_model/src/sysroot.rs +++ b/crates/ra_project_model/src/sysroot.rs @@ -2,7 +2,7 @@ use anyhow::{bail, Context, Result}; use std::{ - env, + env, ops, path::{Path, PathBuf}, process::{Command, Output}, }; @@ -19,10 +19,17 @@ pub struct SysrootCrate(RawId); impl_arena_id!(SysrootCrate); #[derive(Debug, Clone)] -struct SysrootCrateData { - name: String, - root: PathBuf, - deps: Vec, +pub struct SysrootCrateData { + pub name: String, + pub root: PathBuf, + pub deps: Vec, +} + +impl ops::Index for Sysroot { + type Output = SysrootCrateData; + fn index(&self, index: SysrootCrate) -> &SysrootCrateData { + &self.crates[index] + } } impl Sysroot { @@ -129,18 +136,9 @@ fn get_or_install_rust_src(cargo_toml: &Path) -> Result { Ok(src_path) } -impl SysrootCrate { - pub fn name(self, sysroot: &Sysroot) -> &str { - &sysroot.crates[self].name - } - pub fn root(self, sysroot: &Sysroot) -> &Path { - sysroot.crates[self].root.as_path() - } - pub fn root_dir(self, sysroot: &Sysroot) -> &Path { - self.root(sysroot).parent().unwrap() - } - pub fn deps<'a>(self, sysroot: &'a Sysroot) -> impl Iterator + 'a { - sysroot.crates[self].deps.iter().copied() +impl SysrootCrateData { + pub fn root_dir(&self) -> &Path { + self.root.parent().unwrap() } } -- cgit v1.2.3