From 0320ebdd101b01abe5f24e9efcef7c15005fd3a5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 9 Mar 2020 11:11:59 +0100 Subject: Use `Index` for CrateGraph --- crates/ra_db/src/input.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'crates/ra_db') diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index 4069c0fed..cd078e43a 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -6,7 +6,7 @@ //! actual IO. See `vfs` and `project_model` in the `rust-analyzer` crate for how //! actual IO is done and lowered to input. -use std::{fmt, str::FromStr}; +use std::{fmt, ops, str::FromStr}; use ra_cfg::CfgOptions; use ra_syntax::SmolStr; @@ -174,10 +174,6 @@ impl CrateGraph { self.arena.keys().copied() } - pub fn crate_data(&self, crate_id: &CrateId) -> &CrateData { - &self.arena[crate_id] - } - // FIXME: this only finds one crate with the given root; we could have multiple pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option { let (&crate_id, _) = @@ -207,7 +203,7 @@ impl CrateGraph { return false; } - for dep in &self.crate_data(&from).dependencies { + for dep in &self[from].dependencies { let crate_id = dep.crate_id(); if crate_id == target { return true; @@ -221,6 +217,13 @@ impl CrateGraph { } } +impl ops::Index for CrateGraph { + type Output = CrateData; + fn index(&self, crate_id: CrateId) -> &CrateData { + &self.arena[&crate_id] + } +} + impl CrateId { pub fn shift(self, amount: u32) -> CrateId { CrateId(self.0 + amount) @@ -376,7 +379,7 @@ mod tests { .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2) .is_ok()); assert_eq!( - graph.crate_data(&crate1).dependencies, + graph[crate1].dependencies, vec![Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }] ); } -- cgit v1.2.3 From 254ef1860b4b5a27bfcba2d20f29761c14a9ab2e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 9 Mar 2020 11:14:51 +0100 Subject: Minimize API --- crates/ra_db/src/input.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'crates/ra_db') diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index cd078e43a..3da28b435 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -111,8 +111,8 @@ pub struct CrateData { /// This actual crate name can be different in a particular dependent crate /// or may even be missing for some cases, such as a dummy crate for the code snippet. pub display_name: Option, - cfg_options: CfgOptions, - env: Env, + pub cfg_options: CfgOptions, + pub env: Env, pub dependencies: Vec, } @@ -149,10 +149,6 @@ impl CrateGraph { crate_id } - pub fn cfg_options(&self, crate_id: CrateId) -> &CfgOptions { - &self.arena[&crate_id].cfg_options - } - pub fn add_dep( &mut self, from: CrateId, -- cgit v1.2.3 From d0d5aa935b8b5ceb339fb2afabf449032559766a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 9 Mar 2020 11:17:39 +0100 Subject: Simplify --- crates/ra_db/src/input.rs | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) (limited to 'crates/ra_db') diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index 3da28b435..2912eb9ba 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -142,7 +142,14 @@ impl CrateGraph { cfg_options: CfgOptions, env: Env, ) -> CrateId { - let data = CrateData::new(file_id, edition, display_name, cfg_options, env); + let data = CrateData { + root_file_id: file_id, + edition, + display_name, + cfg_options, + env, + dependencies: Vec::new(), + }; let crate_id = CrateId(self.arena.len() as u32); let prev = self.arena.insert(crate_id, data); assert!(prev.is_none()); @@ -227,23 +234,6 @@ impl CrateId { } impl CrateData { - fn new( - root_file_id: FileId, - edition: Edition, - display_name: Option, - cfg_options: CfgOptions, - env: Env, - ) -> CrateData { - CrateData { - root_file_id, - edition, - display_name, - dependencies: Vec::new(), - cfg_options, - env, - } - } - fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) { self.dependencies.push(Dependency { name, crate_id }) } -- cgit v1.2.3 From 100cbc57ce2bd903ecab7d8bfb0abf7777076510 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 9 Mar 2020 11:18:41 +0100 Subject: Simplify --- crates/ra_db/src/input.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'crates/ra_db') diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index 2912eb9ba..b77640b2b 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -207,7 +207,7 @@ impl CrateGraph { } for dep in &self[from].dependencies { - let crate_id = dep.crate_id(); + let crate_id = dep.crate_id; if crate_id == target { return true; } @@ -261,12 +261,6 @@ impl fmt::Display for Edition { } } -impl Dependency { - pub fn crate_id(&self) -> CrateId { - self.crate_id - } -} - #[derive(Debug)] pub struct ParseEditionError { invalid_input: String, -- cgit v1.2.3