mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-04 02:54:00 +00:00
Use Index
for CrateGraph
This commit is contained in:
parent
57c27f9139
commit
0320ebdd10
@ -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<CrateId> {
|
||||
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<CrateId> 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() }]
|
||||
);
|
||||
}
|
||||
|
@ -54,8 +54,7 @@ pub struct CrateDependency {
|
||||
|
||||
impl Crate {
|
||||
pub fn dependencies(self, db: &impl DefDatabase) -> Vec<CrateDependency> {
|
||||
db.crate_graph()
|
||||
.crate_data(&self.id)
|
||||
db.crate_graph()[self.id]
|
||||
.dependencies
|
||||
.iter()
|
||||
.map(|dep| {
|
||||
@ -72,7 +71,7 @@ impl Crate {
|
||||
crate_graph
|
||||
.iter()
|
||||
.filter(|&krate| {
|
||||
crate_graph.crate_data(&krate).dependencies.iter().any(|it| it.crate_id == self.id)
|
||||
crate_graph[krate].dependencies.iter().any(|it| it.crate_id == self.id)
|
||||
})
|
||||
.map(|id| Crate { id })
|
||||
.collect()
|
||||
@ -84,11 +83,11 @@ impl Crate {
|
||||
}
|
||||
|
||||
pub fn root_file(self, db: &impl DefDatabase) -> FileId {
|
||||
db.crate_graph().crate_data(&self.id).root_file_id
|
||||
db.crate_graph()[self.id].root_file_id
|
||||
}
|
||||
|
||||
pub fn edition(self, db: &impl DefDatabase) -> Edition {
|
||||
db.crate_graph().crate_data(&self.id).edition
|
||||
db.crate_graph()[self.id].edition
|
||||
}
|
||||
|
||||
pub fn all(db: &impl DefDatabase) -> Vec<Crate> {
|
||||
|
@ -176,7 +176,7 @@ fn find_importable_locations(
|
||||
// directly (only through reexports in direct dependencies).
|
||||
for krate in Some(from.krate)
|
||||
.into_iter()
|
||||
.chain(crate_graph.crate_data(&from.krate).dependencies.iter().map(|dep| dep.crate_id))
|
||||
.chain(crate_graph[from.krate].dependencies.iter().map(|dep| dep.crate_id))
|
||||
{
|
||||
result.extend(
|
||||
importable_locations_in_crate(db, item, krate)
|
||||
|
@ -116,8 +116,7 @@ impl LangItems {
|
||||
if let Some(target) = start_crate_target {
|
||||
return Some(*target);
|
||||
}
|
||||
db.crate_graph()
|
||||
.crate_data(&start_crate)
|
||||
db.crate_graph()[start_crate]
|
||||
.dependencies
|
||||
.iter()
|
||||
.find_map(|dep| db.lang_item(dep.crate_id, item.clone()))
|
||||
|
@ -179,7 +179,7 @@ impl CrateDefMap {
|
||||
pub(crate) fn crate_def_map_query(db: &impl DefDatabase, krate: CrateId) -> Arc<CrateDefMap> {
|
||||
let _p = profile("crate_def_map_query");
|
||||
let def_map = {
|
||||
let edition = db.crate_graph().crate_data(&krate).edition;
|
||||
let edition = db.crate_graph()[krate].edition;
|
||||
let mut modules: Arena<LocalModuleId, ModuleData> = Arena::default();
|
||||
let root = modules.alloc(ModuleData::default());
|
||||
CrateDefMap {
|
||||
|
@ -34,7 +34,7 @@ pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> C
|
||||
let crate_graph = db.crate_graph();
|
||||
|
||||
// populate external prelude
|
||||
for dep in &crate_graph.crate_data(&def_map.krate).dependencies {
|
||||
for dep in &crate_graph[def_map.krate].dependencies {
|
||||
let dep_def_map = db.crate_def_map(dep.crate_id);
|
||||
log::debug!("crate dep {:?} -> {:?}", dep.name, dep.crate_id);
|
||||
def_map.extern_prelude.insert(
|
||||
@ -128,7 +128,7 @@ where
|
||||
DB: DefDatabase,
|
||||
{
|
||||
fn collect(&mut self) {
|
||||
let file_id = self.db.crate_graph().crate_data(&self.def_map.krate).root_file_id;
|
||||
let file_id = self.db.crate_graph()[self.def_map.krate].root_file_id;
|
||||
let raw_items = self.db.raw_items(file_id.into());
|
||||
let module_id = self.def_map.root;
|
||||
self.def_map.modules[module_id].origin = ModuleOrigin::CrateRoot { definition: file_id };
|
||||
@ -954,7 +954,7 @@ mod tests {
|
||||
let krate = db.test_crate();
|
||||
|
||||
let def_map = {
|
||||
let edition = db.crate_graph().crate_data(&krate).edition;
|
||||
let edition = db.crate_graph()[krate].edition;
|
||||
let mut modules: Arena<LocalModuleId, ModuleData> = Arena::default();
|
||||
let root = modules.alloc(ModuleData::default());
|
||||
CrateDefMap {
|
||||
|
@ -47,7 +47,7 @@ pub(crate) fn impls_for_trait_query(
|
||||
// will only ever get called for a few crates near the root of the tree (the
|
||||
// ones the user is editing), so this may actually be a waste of memory. I'm
|
||||
// doing it like this mainly for simplicity for now.
|
||||
for dep in &db.crate_graph().crate_data(&krate).dependencies {
|
||||
for dep in &db.crate_graph()[krate].dependencies {
|
||||
impls.extend(db.impls_for_trait(dep.crate_id, trait_).iter());
|
||||
}
|
||||
let crate_impl_defs = db.impls_in_crate(krate);
|
||||
|
@ -121,7 +121,7 @@ fn definition_owner_name(db: &RootDatabase, def: &Definition) -> Option<String>
|
||||
|
||||
fn determine_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> {
|
||||
let mod_path = def.module(db).map(|module| {
|
||||
once(db.crate_graph().crate_data(&module.krate().into()).display_name.clone())
|
||||
once(db.crate_graph()[module.krate().into()].display_name.clone())
|
||||
.chain(
|
||||
module
|
||||
.path_to_root(db)
|
||||
|
@ -421,12 +421,12 @@ impl Analysis {
|
||||
|
||||
/// Returns the edition of the given crate.
|
||||
pub fn crate_edition(&self, crate_id: CrateId) -> Cancelable<Edition> {
|
||||
self.with_db(|db| db.crate_graph().crate_data(&crate_id).edition)
|
||||
self.with_db(|db| db.crate_graph()[crate_id].edition)
|
||||
}
|
||||
|
||||
/// Returns the root file of the given crate.
|
||||
pub fn crate_root(&self, crate_id: CrateId) -> Cancelable<FileId> {
|
||||
self.with_db(|db| db.crate_graph().crate_data(&crate_id).root_file_id)
|
||||
self.with_db(|db| db.crate_graph()[crate_id].root_file_id)
|
||||
}
|
||||
|
||||
/// Returns the set of possible targets to run for the current file.
|
||||
|
Loading…
Reference in New Issue
Block a user