diff options
author | Aleksey Kladov <[email protected]> | 2018-11-25 21:46:13 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-11-27 11:23:06 +0000 |
commit | ed023929d5f3a53a3989371452291176d3ce543c (patch) | |
tree | 96b005cb9dd8e6ba304310a3358601a3e4a0273c /crates/ra_analysis/src | |
parent | 8a572043e7effe1d0b36a0d247960111312edc2a (diff) |
Switch to id-arena
Diffstat (limited to 'crates/ra_analysis/src')
-rw-r--r-- | crates/ra_analysis/src/arena.rs | 56 | ||||
-rw-r--r-- | crates/ra_analysis/src/descriptors/function/scope.rs | 4 | ||||
-rw-r--r-- | crates/ra_analysis/src/descriptors/module/mod.rs | 8 |
3 files changed, 19 insertions, 49 deletions
diff --git a/crates/ra_analysis/src/arena.rs b/crates/ra_analysis/src/arena.rs index 98ed89274..a752ec0c1 100644 --- a/crates/ra_analysis/src/arena.rs +++ b/crates/ra_analysis/src/arena.rs | |||
@@ -4,7 +4,6 @@ | |||
4 | 4 | ||
5 | use std::{ | 5 | use std::{ |
6 | fmt, | 6 | fmt, |
7 | ops::{Index, IndexMut}, | ||
8 | hash::{Hash, Hasher}, | 7 | hash::{Hash, Hasher}, |
9 | marker::PhantomData, | 8 | marker::PhantomData, |
10 | }; | 9 | }; |
@@ -41,56 +40,27 @@ impl<T> Hash for Id<T> { | |||
41 | } | 40 | } |
42 | 41 | ||
43 | #[derive(Debug, PartialEq, Eq)] | 42 | #[derive(Debug, PartialEq, Eq)] |
44 | pub(crate) struct Arena<T> { | 43 | pub(crate) struct ArenaBehavior<T> { |
45 | data: Vec<T>, | 44 | _ty: PhantomData<T>, |
46 | } | 45 | } |
47 | 46 | ||
48 | impl<T> Default for Arena<T> { | 47 | impl<T> id_arena::ArenaBehavior for ArenaBehavior<T> { |
49 | fn default() -> Arena<T> { | 48 | type Id = Id<T>; |
50 | Arena { data: Vec::new() } | 49 | fn new_arena_id() -> usize { |
50 | 0 | ||
51 | } | 51 | } |
52 | } | 52 | fn new_id(_arena_id: usize, index: usize) -> Id<T> { |
53 | |||
54 | impl<T> Arena<T> { | ||
55 | pub(crate) fn push(&mut self, value: T) -> Id<T> { | ||
56 | let id = self.data.len() as u32; | ||
57 | self.data.push(value); | ||
58 | Id { | 53 | Id { |
59 | idx: id as u32, | 54 | idx: index as u32, |
60 | _ty: PhantomData, | 55 | _ty: PhantomData, |
61 | } | 56 | } |
62 | } | 57 | } |
63 | 58 | fn index(id: Id<T>) -> usize { | |
64 | pub(crate) fn keys<'a>(&'a self) -> impl Iterator<Item = Id<T>> + 'a { | 59 | id.idx as usize |
65 | (0..(self.data.len() as u32)).into_iter().map(|idx| Id { | ||
66 | idx, | ||
67 | _ty: PhantomData, | ||
68 | }) | ||
69 | } | ||
70 | |||
71 | pub(crate) fn items<'a>(&'a self) -> impl Iterator<Item = (Id<T>, &T)> + 'a { | ||
72 | self.data.iter().enumerate().map(|(idx, item)| { | ||
73 | let idx = idx as u32; | ||
74 | ( | ||
75 | Id { | ||
76 | idx, | ||
77 | _ty: PhantomData, | ||
78 | }, | ||
79 | item, | ||
80 | ) | ||
81 | }) | ||
82 | } | 60 | } |
83 | } | 61 | fn arena_id(_id: Id<T>) -> usize { |
84 | 62 | 0 | |
85 | impl<T> Index<Id<T>> for Arena<T> { | ||
86 | type Output = T; | ||
87 | fn index(&self, id: Id<T>) -> &T { | ||
88 | &self.data[id.idx as usize] | ||
89 | } | 63 | } |
90 | } | 64 | } |
91 | 65 | ||
92 | impl<T> IndexMut<Id<T>> for Arena<T> { | 66 | pub(crate) type Arena<T> = id_arena::Arena<T, ArenaBehavior<T>>; |
93 | fn index_mut(&mut self, id: Id<T>) -> &mut T { | ||
94 | &mut self.data[id.idx as usize] | ||
95 | } | ||
96 | } | ||
diff --git a/crates/ra_analysis/src/descriptors/function/scope.rs b/crates/ra_analysis/src/descriptors/function/scope.rs index 54d7fa456..5307a0a8e 100644 --- a/crates/ra_analysis/src/descriptors/function/scope.rs +++ b/crates/ra_analysis/src/descriptors/function/scope.rs | |||
@@ -58,13 +58,13 @@ impl FnScopes { | |||
58 | }) | 58 | }) |
59 | } | 59 | } |
60 | fn root_scope(&mut self) -> ScopeId { | 60 | fn root_scope(&mut self) -> ScopeId { |
61 | self.scopes.push(ScopeData { | 61 | self.scopes.alloc(ScopeData { |
62 | parent: None, | 62 | parent: None, |
63 | entries: vec![], | 63 | entries: vec![], |
64 | }) | 64 | }) |
65 | } | 65 | } |
66 | fn new_scope(&mut self, parent: ScopeId) -> ScopeId { | 66 | fn new_scope(&mut self, parent: ScopeId) -> ScopeId { |
67 | self.scopes.push(ScopeData { | 67 | self.scopes.alloc(ScopeData { |
68 | parent: Some(parent), | 68 | parent: Some(parent), |
69 | entries: vec![], | 69 | entries: vec![], |
70 | }) | 70 | }) |
diff --git a/crates/ra_analysis/src/descriptors/module/mod.rs b/crates/ra_analysis/src/descriptors/module/mod.rs index 54ff95b66..a6eaec178 100644 --- a/crates/ra_analysis/src/descriptors/module/mod.rs +++ b/crates/ra_analysis/src/descriptors/module/mod.rs | |||
@@ -166,12 +166,12 @@ pub(crate) struct ModuleTree { | |||
166 | 166 | ||
167 | impl ModuleTree { | 167 | impl ModuleTree { |
168 | fn modules<'a>(&'a self) -> impl Iterator<Item = ModuleId> + 'a { | 168 | fn modules<'a>(&'a self) -> impl Iterator<Item = ModuleId> + 'a { |
169 | self.mods.keys() | 169 | self.mods.iter().map(|(id, _)| id) |
170 | } | 170 | } |
171 | 171 | ||
172 | fn modules_for_source(&self, source: ModuleSource) -> Vec<ModuleId> { | 172 | fn modules_for_source(&self, source: ModuleSource) -> Vec<ModuleId> { |
173 | self.mods | 173 | self.mods |
174 | .items() | 174 | .iter() |
175 | .filter(|(_idx, it)| it.source == source) | 175 | .filter(|(_idx, it)| it.source == source) |
176 | .map(|(idx, _)| idx) | 176 | .map(|(idx, _)| idx) |
177 | .collect() | 177 | .collect() |
@@ -333,11 +333,11 @@ struct LinkData { | |||
333 | 333 | ||
334 | impl ModuleTree { | 334 | impl ModuleTree { |
335 | fn push_mod(&mut self, data: ModuleData) -> ModuleId { | 335 | fn push_mod(&mut self, data: ModuleData) -> ModuleId { |
336 | self.mods.push(data) | 336 | self.mods.alloc(data) |
337 | } | 337 | } |
338 | fn push_link(&mut self, data: LinkData) -> LinkId { | 338 | fn push_link(&mut self, data: LinkData) -> LinkId { |
339 | let owner = data.owner; | 339 | let owner = data.owner; |
340 | let id = self.links.push(data); | 340 | let id = self.links.alloc(data); |
341 | self.mods[owner].children.push(id); | 341 | self.mods[owner].children.push(id); |
342 | id | 342 | id |
343 | } | 343 | } |