@@ -86,7 +86,7 @@ pub struct CrateId(pub u32);
86
86
pub struct CrateName ( SmolStr ) ;
87
87
88
88
impl 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.
90
90
/// Dashes are not allowed in the crate names,
91
91
/// hence the input string is returned as `Err` for those cases.
92
92
pub fn new ( name : & str ) -> Result < CrateName , & str > {
@@ -97,19 +97,23 @@ impl CrateName {
97
97
}
98
98
}
99
99
100
- /// Crates a crate name, unconditionally replacing the dashes with underscores.
100
+ /// Creates a crate name, unconditionally replacing the dashes with underscores.
101
101
pub fn normalize_dashes ( name : & str ) -> CrateName {
102
102
Self ( SmolStr :: new ( name. replace ( '-' , "_" ) ) )
103
103
}
104
104
}
105
105
106
106
#[ 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 > ,
110
114
cfg_options : CfgOptions ,
111
115
env : Env ,
112
- dependencies : Vec < Dependency > ,
116
+ pub dependencies : Vec < Dependency > ,
113
117
}
114
118
115
119
#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
@@ -134,10 +138,11 @@ impl CrateGraph {
134
138
& mut self ,
135
139
file_id : FileId ,
136
140
edition : Edition ,
141
+ display_name : Option < String > ,
137
142
cfg_options : CfgOptions ,
138
143
env : Env ,
139
144
) -> 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) ;
141
146
let crate_id = CrateId ( self . arena . len ( ) as u32 ) ;
142
147
let prev = self . arena . insert ( crate_id, data) ;
143
148
assert ! ( prev. is_none( ) ) ;
@@ -169,24 +174,17 @@ impl CrateGraph {
169
174
self . arena . keys ( ) . copied ( )
170
175
}
171
176
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]
178
179
}
179
180
180
181
// FIXME: this only finds one crate with the given root; we could have multiple
181
182
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) ?;
183
185
Some ( crate_id)
184
186
}
185
187
186
- pub fn dependencies ( & self , crate_id : CrateId ) -> impl Iterator < Item = & Dependency > {
187
- self . arena [ & crate_id] . dependencies . iter ( )
188
- }
189
-
190
188
/// Extends this crate graph by adding a complete disjoint second crate
191
189
/// graph.
192
190
///
@@ -209,7 +207,7 @@ impl CrateGraph {
209
207
return false ;
210
208
}
211
209
212
- for dep in self . dependencies ( from) {
210
+ for dep in & self . crate_data ( & from) . dependencies {
213
211
let crate_id = dep. crate_id ( ) ;
214
212
if crate_id == target {
215
213
return true ;
@@ -230,8 +228,21 @@ impl CrateId {
230
228
}
231
229
232
230
impl 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
+ }
235
246
}
236
247
237
248
fn add_dep ( & mut self , name : SmolStr , crate_id : CrateId ) {
@@ -290,12 +301,27 @@ mod tests {
290
301
#[ test]
291
302
fn it_should_panic_because_of_cycle_dependencies ( ) {
292
303
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
+ ) ;
299
325
assert ! ( graph. add_dep( crate1, CrateName :: new( "crate2" ) . unwrap( ) , crate2) . is_ok( ) ) ;
300
326
assert ! ( graph. add_dep( crate2, CrateName :: new( "crate3" ) . unwrap( ) , crate3) . is_ok( ) ) ;
301
327
assert ! ( graph. add_dep( crate3, CrateName :: new( "crate1" ) . unwrap( ) , crate1) . is_err( ) ) ;
@@ -304,29 +330,54 @@ mod tests {
304
330
#[ test]
305
331
fn it_works ( ) {
306
332
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
+ ) ;
313
354
assert ! ( graph. add_dep( crate1, CrateName :: new( "crate2" ) . unwrap( ) , crate2) . is_ok( ) ) ;
314
355
assert ! ( graph. add_dep( crate2, CrateName :: new( "crate3" ) . unwrap( ) , crate3) . is_ok( ) ) ;
315
356
}
316
357
317
358
#[ test]
318
359
fn dashes_are_normalized ( ) {
319
360
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
+ ) ;
324
375
assert ! ( graph
325
376
. add_dep( crate1, CrateName :: normalize_dashes( "crate-name-with-dashes" ) , crate2)
326
377
. is_ok( ) ) ;
327
378
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( ) } ]
330
381
) ;
331
382
}
332
383
}
0 commit comments