aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_db/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-03-09 10:11:59 +0000
committerAleksey Kladov <[email protected]>2020-03-09 10:11:59 +0000
commit0320ebdd101b01abe5f24e9efcef7c15005fd3a5 (patch)
tree37d5176dfabe886b1eadb2ddb16fbb6834df0a16 /crates/ra_db/src
parent57c27f91392fdd9d72fd023f4e2fecd8b68a7d09 (diff)
Use `Index` for CrateGraph
Diffstat (limited to 'crates/ra_db/src')
-rw-r--r--crates/ra_db/src/input.rs17
1 files changed, 10 insertions, 7 deletions
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 @@
6//! actual IO. See `vfs` and `project_model` in the `rust-analyzer` crate for how 6//! actual IO. See `vfs` and `project_model` in the `rust-analyzer` crate for how
7//! actual IO is done and lowered to input. 7//! actual IO is done and lowered to input.
8 8
9use std::{fmt, str::FromStr}; 9use std::{fmt, ops, str::FromStr};
10 10
11use ra_cfg::CfgOptions; 11use ra_cfg::CfgOptions;
12use ra_syntax::SmolStr; 12use ra_syntax::SmolStr;
@@ -174,10 +174,6 @@ impl CrateGraph {
174 self.arena.keys().copied() 174 self.arena.keys().copied()
175 } 175 }
176 176
177 pub fn crate_data(&self, crate_id: &CrateId) -> &CrateData {
178 &self.arena[crate_id]
179 }
180
181 // FIXME: this only finds one crate with the given root; we could have multiple 177 // FIXME: this only finds one crate with the given root; we could have multiple
182 pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> { 178 pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> {
183 let (&crate_id, _) = 179 let (&crate_id, _) =
@@ -207,7 +203,7 @@ impl CrateGraph {
207 return false; 203 return false;
208 } 204 }
209 205
210 for dep in &self.crate_data(&from).dependencies { 206 for dep in &self[from].dependencies {
211 let crate_id = dep.crate_id(); 207 let crate_id = dep.crate_id();
212 if crate_id == target { 208 if crate_id == target {
213 return true; 209 return true;
@@ -221,6 +217,13 @@ impl CrateGraph {
221 } 217 }
222} 218}
223 219
220impl ops::Index<CrateId> for CrateGraph {
221 type Output = CrateData;
222 fn index(&self, crate_id: CrateId) -> &CrateData {
223 &self.arena[&crate_id]
224 }
225}
226
224impl CrateId { 227impl CrateId {
225 pub fn shift(self, amount: u32) -> CrateId { 228 pub fn shift(self, amount: u32) -> CrateId {
226 CrateId(self.0 + amount) 229 CrateId(self.0 + amount)
@@ -376,7 +379,7 @@ mod tests {
376 .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2) 379 .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2)
377 .is_ok()); 380 .is_ok());
378 assert_eq!( 381 assert_eq!(
379 graph.crate_data(&crate1).dependencies, 382 graph[crate1].dependencies,
380 vec![Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }] 383 vec![Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }]
381 ); 384 );
382 } 385 }