aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/arena.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_analysis/src/arena.rs')
-rw-r--r--crates/ra_analysis/src/arena.rs56
1 files changed, 13 insertions, 43 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
5use std::{ 5use 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)]
44pub(crate) struct Arena<T> { 43pub(crate) struct ArenaBehavior<T> {
45 data: Vec<T>, 44 _ty: PhantomData<T>,
46} 45}
47 46
48impl<T> Default for Arena<T> { 47impl<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
54impl<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
85impl<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
92impl<T> IndexMut<Id<T>> for Arena<T> { 66pub(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}