aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/trace.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def/src/trace.rs')
-rw-r--r--crates/ra_hir_def/src/trace.rs42
1 files changed, 26 insertions, 16 deletions
diff --git a/crates/ra_hir_def/src/trace.rs b/crates/ra_hir_def/src/trace.rs
index fc26f5a48..2bcd707bc 100644
--- a/crates/ra_hir_def/src/trace.rs
+++ b/crates/ra_hir_def/src/trace.rs
@@ -12,38 +12,48 @@
12use ra_arena::{map::ArenaMap, Arena, ArenaId, RawId}; 12use ra_arena::{map::ArenaMap, Arena, ArenaId, RawId};
13 13
14pub(crate) struct Trace<ID: ArenaId, T, V> { 14pub(crate) struct Trace<ID: ArenaId, T, V> {
15 for_arena: bool, 15 arena: Option<Arena<ID, T>>,
16 arena: Arena<ID, T>, 16 map: Option<ArenaMap<ID, V>>,
17 map: ArenaMap<ID, V>,
18 len: u32, 17 len: u32,
19} 18}
20 19
21impl<ID: ra_arena::ArenaId, T, V> Trace<ID, T, V> { 20impl<ID: ra_arena::ArenaId + Copy, T, V> Trace<ID, T, V> {
21 pub(crate) fn new() -> Trace<ID, T, V> {
22 Trace { arena: Some(Arena::default()), map: Some(ArenaMap::default()), len: 0 }
23 }
24
22 pub(crate) fn new_for_arena() -> Trace<ID, T, V> { 25 pub(crate) fn new_for_arena() -> Trace<ID, T, V> {
23 Trace { for_arena: true, arena: Arena::default(), map: ArenaMap::default(), len: 0 } 26 Trace { arena: Some(Arena::default()), map: None, len: 0 }
24 } 27 }
25 28
26 pub(crate) fn new_for_map() -> Trace<ID, T, V> { 29 pub(crate) fn new_for_map() -> Trace<ID, T, V> {
27 Trace { for_arena: false, arena: Arena::default(), map: ArenaMap::default(), len: 0 } 30 Trace { arena: None, map: Some(ArenaMap::default()), len: 0 }
28 } 31 }
29 32
30 pub(crate) fn alloc(&mut self, value: impl Fn() -> V, data: impl Fn() -> T) { 33 pub(crate) fn alloc(&mut self, value: impl FnOnce() -> V, data: impl FnOnce() -> T) -> ID {
31 if self.for_arena { 34 let id = if let Some(arena) = &mut self.arena {
32 self.arena.alloc(data()); 35 arena.alloc(data())
33 } else { 36 } else {
34 let id = ID::from_raw(RawId::from(self.len)); 37 let id = ID::from_raw(RawId::from(self.len));
35 self.len += 1; 38 self.len += 1;
36 self.map.insert(id, value()); 39 id
40 };
41
42 if let Some(map) = &mut self.map {
43 map.insert(id, value());
37 } 44 }
45 id
46 }
47
48 pub(crate) fn into_arena(mut self) -> Arena<ID, T> {
49 self.arena.take().unwrap()
38 } 50 }
39 51
40 pub(crate) fn into_arena(self) -> Arena<ID, T> { 52 pub(crate) fn into_map(mut self) -> ArenaMap<ID, V> {
41 assert!(self.for_arena); 53 self.map.take().unwrap()
42 self.arena
43 } 54 }
44 55
45 pub(crate) fn into_map(self) -> ArenaMap<ID, V> { 56 pub(crate) fn into_arena_and_map(mut self) -> (Arena<ID, T>, ArenaMap<ID, V>) {
46 assert!(!self.for_arena); 57 (self.arena.take().unwrap(), self.map.take().unwrap())
47 self.map
48 } 58 }
49} 59}