From 0b1c0ee225a075d25d700fcd1c007615556e2afb Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 5 Dec 2018 16:01:18 +0300 Subject: First step towards crate deps --- crates/ra_db/src/input.rs | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) (limited to 'crates/ra_db') diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index b4fbb3787..7d9faa43c 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -14,24 +14,51 @@ pub struct CrateId(pub u32); #[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct CrateGraph { - crate_roots: FxHashMap, + arena: FxHashMap, } -impl CrateGraph { - pub fn crate_root(&self, crate_id: CrateId) -> FileId { - self.crate_roots[&crate_id] +#[derive(Debug, Clone, PartialEq, Eq)] +struct CrateData { + file_id: FileId, + deps: Vec, +} + +impl CrateData { + fn new(file_id: FileId) -> CrateData { + CrateData { + file_id, + deps: Vec::new(), + } + } + + fn add_dep(&mut self, dep: CrateId) { + self.deps.push(Dependency { crate_: dep }) } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Dependency { + crate_: CrateId, +} + +impl CrateGraph { pub fn add_crate_root(&mut self, file_id: FileId) -> CrateId { - let crate_id = CrateId(self.crate_roots.len() as u32); - let prev = self.crate_roots.insert(crate_id, file_id); + let crate_id = CrateId(self.arena.len() as u32); + let prev = self.arena.insert(crate_id, CrateData::new(file_id)); assert!(prev.is_none()); crate_id } + pub fn add_dep(&mut self, from: CrateId, to: CrateId) { + self.arena.get_mut(&from).unwrap().add_dep(to) + } + pub fn crate_root(&self, crate_id: CrateId) -> FileId { + self.arena[&crate_id].file_id + } pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option { let (&crate_id, _) = self - .crate_roots + .arena .iter() - .find(|(_crate_id, &root_id)| root_id == file_id)?; + .find(|(_crate_id, data)| data.file_id == file_id)?; Some(crate_id) } } -- cgit v1.2.3