aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/loc2id.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_analysis/src/loc2id.rs')
-rw-r--r--crates/ra_analysis/src/loc2id.rs50
1 files changed, 47 insertions, 3 deletions
diff --git a/crates/ra_analysis/src/loc2id.rs b/crates/ra_analysis/src/loc2id.rs
index a53ce8348..8eaa24997 100644
--- a/crates/ra_analysis/src/loc2id.rs
+++ b/crates/ra_analysis/src/loc2id.rs
@@ -1,7 +1,16 @@
1use std::hash::Hash; 1use parking_lot::Mutex;
2
3use std::{
4 hash::Hash,
5 sync::Arc,
6};
2 7
3use rustc_hash::FxHashMap; 8use rustc_hash::FxHashMap;
4 9
10use crate::{
11 syntax_ptr::SyntaxPtr,
12};
13
5/// There are two principle ways to refer to things: 14/// There are two principle ways to refer to things:
6/// - by their locatinon (module in foo/bar/baz.rs at line 42) 15/// - by their locatinon (module in foo/bar/baz.rs at line 42)
7/// - by their numeric id (module `ModuleId(42)`) 16/// - by their numeric id (module `ModuleId(42)`)
@@ -53,8 +62,8 @@ where
53 id 62 id
54 } 63 }
55 64
56 pub fn id2loc(&self, id: &ID) -> L { 65 pub fn id2loc(&self, id: ID) -> L {
57 self.id2loc[id].clone() 66 self.id2loc[&id].clone()
58 } 67 }
59} 68}
60 69
@@ -62,3 +71,38 @@ pub(crate) trait NumericId: Clone + Eq + Hash {
62 fn from_u32(id: u32) -> Self; 71 fn from_u32(id: u32) -> Self;
63 fn to_u32(self) -> u32; 72 fn to_u32(self) -> u32;
64} 73}
74
75#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
76pub(crate) struct FnId(u32);
77
78impl NumericId for FnId {
79 fn from_u32(id: u32) -> FnId {
80 FnId(id)
81 }
82 fn to_u32(self) -> u32 {
83 self.0
84 }
85}
86
87pub(crate) trait IdDatabase: salsa::Database {
88 fn id_maps(&self) -> &IdMaps;
89}
90
91#[derive(Debug, Default, Clone)]
92pub(crate) struct IdMaps {
93 inner: Arc<IdMapsInner>,
94}
95
96impl IdMaps {
97 pub(crate) fn fn_id(&self, ptr: SyntaxPtr) -> FnId {
98 self.inner.fns.lock().loc2id(&ptr)
99 }
100 pub(crate) fn fn_ptr(&self, fn_id: FnId) -> SyntaxPtr {
101 self.inner.fns.lock().id2loc(fn_id)
102 }
103}
104
105#[derive(Debug, Default)]
106struct IdMapsInner {
107 fns: Mutex<Loc2IdMap<SyntaxPtr, FnId>>,
108}