@@ -14,24 +14,51 @@ pub struct CrateId(pub u32);
14
14
15
15
#[ derive( Debug , Clone , Default , PartialEq , Eq ) ]
16
16
pub struct CrateGraph {
17
- crate_roots : FxHashMap < CrateId , FileId > ,
17
+ arena : FxHashMap < CrateId , CrateData > ,
18
18
}
19
19
20
- impl CrateGraph {
21
- pub fn crate_root ( & self , crate_id : CrateId ) -> FileId {
22
- self . crate_roots [ & crate_id]
20
+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
21
+ struct CrateData {
22
+ file_id : FileId ,
23
+ deps : Vec < Dependency > ,
24
+ }
25
+
26
+ impl 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 ) ]
40
+ pub struct Dependency {
41
+ crate_ : CrateId ,
42
+ }
43
+
44
+ impl CrateGraph {
24
45
pub fn add_crate_root ( & mut self , file_id : FileId ) -> CrateId {
25
- let crate_id = CrateId ( self . crate_roots . len ( ) as u32 ) ;
26
- let prev = self . crate_roots . insert ( crate_id, file_id) ;
46
+ let crate_id = CrateId ( self . arena . len ( ) as u32 ) ;
47
+ let prev = self . arena . insert ( crate_id, CrateData :: new ( file_id) ) ;
27
48
assert ! ( prev. is_none( ) ) ;
28
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
57
pub fn crate_id_for_crate_root ( & self , file_id : FileId ) -> Option < CrateId > {
31
58
let ( & crate_id, _) = self
32
- . crate_roots
59
+ . arena
33
60
. iter ( )
34
- . find ( |( _crate_id, & root_id ) | root_id == file_id) ?;
61
+ . find ( |( _crate_id, data ) | data . file_id == file_id) ?;
35
62
Some ( crate_id)
36
63
}
37
64
}
0 commit comments