@@ -86,7 +86,7 @@ pub struct CrateId(pub u32);
8686pub struct CrateName ( SmolStr ) ;
8787
8888impl CrateName {
89- /// Crates a crate name, checking for dashes in the string provided.
89+ /// Creates a crate name, checking for dashes in the string provided.
9090 /// Dashes are not allowed in the crate names,
9191 /// hence the input string is returned as `Err` for those cases.
9292 pub fn new ( name : & str ) -> Result < CrateName , & str > {
@@ -97,19 +97,23 @@ impl CrateName {
9797 }
9898 }
9999
100- /// Crates a crate name, unconditionally replacing the dashes with underscores.
100+ /// Creates a crate name, unconditionally replacing the dashes with underscores.
101101 pub fn normalize_dashes ( name : & str ) -> CrateName {
102102 Self ( SmolStr :: new ( name. replace ( '-' , "_" ) ) )
103103 }
104104}
105105
106106#[ derive( Debug , Clone , PartialEq , Eq ) ]
107- struct CrateData {
108- file_id : FileId ,
109- edition : Edition ,
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 > ,
110114 cfg_options : CfgOptions ,
111115 env : Env ,
112- dependencies : Vec < Dependency > ,
116+ pub dependencies : Vec < Dependency > ,
113117}
114118
115119#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
@@ -134,10 +138,11 @@ impl CrateGraph {
134138 & mut self ,
135139 file_id : FileId ,
136140 edition : Edition ,
141+ display_name : Option < String > ,
137142 cfg_options : CfgOptions ,
138143 env : Env ,
139144 ) -> CrateId {
140- let data = CrateData :: new ( file_id, edition, cfg_options, env) ;
145+ let data = CrateData :: new ( file_id, edition, display_name , cfg_options, env) ;
141146 let crate_id = CrateId ( self . arena . len ( ) as u32 ) ;
142147 let prev = self . arena . insert ( crate_id, data) ;
143148 assert ! ( prev. is_none( ) ) ;
@@ -169,24 +174,17 @@ impl CrateGraph {
169174 self . arena . keys ( ) . copied ( )
170175 }
171176
172- pub fn crate_root ( & self , crate_id : CrateId ) -> FileId {
173- self . arena [ & crate_id] . file_id
174- }
175-
176- pub fn edition ( & self , crate_id : CrateId ) -> Edition {
177- self . arena [ & crate_id] . edition
177+ pub fn crate_data ( & self , crate_id : & CrateId ) -> & CrateData {
178+ & self . arena [ crate_id]
178179 }
179180
180181 // FIXME: this only finds one crate with the given root; we could have multiple
181182 pub fn crate_id_for_crate_root ( & self , file_id : FileId ) -> Option < CrateId > {
182- 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) ?;
183185 Some ( crate_id)
184186 }
185187
186- pub fn dependencies ( & self , crate_id : CrateId ) -> impl Iterator < Item = & Dependency > {
187- self . arena [ & crate_id] . dependencies . iter ( )
188- }
189-
190188 /// Extends this crate graph by adding a complete disjoint second crate
191189 /// graph.
192190 ///
@@ -209,7 +207,7 @@ impl CrateGraph {
209207 return false ;
210208 }
211209
212- for dep in self . dependencies ( from) {
210+ for dep in & self . crate_data ( & from) . dependencies {
213211 let crate_id = dep. crate_id ( ) ;
214212 if crate_id == target {
215213 return true ;
@@ -230,8 +228,21 @@ impl CrateId {
230228}
231229
232230impl CrateData {
233- fn new ( file_id : FileId , edition : Edition , cfg_options : CfgOptions , env : Env ) -> CrateData {
234- CrateData { file_id, edition, dependencies : Vec :: new ( ) , cfg_options, env }
231+ fn new (
232+ root_file_id : FileId ,
233+ edition : Edition ,
234+ display_name : Option < String > ,
235+ cfg_options : CfgOptions ,
236+ env : Env ,
237+ ) -> CrateData {
238+ CrateData {
239+ root_file_id,
240+ edition,
241+ display_name,
242+ dependencies : Vec :: new ( ) ,
243+ cfg_options,
244+ env,
245+ }
235246 }
236247
237248 fn add_dep ( & mut self , name : SmolStr , crate_id : CrateId ) {
@@ -290,12 +301,27 @@ mod tests {
290301 #[ test]
291302 fn it_should_panic_because_of_cycle_dependencies ( ) {
292303 let mut graph = CrateGraph :: default ( ) ;
293- let crate1 =
294- graph. add_crate_root ( FileId ( 1u32 ) , Edition2018 , CfgOptions :: default ( ) , Env :: default ( ) ) ;
295- let crate2 =
296- graph. add_crate_root ( FileId ( 2u32 ) , Edition2018 , CfgOptions :: default ( ) , Env :: default ( ) ) ;
297- let crate3 =
298- graph. add_crate_root ( FileId ( 3u32 ) , Edition2018 , CfgOptions :: default ( ) , Env :: default ( ) ) ;
304+ let crate1 = graph. add_crate_root (
305+ FileId ( 1u32 ) ,
306+ Edition2018 ,
307+ None ,
308+ CfgOptions :: default ( ) ,
309+ Env :: default ( ) ,
310+ ) ;
311+ let crate2 = graph. add_crate_root (
312+ FileId ( 2u32 ) ,
313+ Edition2018 ,
314+ None ,
315+ CfgOptions :: default ( ) ,
316+ Env :: default ( ) ,
317+ ) ;
318+ let crate3 = graph. add_crate_root (
319+ FileId ( 3u32 ) ,
320+ Edition2018 ,
321+ None ,
322+ CfgOptions :: default ( ) ,
323+ Env :: default ( ) ,
324+ ) ;
299325 assert ! ( graph. add_dep( crate1, CrateName :: new( "crate2" ) . unwrap( ) , crate2) . is_ok( ) ) ;
300326 assert ! ( graph. add_dep( crate2, CrateName :: new( "crate3" ) . unwrap( ) , crate3) . is_ok( ) ) ;
301327 assert ! ( graph. add_dep( crate3, CrateName :: new( "crate1" ) . unwrap( ) , crate1) . is_err( ) ) ;
@@ -304,29 +330,54 @@ mod tests {
304330 #[ test]
305331 fn it_works ( ) {
306332 let mut graph = CrateGraph :: default ( ) ;
307- let crate1 =
308- graph. add_crate_root ( FileId ( 1u32 ) , Edition2018 , CfgOptions :: default ( ) , Env :: default ( ) ) ;
309- let crate2 =
310- graph. add_crate_root ( FileId ( 2u32 ) , Edition2018 , CfgOptions :: default ( ) , Env :: default ( ) ) ;
311- let crate3 =
312- graph. add_crate_root ( FileId ( 3u32 ) , Edition2018 , CfgOptions :: default ( ) , Env :: default ( ) ) ;
333+ let crate1 = graph. add_crate_root (
334+ FileId ( 1u32 ) ,
335+ Edition2018 ,
336+ None ,
337+ CfgOptions :: default ( ) ,
338+ Env :: default ( ) ,
339+ ) ;
340+ let crate2 = graph. add_crate_root (
341+ FileId ( 2u32 ) ,
342+ Edition2018 ,
343+ None ,
344+ CfgOptions :: default ( ) ,
345+ Env :: default ( ) ,
346+ ) ;
347+ let crate3 = graph. add_crate_root (
348+ FileId ( 3u32 ) ,
349+ Edition2018 ,
350+ None ,
351+ CfgOptions :: default ( ) ,
352+ Env :: default ( ) ,
353+ ) ;
313354 assert ! ( graph. add_dep( crate1, CrateName :: new( "crate2" ) . unwrap( ) , crate2) . is_ok( ) ) ;
314355 assert ! ( graph. add_dep( crate2, CrateName :: new( "crate3" ) . unwrap( ) , crate3) . is_ok( ) ) ;
315356 }
316357
317358 #[ test]
318359 fn dashes_are_normalized ( ) {
319360 let mut graph = CrateGraph :: default ( ) ;
320- let crate1 =
321- graph. add_crate_root ( FileId ( 1u32 ) , Edition2018 , CfgOptions :: default ( ) , Env :: default ( ) ) ;
322- let crate2 =
323- graph. add_crate_root ( FileId ( 2u32 ) , Edition2018 , CfgOptions :: default ( ) , Env :: default ( ) ) ;
361+ let crate1 = graph. add_crate_root (
362+ FileId ( 1u32 ) ,
363+ Edition2018 ,
364+ None ,
365+ CfgOptions :: default ( ) ,
366+ Env :: default ( ) ,
367+ ) ;
368+ let crate2 = graph. add_crate_root (
369+ FileId ( 2u32 ) ,
370+ Edition2018 ,
371+ None ,
372+ CfgOptions :: default ( ) ,
373+ Env :: default ( ) ,
374+ ) ;
324375 assert ! ( graph
325376 . add_dep( crate1, CrateName :: normalize_dashes( "crate-name-with-dashes" ) , crate2)
326377 . is_ok( ) ) ;
327378 assert_eq ! (
328- graph. dependencies ( crate1) . collect :: < Vec <_>> ( ) ,
329- 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( ) } ]
330381 ) ;
331382 }
332383}
0 commit comments