From bef899aa78f65dbf4ee0d304c171ba02e101a685 Mon Sep 17 00:00:00 2001 From: veetaha Date: Wed, 1 Apr 2020 03:13:39 +0300 Subject: [PATCH] Less mutability --- crates/ra_project_model/src/lib.rs | 42 ++++++++++++++++-------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index e9c0c83dbb4..ec857495220 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -185,10 +185,12 @@ pub fn to_crate_graph( let mut crate_graph = CrateGraph::default(); match self { ProjectWorkspace::Json { project } => { - let mut crates = FxHashMap::default(); - for (id, krate) in project.crates.iter().enumerate() { - let crate_id = json_project::CrateId(id); - if let Some(file_id) = load(&krate.root_module) { + let crates: FxHashMap<_, _> = project + .crates + .iter() + .enumerate() + .filter_map(|(seq_index, krate)| { + let file_id = load(&krate.root_module)?; let edition = match krate.edition { json_project::Edition::Edition2015 => Edition::Edition2015, json_project::Edition::Edition2018 => Edition::Edition2018, @@ -218,8 +220,8 @@ pub fn to_crate_graph( .clone() .map(|it| proc_macro_client.by_dylib_path(&it)); // FIXME: No crate name in json definition such that we cannot add OUT_DIR to env - crates.insert( - crate_id, + Some(( + json_project::CrateId(seq_index), crate_graph.add_crate_root( file_id, edition, @@ -230,9 +232,9 @@ pub fn to_crate_graph( extern_source, proc_macro.unwrap_or_default(), ), - ); - } - } + )) + }) + .collect(); for (id, krate) in project.crates.iter().enumerate() { for dep in &krate.deps { @@ -256,9 +258,11 @@ pub fn to_crate_graph( } } ProjectWorkspace::Cargo { cargo, sysroot } => { - let mut sysroot_crates = FxHashMap::default(); - for krate in sysroot.crates() { - if let Some(file_id) = load(&sysroot[krate].root) { + let sysroot_crates: FxHashMap<_, _> = sysroot + .crates() + .filter_map(|krate| { + let file_id = load(&sysroot[krate].root)?; + // Crates from sysroot have `cfg(test)` disabled let cfg_options = { let mut opts = default_cfg_options.clone(); @@ -269,22 +273,22 @@ pub fn to_crate_graph( let env = Env::default(); let extern_source = ExternSource::default(); let proc_macro = vec![]; + let crate_name = CrateName::new(&sysroot[krate].name) + .expect("Sysroot crate names should not contain dashes"); let crate_id = crate_graph.add_crate_root( file_id, Edition::Edition2018, - Some( - CrateName::new(&sysroot[krate].name) - .expect("Sysroot crate names should not contain dashes"), - ), + Some(crate_name), cfg_options, env, extern_source, proc_macro, ); - sysroot_crates.insert(krate, crate_id); - } - } + Some((krate, crate_id)) + }) + .collect(); + for from in sysroot.crates() { for &to in sysroot[from].deps.iter() { let name = &sysroot[to].name;