aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-12-05 13:01:18 +0000
committerAleksey Kladov <[email protected]>2018-12-05 13:01:18 +0000
commit0b1c0ee225a075d25d700fcd1c007615556e2afb (patch)
tree92b7da90af3504bdb9d1b9c408e61cc405105f04 /crates
parent7ef1697df84696367f8ee6302febabcb50836a55 (diff)
First step towards crate deps
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_db/src/input.rs43
1 files changed, 35 insertions, 8 deletions
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);
14 14
15#[derive(Debug, Clone, Default, PartialEq, Eq)] 15#[derive(Debug, Clone, Default, PartialEq, Eq)]
16pub struct CrateGraph { 16pub struct CrateGraph {
17 crate_roots: FxHashMap<CrateId, FileId>, 17 arena: FxHashMap<CrateId, CrateData>,
18} 18}
19 19
20impl CrateGraph { 20#[derive(Debug, Clone, PartialEq, Eq)]
21 pub fn crate_root(&self, crate_id: CrateId) -> FileId { 21struct CrateData {
22 self.crate_roots[&crate_id] 22 file_id: FileId,
23 deps: Vec<Dependency>,
24}
25
26impl CrateData {
27 fn new(file_id: FileId) -> CrateData {
28 CrateData {
29 file_id,
30 deps: Vec::new(),
31 }
32 }
33
34 fn add_dep(&mut self, dep: CrateId) {
35 self.deps.push(Dependency { crate_: dep })
23 } 36 }
37}
38
39#[derive(Debug, Clone, PartialEq, Eq)]
40pub struct Dependency {
41 crate_: CrateId,
42}
43
44impl CrateGraph {
24 pub fn add_crate_root(&mut self, file_id: FileId) -> CrateId { 45 pub fn add_crate_root(&mut self, file_id: FileId) -> CrateId {
25 let crate_id = CrateId(self.crate_roots.len() as u32); 46 let crate_id = CrateId(self.arena.len() as u32);
26 let prev = self.crate_roots.insert(crate_id, file_id); 47 let prev = self.arena.insert(crate_id, CrateData::new(file_id));
27 assert!(prev.is_none()); 48 assert!(prev.is_none());
28 crate_id 49 crate_id
29 } 50 }
51 pub fn add_dep(&mut self, from: CrateId, to: CrateId) {
52 self.arena.get_mut(&from).unwrap().add_dep(to)
53 }
54 pub fn crate_root(&self, crate_id: CrateId) -> FileId {
55 self.arena[&crate_id].file_id
56 }
30 pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> { 57 pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> {
31 let (&crate_id, _) = self 58 let (&crate_id, _) = self
32 .crate_roots 59 .arena
33 .iter() 60 .iter()
34 .find(|(_crate_id, &root_id)| root_id == file_id)?; 61 .find(|(_crate_id, data)| data.file_id == file_id)?;
35 Some(crate_id) 62 Some(crate_id)
36 } 63 }
37} 64}