diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-11-03 21:34:50 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-11-03 21:34:50 +0000 |
commit | 4fd742d6bc23e2f61d0980e15234f09d7e715b76 (patch) | |
tree | c37d38f9313f54ea71580f6bd005f554388a57ab /crates/ra_hir/src/from_id.rs | |
parent | f9f1effd011b906903891c09f1cb6b2a42f73e95 (diff) | |
parent | 8922a44395482896fec0c0a47a7fac4612112d45 (diff) |
Merge #2163
2163: Move CrateDefMap to hir_def r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/from_id.rs')
-rw-r--r-- | crates/ra_hir/src/from_id.rs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/crates/ra_hir/src/from_id.rs b/crates/ra_hir/src/from_id.rs new file mode 100644 index 000000000..089dbc908 --- /dev/null +++ b/crates/ra_hir/src/from_id.rs | |||
@@ -0,0 +1,63 @@ | |||
1 | //! Utility module for converting between hir_def ids and code_model wrappers. | ||
2 | //! | ||
3 | //! It's unclear if we need this long-term, but it's definitelly useful while we | ||
4 | //! are splitting the hir. | ||
5 | |||
6 | use hir_def::{AdtId, EnumVariantId, ModuleDefId}; | ||
7 | |||
8 | use crate::{Adt, EnumVariant, ModuleDef}; | ||
9 | |||
10 | macro_rules! from_id { | ||
11 | ($(($id:path, $ty:path)),*) => {$( | ||
12 | impl From<$id> for $ty { | ||
13 | fn from(id: $id) -> $ty { | ||
14 | $ty { id } | ||
15 | } | ||
16 | } | ||
17 | )*} | ||
18 | } | ||
19 | |||
20 | from_id![ | ||
21 | (hir_def::ModuleId, crate::Module), | ||
22 | (hir_def::StructId, crate::Struct), | ||
23 | (hir_def::UnionId, crate::Union), | ||
24 | (hir_def::EnumId, crate::Enum), | ||
25 | (hir_def::TypeAliasId, crate::TypeAlias), | ||
26 | (hir_def::TraitId, crate::Trait), | ||
27 | (hir_def::StaticId, crate::Static), | ||
28 | (hir_def::ConstId, crate::Const), | ||
29 | (hir_def::FunctionId, crate::Function), | ||
30 | (hir_expand::MacroDefId, crate::MacroDef) | ||
31 | ]; | ||
32 | |||
33 | impl From<AdtId> for Adt { | ||
34 | fn from(id: AdtId) -> Self { | ||
35 | match id { | ||
36 | AdtId::StructId(it) => Adt::Struct(it.into()), | ||
37 | AdtId::UnionId(it) => Adt::Union(it.into()), | ||
38 | AdtId::EnumId(it) => Adt::Enum(it.into()), | ||
39 | } | ||
40 | } | ||
41 | } | ||
42 | |||
43 | impl From<EnumVariantId> for EnumVariant { | ||
44 | fn from(id: EnumVariantId) -> Self { | ||
45 | EnumVariant { parent: id.parent.into(), id: id.local_id } | ||
46 | } | ||
47 | } | ||
48 | |||
49 | impl From<ModuleDefId> for ModuleDef { | ||
50 | fn from(id: ModuleDefId) -> Self { | ||
51 | match id { | ||
52 | ModuleDefId::ModuleId(it) => ModuleDef::Module(it.into()), | ||
53 | ModuleDefId::FunctionId(it) => ModuleDef::Function(it.into()), | ||
54 | ModuleDefId::AdtId(it) => ModuleDef::Adt(it.into()), | ||
55 | ModuleDefId::EnumVariantId(it) => ModuleDef::EnumVariant(it.into()), | ||
56 | ModuleDefId::ConstId(it) => ModuleDef::Const(it.into()), | ||
57 | ModuleDefId::StaticId(it) => ModuleDef::Static(it.into()), | ||
58 | ModuleDefId::TraitId(it) => ModuleDef::Trait(it.into()), | ||
59 | ModuleDefId::TypeAliasId(it) => ModuleDef::TypeAlias(it.into()), | ||
60 | ModuleDefId::BuiltinType(it) => ModuleDef::BuiltinType(it), | ||
61 | } | ||
62 | } | ||
63 | } | ||