rust/crates/ra_hir_def/src/keys.rs

59 lines
2.3 KiB
Rust
Raw Normal View History

//! keys to be used with `DynMap`
use std::marker::PhantomData;
2020-01-16 09:08:46 -06:00
use hir_expand::{InFile, MacroDefId};
use ra_syntax::{ast, AstNode, AstPtr};
use rustc_hash::FxHashMap;
use crate::{
dyn_map::{DynMap, Policy},
2020-04-25 07:23:34 -05:00
ConstId, EnumId, EnumVariantId, FieldId, FunctionId, ImplId, StaticId, StructId, TraitId,
2019-12-12 08:13:05 -06:00
TypeAliasId, TypeParamId, UnionId,
};
2019-12-12 08:50:16 -06:00
pub type Key<K, V> = crate::dyn_map::Key<InFile<K>, V, AstPtrPolicy<K, V>>;
2020-07-30 07:51:08 -05:00
pub const FUNCTION: Key<ast::Fn, FunctionId> = Key::new();
pub const CONST: Key<ast::ConstDef, ConstId> = Key::new();
pub const STATIC: Key<ast::StaticDef, StaticId> = Key::new();
2020-07-30 08:25:46 -05:00
pub const TYPE_ALIAS: Key<ast::TypeAlias, TypeAliasId> = Key::new();
2020-02-29 14:24:40 -06:00
pub const IMPL: Key<ast::ImplDef, ImplId> = Key::new();
2019-12-12 07:34:03 -06:00
pub const TRAIT: Key<ast::TraitDef, TraitId> = Key::new();
2019-12-12 07:58:04 -06:00
pub const STRUCT: Key<ast::StructDef, StructId> = Key::new();
2020-07-30 10:36:46 -05:00
pub const UNION: Key<ast::Union, UnionId> = Key::new();
2019-12-12 08:11:57 -06:00
pub const ENUM: Key<ast::EnumDef, EnumId> = Key::new();
2019-12-12 07:09:13 -06:00
pub const ENUM_VARIANT: Key<ast::EnumVariant, EnumVariantId> = Key::new();
2020-07-30 09:49:13 -05:00
pub const TUPLE_FIELD: Key<ast::TupleField, FieldId> = Key::new();
pub const RECORD_FIELD: Key<ast::RecordField, FieldId> = Key::new();
pub const TYPE_PARAM: Key<ast::TypeParam, TypeParamId> = Key::new();
2020-01-16 09:08:46 -06:00
pub const MACRO: Key<ast::MacroCall, MacroDefId> = Key::new();
/// XXX: AST Nodes and SyntaxNodes have identity equality semantics: nodes are
/// equal if they point to exactly the same object.
///
/// In general, we do not guarantee that we have exactly one instance of a
/// syntax tree for each file. We probably should add such guarantee, but, for
/// the time being, we will use identity-less AstPtr comparison.
pub struct AstPtrPolicy<AST, ID> {
_phantom: PhantomData<(AST, ID)>,
}
impl<AST: AstNode + 'static, ID: 'static> Policy for AstPtrPolicy<AST, ID> {
type K = InFile<AST>;
type V = ID;
fn insert(map: &mut DynMap, key: InFile<AST>, value: ID) {
let key = key.as_ref().map(AstPtr::new);
map.map
.entry::<FxHashMap<InFile<AstPtr<AST>>, ID>>()
.or_insert_with(Default::default)
.insert(key, value);
}
fn get<'a>(map: &'a DynMap, key: &InFile<AST>) -> Option<&'a ID> {
let key = key.as_ref().map(AstPtr::new);
map.map.get::<FxHashMap<InFile<AstPtr<AST>>, ID>>()?.get(&key)
}
}