diff options
author | Seivan Heidari <[email protected]> | 2019-11-04 12:45:27 +0000 |
---|---|---|
committer | Seivan Heidari <[email protected]> | 2019-11-04 12:45:27 +0000 |
commit | dad9bc6caad71e6aebb92ad9883c08d30431e9b1 (patch) | |
tree | 6495d47108bc56ab0fbb358125fe65ebece8934f /crates/ra_hir/src/from_id.rs | |
parent | 1d8bb4c6c1fef1f8ea513e07d0a7d4c5483129d2 (diff) | |
parent | cc2d75d0f88bdcb1b3e20db36decb6ee6eca517a (diff) |
Merge branch 'master' into feature/themes
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 | } | ||