aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_db/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_db/src')
-rw-r--r--crates/ra_db/src/lib.rs6
-rw-r--r--crates/ra_db/src/loc2id.rs103
2 files changed, 1 insertions, 108 deletions
diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs
index f3389c91f..1cd400752 100644
--- a/crates/ra_db/src/lib.rs
+++ b/crates/ra_db/src/lib.rs
@@ -1,11 +1,8 @@
1//! ra_db defines basic database traits. The concrete DB is defined by ra_ide_api. 1//! ra_db defines basic database traits. The concrete DB is defined by ra_ide_api.
2mod cancellation; 2mod cancellation;
3mod input; 3mod input;
4mod loc2id;
5 4
6use std::{ 5use std::{panic, sync::Arc};
7 panic, sync::Arc,
8};
9 6
10use ra_syntax::{TextUnit, TextRange, SourceFile, TreeArc}; 7use ra_syntax::{TextUnit, TextRange, SourceFile, TreeArc};
11use relative_path::RelativePathBuf; 8use relative_path::RelativePathBuf;
@@ -16,7 +13,6 @@ pub use crate::{
16 input::{ 13 input::{
17 FileId, CrateId, SourceRoot, SourceRootId, CrateGraph, Dependency, Edition, 14 FileId, CrateId, SourceRoot, SourceRootId, CrateGraph, Dependency, Edition,
18 }, 15 },
19 loc2id::LocationInterner,
20}; 16};
21 17
22pub trait CheckCanceled: panic::RefUnwindSafe { 18pub trait CheckCanceled: panic::RefUnwindSafe {
diff --git a/crates/ra_db/src/loc2id.rs b/crates/ra_db/src/loc2id.rs
deleted file mode 100644
index eae64a4eb..000000000
--- a/crates/ra_db/src/loc2id.rs
+++ /dev/null
@@ -1,103 +0,0 @@
1use std::{panic, hash::Hash};
2
3use parking_lot::Mutex;
4use rustc_hash::FxHashMap;
5use ra_arena::{Arena, ArenaId};
6
7/// There are two principle ways to refer to things:
8/// - by their location (module in foo/bar/baz.rs at line 42)
9/// - by their numeric id (module `ModuleId(42)`)
10///
11/// The first one is more powerful (you can actually find the thing in question
12/// by id), but the second one is so much more compact.
13///
14/// `Loc2IdMap` allows us to have a cake an eat it as well: by maintaining a
15/// bidirectional mapping between positional and numeric ids, we can use compact
16/// representation which still allows us to get the actual item.
17#[derive(Debug)]
18struct Loc2IdMap<LOC, ID>
19where
20 ID: ArenaId + Clone,
21 LOC: Clone + Eq + Hash,
22{
23 id2loc: Arena<ID, LOC>,
24 loc2id: FxHashMap<LOC, ID>,
25}
26
27impl<LOC, ID> Default for Loc2IdMap<LOC, ID>
28where
29 ID: ArenaId + Clone,
30 LOC: Clone + Eq + Hash,
31{
32 fn default() -> Self {
33 Loc2IdMap { id2loc: Arena::default(), loc2id: FxHashMap::default() }
34 }
35}
36
37impl<LOC, ID> Loc2IdMap<LOC, ID>
38where
39 ID: ArenaId + Clone,
40 LOC: Clone + Eq + Hash,
41{
42 pub fn len(&self) -> usize {
43 self.id2loc.len()
44 }
45
46 pub fn loc2id(&mut self, loc: &LOC) -> ID {
47 match self.loc2id.get(loc) {
48 Some(id) => return id.clone(),
49 None => (),
50 }
51 let id = self.id2loc.alloc(loc.clone());
52 self.loc2id.insert(loc.clone(), id.clone());
53 id
54 }
55
56 pub fn id2loc(&self, id: ID) -> LOC {
57 self.id2loc[id].clone()
58 }
59}
60
61#[derive(Debug)]
62pub struct LocationInterner<LOC, ID>
63where
64 ID: ArenaId + Clone,
65 LOC: Clone + Eq + Hash,
66{
67 map: Mutex<Loc2IdMap<LOC, ID>>,
68}
69
70impl<LOC, ID> panic::RefUnwindSafe for LocationInterner<LOC, ID>
71where
72 ID: ArenaId + Clone,
73 LOC: Clone + Eq + Hash,
74 ID: panic::RefUnwindSafe,
75 LOC: panic::RefUnwindSafe,
76{
77}
78
79impl<LOC, ID> Default for LocationInterner<LOC, ID>
80where
81 ID: ArenaId + Clone,
82 LOC: Clone + Eq + Hash,
83{
84 fn default() -> Self {
85 LocationInterner { map: Default::default() }
86 }
87}
88
89impl<LOC, ID> LocationInterner<LOC, ID>
90where
91 ID: ArenaId + Clone,
92 LOC: Clone + Eq + Hash,
93{
94 pub fn len(&self) -> usize {
95 self.map.lock().len()
96 }
97 pub fn loc2id(&self, loc: &LOC) -> ID {
98 self.map.lock().loc2id(loc)
99 }
100 pub fn id2loc(&self, id: ID) -> LOC {
101 self.map.lock().id2loc(id)
102 }
103}