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.rs49
1 files changed, 49 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..be702a4f8
--- /dev/null
+++ b/crates/ra_hir_def/src/keys.rs
@@ -0,0 +1,49 @@
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, TypeParamId,
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();
23pub const TYPE_PARAM: Key<ast::TypeParam, TypeParamId> = Key::new();
24
25/// XXX: AST Nodes and SyntaxNodes have identity equality semantics: nodes are
26/// equal if they point to exactly the same object.
27///
28/// In general, we do not guarantee that we have exactly one instance of a
29/// syntax tree for each file. We probably should add such guarantee, but, for
30/// the time being, we will use identity-less AstPtr comparison.
31pub struct AstPtrPolicy<AST, ID> {
32 _phantom: PhantomData<(AST, ID)>,
33}
34
35impl<AST: AstNode + 'static, ID: 'static> Policy for AstPtrPolicy<AST, ID> {
36 type K = InFile<AST>;
37 type V = ID;
38 fn insert(map: &mut DynMap, key: InFile<AST>, value: ID) {
39 let key = key.as_ref().map(AstPtr::new);
40 map.map
41 .entry::<FxHashMap<InFile<AstPtr<AST>>, ID>>()
42 .or_insert_with(Default::default)
43 .insert(key, value);
44 }
45 fn get<'a>(map: &'a DynMap, key: &InFile<AST>) -> Option<&'a ID> {
46 let key = key.as_ref().map(AstPtr::new);
47 map.map.get::<FxHashMap<InFile<AstPtr<AST>>, ID>>()?.get(&key)
48 }
49}