From 9786a8f2d4cf0a2454919c75426bd3a847615a1c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 25 Aug 2020 18:39:18 +0200 Subject: Don't expose indexing details --- crates/project_model/src/lib.rs | 36 ++++++++++++-------------------- crates/project_model/src/project_json.rs | 8 ++++++- 2 files changed, 20 insertions(+), 24 deletions(-) (limited to 'crates/project_model/src') diff --git a/crates/project_model/src/lib.rs b/crates/project_model/src/lib.rs index cbd4c5009..724c586db 100644 --- a/crates/project_model/src/lib.rs +++ b/crates/project_model/src/lib.rs @@ -43,7 +43,7 @@ impl fmt::Debug for ProjectWorkspace { f.debug_struct("Cargo").field("n_packages", &cargo.packages().len()).finish() } ProjectWorkspace::Json { project } => { - f.debug_struct("Json").field("n_crates", &project.crates.len()).finish() + f.debug_struct("Json").field("n_crates", &project.n_crates()).finish() } } } @@ -202,9 +202,8 @@ impl ProjectWorkspace { pub fn to_roots(&self) -> Vec { match self { ProjectWorkspace::Json { project } => project - .crates - .iter() - .map(|krate| PackageRoot { + .crates() + .map(|(_, krate)| PackageRoot { is_member: krate.is_workspace_member, include: krate.include.clone(), exclude: krate.exclude.clone(), @@ -243,9 +242,8 @@ impl ProjectWorkspace { pub fn proc_macro_dylib_paths(&self) -> Vec { match self { ProjectWorkspace::Json { project } => project - .crates - .iter() - .filter_map(|krate| krate.proc_macro_dylib_path.as_ref()) + .crates() + .filter_map(|(_, krate)| krate.proc_macro_dylib_path.as_ref()) .cloned() .collect(), ProjectWorkspace::Cargo { cargo, sysroot: _sysroot } => cargo @@ -258,7 +256,7 @@ impl ProjectWorkspace { pub fn n_packages(&self) -> usize { match self { - ProjectWorkspace::Json { project, .. } => project.crates.len(), + ProjectWorkspace::Json { project, .. } => project.n_crates(), ProjectWorkspace::Cargo { cargo, sysroot } => { cargo.packages().len() + sysroot.crates().len() } @@ -276,10 +274,8 @@ impl ProjectWorkspace { ProjectWorkspace::Json { project } => { let mut cfg_cache: FxHashMap, Vec> = FxHashMap::default(); let crates: FxHashMap<_, _> = project - .crates - .iter() - .enumerate() - .filter_map(|(seq_index, krate)| { + .crates() + .filter_map(|(crate_id, krate)| { let file_path = &krate.root_module; let file_id = load(&file_path)?; @@ -297,9 +293,8 @@ impl ProjectWorkspace { let mut cfg_options = CfgOptions::default(); cfg_options.extend(target_cfgs.iter().chain(krate.cfg.iter()).cloned()); - // FIXME: No crate name in json definition such that we cannot add OUT_DIR to env Some(( - CrateId(seq_index as u32), + crate_id, crate_graph.add_crate_root( file_id, krate.edition, @@ -313,19 +308,14 @@ impl ProjectWorkspace { }) .collect(); - for (id, krate) in project.crates.iter().enumerate() { + for (from, krate) in project.crates() { for dep in &krate.deps { - let from_crate_id = CrateId(id as u32); let to_crate_id = dep.crate_id; if let (Some(&from), Some(&to)) = - (crates.get(&from_crate_id), crates.get(&to_crate_id)) + (crates.get(&from), crates.get(&to_crate_id)) { - if crate_graph.add_dep(from, dep.name.clone(), to).is_err() { - log::error!( - "cyclic dependency {:?} -> {:?}", - from_crate_id, - to_crate_id - ); + if let Err(_) = crate_graph.add_dep(from, dep.name.clone(), to) { + log::error!("cyclic dependency {:?} -> {:?}", from, to_crate_id); } } } diff --git a/crates/project_model/src/project_json.rs b/crates/project_model/src/project_json.rs index 060ea5b7d..ae14e5126 100644 --- a/crates/project_model/src/project_json.rs +++ b/crates/project_model/src/project_json.rs @@ -12,7 +12,7 @@ use crate::cfg_flag::CfgFlag; /// Roots and crates that compose this Rust project. #[derive(Clone, Debug, Eq, PartialEq)] pub struct ProjectJson { - pub(crate) crates: Vec, + crates: Vec, } /// A crate points to the root module of a crate and lists the dependencies of the crate. This is @@ -79,6 +79,12 @@ impl ProjectJson { .collect::>(), } } + pub fn n_crates(&self) -> usize { + self.crates.len() + } + pub fn crates(&self) -> impl Iterator + '_ { + self.crates.iter().enumerate().map(|(idx, krate)| (CrateId(idx as u32), krate)) + } } #[derive(Deserialize)] -- cgit v1.2.3