Simplify Sysroot

This commit is contained in:
Aleksey Kladov 2020-03-19 17:59:31 +01:00
parent 516fe293a8
commit fc230b943b
2 changed files with 20 additions and 22 deletions

View file

@ -143,7 +143,7 @@ impl ProjectWorkspace {
roots.push(PackageRoot::new(root, member)); roots.push(PackageRoot::new(root, member));
} }
for krate in sysroot.crates() { 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 roots
} }
@ -260,7 +260,7 @@ impl ProjectWorkspace {
ProjectWorkspace::Cargo { cargo, sysroot } => { ProjectWorkspace::Cargo { cargo, sysroot } => {
let mut sysroot_crates = FxHashMap::default(); let mut sysroot_crates = FxHashMap::default();
for krate in sysroot.crates() { 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 // Crates from sysroot have `cfg(test)` disabled
let cfg_options = { let cfg_options = {
let mut opts = default_cfg_options.clone(); let mut opts = default_cfg_options.clone();
@ -274,7 +274,7 @@ impl ProjectWorkspace {
file_id, file_id,
Edition::Edition2018, Edition::Edition2018,
Some( Some(
CrateName::new(krate.name(&sysroot)) CrateName::new(&sysroot[krate].name)
.expect("Sysroot crate names should not contain dashes"), .expect("Sysroot crate names should not contain dashes"),
), ),
cfg_options, cfg_options,
@ -285,8 +285,8 @@ impl ProjectWorkspace {
} }
} }
for from in sysroot.crates() { for from in sysroot.crates() {
for to in from.deps(&sysroot) { for &to in sysroot[from].deps.iter() {
let name = to.name(&sysroot); let name = &sysroot[to].name;
if let (Some(&from), Some(&to)) = if let (Some(&from), Some(&to)) =
(sysroot_crates.get(&from), sysroot_crates.get(&to)) (sysroot_crates.get(&from), sysroot_crates.get(&to))
{ {

View file

@ -2,7 +2,7 @@
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use std::{ use std::{
env, env, ops,
path::{Path, PathBuf}, path::{Path, PathBuf},
process::{Command, Output}, process::{Command, Output},
}; };
@ -19,10 +19,17 @@ pub struct SysrootCrate(RawId);
impl_arena_id!(SysrootCrate); impl_arena_id!(SysrootCrate);
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct SysrootCrateData { pub struct SysrootCrateData {
name: String, pub name: String,
root: PathBuf, pub root: PathBuf,
deps: Vec<SysrootCrate>, pub deps: Vec<SysrootCrate>,
}
impl ops::Index<SysrootCrate> for Sysroot {
type Output = SysrootCrateData;
fn index(&self, index: SysrootCrate) -> &SysrootCrateData {
&self.crates[index]
}
} }
impl Sysroot { impl Sysroot {
@ -129,18 +136,9 @@ fn get_or_install_rust_src(cargo_toml: &Path) -> Result<PathBuf> {
Ok(src_path) Ok(src_path)
} }
impl SysrootCrate { impl SysrootCrateData {
pub fn name(self, sysroot: &Sysroot) -> &str { pub fn root_dir(&self) -> &Path {
&sysroot.crates[self].name self.root.parent().unwrap()
}
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<Item = SysrootCrate> + 'a {
sysroot.crates[self].deps.iter().copied()
} }
} }