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/src') 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