66//! actual IO. See `vfs` and `project_model` in the `rust-analyzer` crate for how
77//! actual IO is done and lowered to input.
88
9- use std:: { fmt, str:: FromStr } ;
9+ use std:: { fmt, ops , str:: FromStr } ;
1010
1111use ra_cfg:: CfgOptions ;
1212use ra_syntax:: SmolStr ;
@@ -111,8 +111,8 @@ pub struct CrateData {
111111 /// This actual crate name can be different in a particular dependent crate
112112 /// or may even be missing for some cases, such as a dummy crate for the code snippet.
113113 pub display_name : Option < String > ,
114- cfg_options : CfgOptions ,
115- env : Env ,
114+ pub cfg_options : CfgOptions ,
115+ pub env : Env ,
116116 pub dependencies : Vec < Dependency > ,
117117}
118118
@@ -142,17 +142,20 @@ impl CrateGraph {
142142 cfg_options : CfgOptions ,
143143 env : Env ,
144144 ) -> CrateId {
145- let data = CrateData :: new ( file_id, edition, display_name, cfg_options, env) ;
145+ let data = CrateData {
146+ root_file_id : file_id,
147+ edition,
148+ display_name,
149+ cfg_options,
150+ env,
151+ dependencies : Vec :: new ( ) ,
152+ } ;
146153 let crate_id = CrateId ( self . arena . len ( ) as u32 ) ;
147154 let prev = self . arena . insert ( crate_id, data) ;
148155 assert ! ( prev. is_none( ) ) ;
149156 crate_id
150157 }
151158
152- pub fn cfg_options ( & self , crate_id : CrateId ) -> & CfgOptions {
153- & self . arena [ & crate_id] . cfg_options
154- }
155-
156159 pub fn add_dep (
157160 & mut self ,
158161 from : CrateId ,
@@ -174,10 +177,6 @@ impl CrateGraph {
174177 self . arena . keys ( ) . copied ( )
175178 }
176179
177- pub fn crate_data ( & self , crate_id : & CrateId ) -> & CrateData {
178- & self . arena [ crate_id]
179- }
180-
181180 // FIXME: this only finds one crate with the given root; we could have multiple
182181 pub fn crate_id_for_crate_root ( & self , file_id : FileId ) -> Option < CrateId > {
183182 let ( & crate_id, _) =
@@ -207,8 +206,8 @@ impl CrateGraph {
207206 return false ;
208207 }
209208
210- for dep in & self . crate_data ( & from) . dependencies {
211- let crate_id = dep. crate_id ( ) ;
209+ for dep in & self [ from] . dependencies {
210+ let crate_id = dep. crate_id ;
212211 if crate_id == target {
213212 return true ;
214213 }
@@ -221,30 +220,20 @@ impl CrateGraph {
221220 }
222221}
223222
223+ impl ops:: Index < CrateId > for CrateGraph {
224+ type Output = CrateData ;
225+ fn index ( & self , crate_id : CrateId ) -> & CrateData {
226+ & self . arena [ & crate_id]
227+ }
228+ }
229+
224230impl CrateId {
225231 pub fn shift ( self , amount : u32 ) -> CrateId {
226232 CrateId ( self . 0 + amount)
227233 }
228234}
229235
230236impl CrateData {
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- }
246- }
247-
248237 fn add_dep ( & mut self , name : SmolStr , crate_id : CrateId ) {
249238 self . dependencies . push ( Dependency { name, crate_id } )
250239 }
@@ -272,12 +261,6 @@ impl fmt::Display for Edition {
272261 }
273262}
274263
275- impl Dependency {
276- pub fn crate_id ( & self ) -> CrateId {
277- self . crate_id
278- }
279- }
280-
281264#[ derive( Debug ) ]
282265pub struct ParseEditionError {
283266 invalid_input : String ,
@@ -376,7 +359,7 @@ mod tests {
376359 . add_dep( crate1, CrateName :: normalize_dashes( "crate-name-with-dashes" ) , crate2)
377360 . is_ok( ) ) ;
378361 assert_eq ! (
379- graph. crate_data ( & crate1) . dependencies,
362+ graph[ crate1] . dependencies,
380363 vec![ Dependency { crate_id: crate2, name: "crate_name_with_dashes" . into( ) } ]
381364 ) ;
382365 }
0 commit comments