@@ -104,13 +104,16 @@ impl CrateName {
104104}
105105
106106#[ derive( Debug , Clone , PartialEq , Eq ) ]
107- struct CrateData {
108- file_id : FileId ,
109- edition : Edition ,
110- declaration_name : Option < String > ,
107+ pub struct CrateData {
108+ pub root_file_id : FileId ,
109+ pub edition : Edition ,
110+ /// The name to display to the end user.
111+ /// This actual crate name can be different in a particular dependent crate
112+ /// or may even be missing for some cases, such as a dummy crate for the code snippet.
113+ pub display_name : Option < String > ,
111114 cfg_options : CfgOptions ,
112115 env : Env ,
113- dependencies : Vec < Dependency > ,
116+ pub dependencies : Vec < Dependency > ,
114117}
115118
116119#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
@@ -135,11 +138,11 @@ impl CrateGraph {
135138 & mut self ,
136139 file_id : FileId ,
137140 edition : Edition ,
138- declaration_name : Option < String > ,
141+ display_name : Option < String > ,
139142 cfg_options : CfgOptions ,
140143 env : Env ,
141144 ) -> CrateId {
142- let data = CrateData :: new ( file_id, edition, declaration_name , cfg_options, env) ;
145+ let data = CrateData :: new ( file_id, edition, display_name , cfg_options, env) ;
143146 let crate_id = CrateId ( self . arena . len ( ) as u32 ) ;
144147 let prev = self . arena . insert ( crate_id, data) ;
145148 assert ! ( prev. is_none( ) ) ;
@@ -171,33 +174,17 @@ impl CrateGraph {
171174 self . arena . keys ( ) . copied ( )
172175 }
173176
174- pub fn crate_root ( & self , crate_id : CrateId ) -> FileId {
175- self . arena [ & crate_id] . file_id
176- }
177-
178- pub fn edition ( & self , crate_id : CrateId ) -> Edition {
179- self . arena [ & crate_id] . edition
180- }
181-
182- /// Returns a name of a crate, declared in the root project.
183- /// May be missing for some cases, such as when the crate definition was created for a code snippet.
184- ///
185- /// This should not be considered as a normal crate name, since the actual name can be different in
186- /// a particular dependent crate, where it is specified.
187- pub fn declaration_name ( & self , crate_id : & CrateId ) -> Option < & String > {
188- self . arena [ crate_id] . declaration_name . as_ref ( )
177+ pub fn crate_data ( & self , crate_id : & CrateId ) -> & CrateData {
178+ & self . arena [ crate_id]
189179 }
190180
191181 // FIXME: this only finds one crate with the given root; we could have multiple
192182 pub fn crate_id_for_crate_root ( & self , file_id : FileId ) -> Option < CrateId > {
193- let ( & crate_id, _) = self . arena . iter ( ) . find ( |( _crate_id, data) | data. file_id == file_id) ?;
183+ let ( & crate_id, _) =
184+ self . arena . iter ( ) . find ( |( _crate_id, data) | data. root_file_id == file_id) ?;
194185 Some ( crate_id)
195186 }
196187
197- pub fn dependencies ( & self , crate_id : CrateId ) -> impl Iterator < Item = & Dependency > {
198- self . arena [ & crate_id] . dependencies . iter ( )
199- }
200-
201188 /// Extends this crate graph by adding a complete disjoint second crate
202189 /// graph.
203190 ///
@@ -220,7 +207,7 @@ impl CrateGraph {
220207 return false ;
221208 }
222209
223- for dep in self . dependencies ( from) {
210+ for dep in & self . crate_data ( & from) . dependencies {
224211 let crate_id = dep. crate_id ( ) ;
225212 if crate_id == target {
226213 return true ;
@@ -242,13 +229,20 @@ impl CrateId {
242229
243230impl CrateData {
244231 fn new (
245- file_id : FileId ,
232+ root_file_id : FileId ,
246233 edition : Edition ,
247- declaration_name : Option < String > ,
234+ display_name : Option < String > ,
248235 cfg_options : CfgOptions ,
249236 env : Env ,
250237 ) -> CrateData {
251- CrateData { file_id, edition, declaration_name, dependencies : Vec :: new ( ) , cfg_options, env }
238+ CrateData {
239+ root_file_id,
240+ edition,
241+ display_name,
242+ dependencies : Vec :: new ( ) ,
243+ cfg_options,
244+ env,
245+ }
252246 }
253247
254248 fn add_dep ( & mut self , name : SmolStr , crate_id : CrateId ) {
@@ -382,8 +376,8 @@ mod tests {
382376 . add_dep( crate1, CrateName :: normalize_dashes( "crate-name-with-dashes" ) , crate2)
383377 . is_ok( ) ) ;
384378 assert_eq ! (
385- graph. dependencies ( crate1) . collect :: < Vec <_>> ( ) ,
386- vec![ & Dependency { crate_id: crate2, name: "crate_name_with_dashes" . into( ) } ]
379+ graph. crate_data ( & crate1) . dependencies ,
380+ vec![ Dependency { crate_id: crate2, name: "crate_name_with_dashes" . into( ) } ]
387381 ) ;
388382 }
389383}
0 commit comments