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.rs56
1 files changed, 56 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..d844f7a62
--- /dev/null
+++ b/crates/ra_hir_def/src/keys.rs
@@ -0,0 +1,56 @@
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, EnumId, EnumVariantId, FunctionId, ImplId, StaticId, StructFieldId, StructId, TraitId,
12 TypeAliasId, TypeParamId, UnionId,
13};
14
15pub type Key<K, V> = crate::dyn_map::Key<InFile<K>, V, AstPtrPolicy<K, V>>;
16
17pub const FUNCTION: Key<ast::FnDef, FunctionId> = Key::new();
18pub const CONST: Key<ast::ConstDef, ConstId> = Key::new();
19pub const STATIC: Key<ast::StaticDef, StaticId> = Key::new();
20pub const TYPE_ALIAS: Key<ast::TypeAliasDef, TypeAliasId> = Key::new();
21pub const IMPL: Key<ast::ImplBlock, ImplId> = Key::new();
22pub const TRAIT: Key<ast::TraitDef, TraitId> = Key::new();
23pub const STRUCT: Key<ast::StructDef, StructId> = Key::new();
24pub const UNION: Key<ast::UnionDef, UnionId> = Key::new();
25pub const ENUM: Key<ast::EnumDef, EnumId> = Key::new();
26
27pub const ENUM_VARIANT: Key<ast::EnumVariant, EnumVariantId> = Key::new();
28pub const TUPLE_FIELD: Key<ast::TupleFieldDef, StructFieldId> = Key::new();
29pub const RECORD_FIELD: Key<ast::RecordFieldDef, StructFieldId> = Key::new();
30pub const TYPE_PARAM: Key<ast::TypeParam, TypeParamId> = Key::new();
31
32/// XXX: AST Nodes and SyntaxNodes have identity equality semantics: nodes are
33/// equal if they point to exactly the same object.
34///
35/// In general, we do not guarantee that we have exactly one instance of a
36/// syntax tree for each file. We probably should add such guarantee, but, for
37/// the time being, we will use identity-less AstPtr comparison.
38pub struct AstPtrPolicy<AST, ID> {
39 _phantom: PhantomData<(AST, ID)>,
40}
41
42impl<AST: AstNode + 'static, ID: 'static> Policy for AstPtrPolicy<AST, ID> {
43 type K = InFile<AST>;
44 type V = ID;
45 fn insert(map: &mut DynMap, key: InFile<AST>, value: ID) {
46 let key = key.as_ref().map(AstPtr::new);
47 map.map
48 .entry::<FxHashMap<InFile<AstPtr<AST>>, ID>>()
49 .or_insert_with(Default::default)
50 .insert(key, value);
51 }
52 fn get<'a>(map: &'a DynMap, key: &InFile<AST>) -> Option<&'a ID> {
53 let key = key.as_ref().map(AstPtr::new);
54 map.map.get::<FxHashMap<InFile<AstPtr<AST>>, ID>>()?.get(&key)
55 }
56}