aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_db
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-03-09 10:19:26 +0000
committerGitHub <[email protected]>2020-03-09 10:19:26 +0000
commita6133206d1614624bbb51650d33fae04d70bc21a (patch)
tree461dd502837fe3091e0057f31faa1515fca1ca49 /crates/ra_db
parent0dbd8ff59b854570329c325431126a078505e5f5 (diff)
parent100cbc57ce2bd903ecab7d8bfb0abf7777076510 (diff)
Merge #3527
3527: Simplify r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_db')
-rw-r--r--crates/ra_db/src/input.rs59
1 files changed, 21 insertions, 38 deletions
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs
index 4069c0fed..b77640b2b 100644
--- a/crates/ra_db/src/input.rs
+++ b/crates/ra_db/src/input.rs
@@ -6,7 +6,7 @@
6//! actual IO. See `vfs` and `project_model` in the `rust-analyzer` crate for how 6//! actual IO. See `vfs` and `project_model` in the `rust-analyzer` crate for how
7//! actual IO is done and lowered to input. 7//! actual IO is done and lowered to input.
8 8
9use std::{fmt, str::FromStr}; 9use std::{fmt, ops, str::FromStr};
10 10
11use ra_cfg::CfgOptions; 11use ra_cfg::CfgOptions;
12use ra_syntax::SmolStr; 12use ra_syntax::SmolStr;
@@ -111,8 +111,8 @@ pub struct CrateData {
111 /// This actual crate name can be different in a particular dependent crate 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. 112 /// or may even be missing for some cases, such as a dummy crate for the code snippet.
113 pub display_name: Option<String>, 113 pub display_name: Option<String>,
114 cfg_options: CfgOptions, 114 pub cfg_options: CfgOptions,
115 env: Env, 115 pub env: Env,
116 pub dependencies: Vec<Dependency>, 116 pub dependencies: Vec<Dependency>,
117} 117}
118 118
@@ -142,17 +142,20 @@ impl CrateGraph {
142 cfg_options: CfgOptions, 142 cfg_options: CfgOptions,
143 env: Env, 143 env: Env,
144 ) -> CrateId { 144 ) -> 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 };
146 let crate_id = CrateId(self.arena.len() as u32); 153 let crate_id = CrateId(self.arena.len() as u32);
147 let prev = self.arena.insert(crate_id, data); 154 let prev = self.arena.insert(crate_id, data);
148 assert!(prev.is_none()); 155 assert!(prev.is_none());
149 crate_id 156 crate_id
150 } 157 }
151 158
152 pub fn cfg_options(&self, crate_id: CrateId) -> &CfgOptions {
153 &self.arena[&crate_id].cfg_options
154 }
155
156 pub fn add_dep( 159 pub fn add_dep(
157 &mut self, 160 &mut self,
158 from: CrateId, 161 from: CrateId,
@@ -174,10 +177,6 @@ impl CrateGraph {
174 self.arena.keys().copied() 177 self.arena.keys().copied()
175 } 178 }
176 179
177 pub fn crate_data(&self, crate_id: &CrateId) -> &CrateData {
178 &self.arena[crate_id]
179 }
180
181 // FIXME: this only finds one crate with the given root; we could have multiple 180 // FIXME: this only finds one crate with the given root; we could have multiple
182 pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> { 181 pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> {
183 let (&crate_id, _) = 182 let (&crate_id, _) =
@@ -207,8 +206,8 @@ impl CrateGraph {
207 return false; 206 return false;
208 } 207 }
209 208
210 for dep in &self.crate_data(&from).dependencies { 209 for dep in &self[from].dependencies {
211 let crate_id = dep.crate_id(); 210 let crate_id = dep.crate_id;
212 if crate_id == target { 211 if crate_id == target {
213 return true; 212 return true;
214 } 213 }
@@ -221,6 +220,13 @@ impl CrateGraph {
221 } 220 }
222} 221}
223 222
223impl 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
224impl CrateId { 230impl CrateId {
225 pub fn shift(self, amount: u32) -> CrateId { 231 pub fn shift(self, amount: u32) -> CrateId {
226 CrateId(self.0 + amount) 232 CrateId(self.0 + amount)
@@ -228,23 +234,6 @@ impl CrateId {
228} 234}
229 235
230impl CrateData { 236impl 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
248 fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) { 237 fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) {
249 self.dependencies.push(Dependency { name, crate_id }) 238 self.dependencies.push(Dependency { name, crate_id })
250 } 239 }
@@ -272,12 +261,6 @@ impl fmt::Display for Edition {
272 } 261 }
273} 262}
274 263
275impl Dependency {
276 pub fn crate_id(&self) -> CrateId {
277 self.crate_id
278 }
279}
280
281#[derive(Debug)] 264#[derive(Debug)]
282pub struct ParseEditionError { 265pub struct ParseEditionError {
283 invalid_input: String, 266 invalid_input: String,
@@ -376,7 +359,7 @@ mod tests {
376 .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2) 359 .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2)
377 .is_ok()); 360 .is_ok());
378 assert_eq!( 361 assert_eq!(
379 graph.crate_data(&crate1).dependencies, 362 graph[crate1].dependencies,
380 vec![Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }] 363 vec![Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }]
381 ); 364 );
382 } 365 }