aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_arena/src/map.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-03-19 17:47:43 +0000
committerGitHub <[email protected]>2020-03-19 17:47:43 +0000
commit1ba03c6995015b3143a417ed07437f0c9028a97d (patch)
treece3eb047dd9fe9005750a3b1417d95b1aa8fe01e /crates/ra_arena/src/map.rs
parent988f1dda6bde576ec2457dd97a7525014609c771 (diff)
parentf840fcb2f525c13809d6a736e434155edf075a06 (diff)
Merge #3656
3656: Simplify arenas r=matklad a=matklad At the moment, Arena is paranetrized by two types: index and data. The original motivation was to allow index to be defined in the downstream crate, so that you can add inherent impls to the index. However, it seems like we've never actually used that capability, so perhaps we should switch to a generic Index impl? This PR tries this out, switching only `raw.rs` and parts of `hir_def`. wdyt? Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_arena/src/map.rs')
-rw-r--r--crates/ra_arena/src/map.rs32
1 files changed, 16 insertions, 16 deletions
diff --git a/crates/ra_arena/src/map.rs b/crates/ra_arena/src/map.rs
index b73d4e365..5e764113d 100644
--- a/crates/ra_arena/src/map.rs
+++ b/crates/ra_arena/src/map.rs
@@ -2,17 +2,17 @@
2 2
3use std::marker::PhantomData; 3use std::marker::PhantomData;
4 4
5use super::ArenaId; 5use crate::Idx;
6 6
7/// A map from arena IDs to some other type. Space requirement is O(highest ID). 7/// A map from arena IDs to some other type. Space requirement is O(highest ID).
8#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] 8#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
9pub struct ArenaMap<ID, T> { 9pub struct ArenaMap<ID, V> {
10 v: Vec<Option<T>>, 10 v: Vec<Option<V>>,
11 _ty: PhantomData<ID>, 11 _ty: PhantomData<ID>,
12} 12}
13 13
14impl<ID: ArenaId, T> ArenaMap<ID, T> { 14impl<T, V> ArenaMap<Idx<T>, V> {
15 pub fn insert(&mut self, id: ID, t: T) { 15 pub fn insert(&mut self, id: Idx<T>, t: V) {
16 let idx = Self::to_idx(id); 16 let idx = Self::to_idx(id);
17 if self.v.capacity() <= idx { 17 if self.v.capacity() <= idx {
18 self.v.reserve(idx + 1 - self.v.capacity()); 18 self.v.reserve(idx + 1 - self.v.capacity());
@@ -25,43 +25,43 @@ impl<ID: ArenaId, T> ArenaMap<ID, T> {
25 self.v[idx] = Some(t); 25 self.v[idx] = Some(t);
26 } 26 }
27 27
28 pub fn get(&self, id: ID) -> Option<&T> { 28 pub fn get(&self, id: Idx<T>) -> Option<&V> {
29 self.v.get(Self::to_idx(id)).and_then(|it| it.as_ref()) 29 self.v.get(Self::to_idx(id)).and_then(|it| it.as_ref())
30 } 30 }
31 31
32 pub fn get_mut(&mut self, id: ID) -> Option<&mut T> { 32 pub fn get_mut(&mut self, id: Idx<T>) -> Option<&mut V> {
33 self.v.get_mut(Self::to_idx(id)).and_then(|it| it.as_mut()) 33 self.v.get_mut(Self::to_idx(id)).and_then(|it| it.as_mut())
34 } 34 }
35 35
36 pub fn values(&self) -> impl Iterator<Item = &T> { 36 pub fn values(&self) -> impl Iterator<Item = &V> {
37 self.v.iter().filter_map(|o| o.as_ref()) 37 self.v.iter().filter_map(|o| o.as_ref())
38 } 38 }
39 39
40 pub fn values_mut(&mut self) -> impl Iterator<Item = &mut T> { 40 pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V> {
41 self.v.iter_mut().filter_map(|o| o.as_mut()) 41 self.v.iter_mut().filter_map(|o| o.as_mut())
42 } 42 }
43 43
44 pub fn iter(&self) -> impl Iterator<Item = (ID, &T)> { 44 pub fn iter(&self) -> impl Iterator<Item = (Idx<T>, &V)> {
45 self.v.iter().enumerate().filter_map(|(idx, o)| Some((Self::from_idx(idx), o.as_ref()?))) 45 self.v.iter().enumerate().filter_map(|(idx, o)| Some((Self::from_idx(idx), o.as_ref()?)))
46 } 46 }
47 47
48 fn to_idx(id: ID) -> usize { 48 fn to_idx(id: Idx<T>) -> usize {
49 u32::from(id.into_raw()) as usize 49 u32::from(id.into_raw()) as usize
50 } 50 }
51 51
52 fn from_idx(idx: usize) -> ID { 52 fn from_idx(idx: usize) -> Idx<T> {
53 ID::from_raw((idx as u32).into()) 53 Idx::from_raw((idx as u32).into())
54 } 54 }
55} 55}
56 56
57impl<ID: ArenaId, T> std::ops::Index<ID> for ArenaMap<ID, T> { 57impl<T, V> std::ops::Index<Idx<V>> for ArenaMap<Idx<V>, T> {
58 type Output = T; 58 type Output = T;
59 fn index(&self, id: ID) -> &T { 59 fn index(&self, id: Idx<V>) -> &T {
60 self.v[Self::to_idx(id)].as_ref().unwrap() 60 self.v[Self::to_idx(id)].as_ref().unwrap()
61 } 61 }
62} 62}
63 63
64impl<ID, T> Default for ArenaMap<ID, T> { 64impl<T, V> Default for ArenaMap<Idx<V>, T> {
65 fn default() -> Self { 65 fn default() -> Self {
66 ArenaMap { v: Vec::new(), _ty: PhantomData } 66 ArenaMap { v: Vec::new(), _ty: PhantomData }
67 } 67 }