aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/keys.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def/src/keys.rs')
-rw-r--r--crates/ra_hir_def/src/keys.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/crates/ra_hir_def/src/keys.rs b/crates/ra_hir_def/src/keys.rs
new file mode 100644
index 000000000..447b7e3ba
--- /dev/null
+++ b/crates/ra_hir_def/src/keys.rs
@@ -0,0 +1,48 @@
1//! keys to be used with `DynMap`
2
3use std::marker::PhantomData;
4
5use hir_expand::InFile;
6use ra_syntax::{ast, AstNode, AstPtr};
7use rustc_hash::FxHashMap;
8
9use crate::{
10 dyn_map::{DynMap, Policy},
11 ConstId, EnumVariantId, FunctionId, StaticId, StructFieldId, TypeAliasId,
12};
13
14type Key<K, V> = crate::dyn_map::Key<InFile<K>, V, AstPtrPolicy<K, V>>;
15
16pub const FUNCTION: Key<ast::FnDef, FunctionId> = Key::new();
17pub const CONST: Key<ast::ConstDef, ConstId> = Key::new();
18pub const STATIC: Key<ast::StaticDef, StaticId> = Key::new();
19pub const ENUM_VARIANT: Key<ast::EnumVariant, EnumVariantId> = Key::new();
20pub const TYPE_ALIAS: Key<ast::TypeAliasDef, TypeAliasId> = Key::new();
21pub const TUPLE_FIELD: Key<ast::TupleFieldDef, StructFieldId> = Key::new();
22pub const RECORD_FIELD: Key<ast::RecordFieldDef, StructFieldId> = Key::new();
23
24/// XXX: AST Nodes and SyntaxNodes have identity equality semantics: nodes are
25/// equal if they point to exactly the same object.
26///
27/// In general, we do not guarantee that we have exactly one instance of a
28/// syntax tree for each file. We probably should add such guarantee, but, for
29/// the time being, we will use identity-less AstPtr comparison.
30pub struct AstPtrPolicy<AST, ID> {
31 _phantom: PhantomData<(AST, ID)>,
32}
33
34impl<AST: AstNode + 'static, ID: 'static> Policy for AstPtrPolicy<AST, ID> {
35 type K = InFile<AST>;
36 type V = ID;
37 fn insert(map: &mut DynMap, key: InFile<AST>, value: ID) {
38 let key = key.as_ref().map(AstPtr::new);
39 map.map
40 .entry::<FxHashMap<InFile<AstPtr<AST>>, ID>>()
41 .or_insert_with(Default::default)
42 .insert(key, value);
43 }
44 fn get<'a>(map: &'a DynMap, key: &InFile<AST>) -> Option<&'a ID> {
45 let key = key.as_ref().map(AstPtr::new);
46 map.map.get::<FxHashMap<InFile<AstPtr<AST>>, ID>>()?.get(&key)
47 }
48}