diff options
Diffstat (limited to 'crates/ra_hir_def/src/per_ns.rs')
-rw-r--r-- | crates/ra_hir_def/src/per_ns.rs | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/crates/ra_hir_def/src/per_ns.rs b/crates/ra_hir_def/src/per_ns.rs new file mode 100644 index 000000000..00e866bf9 --- /dev/null +++ b/crates/ra_hir_def/src/per_ns.rs | |||
@@ -0,0 +1,69 @@ | |||
1 | //! In rust, it is possible to have a value, a type and a macro with the same | ||
2 | //! name without conflicts. | ||
3 | //! | ||
4 | //! `PerNs` (per namespace) captures this. | ||
5 | |||
6 | use hir_expand::MacroDefId; | ||
7 | |||
8 | use crate::ModuleDefId; | ||
9 | |||
10 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] | ||
11 | pub struct PerNs { | ||
12 | pub types: Option<ModuleDefId>, | ||
13 | pub values: Option<ModuleDefId>, | ||
14 | /// Since macros has different type, many methods simply ignore it. | ||
15 | /// We can only use special method like `get_macros` to access it. | ||
16 | pub macros: Option<MacroDefId>, | ||
17 | } | ||
18 | |||
19 | impl Default for PerNs { | ||
20 | fn default() -> Self { | ||
21 | PerNs { types: None, values: None, macros: None } | ||
22 | } | ||
23 | } | ||
24 | |||
25 | impl PerNs { | ||
26 | pub fn none() -> PerNs { | ||
27 | PerNs { types: None, values: None, macros: None } | ||
28 | } | ||
29 | |||
30 | pub fn values(t: ModuleDefId) -> PerNs { | ||
31 | PerNs { types: None, values: Some(t), macros: None } | ||
32 | } | ||
33 | |||
34 | pub fn types(t: ModuleDefId) -> PerNs { | ||
35 | PerNs { types: Some(t), values: None, macros: None } | ||
36 | } | ||
37 | |||
38 | pub fn both(types: ModuleDefId, values: ModuleDefId) -> PerNs { | ||
39 | PerNs { types: Some(types), values: Some(values), macros: None } | ||
40 | } | ||
41 | |||
42 | pub fn macros(macro_: MacroDefId) -> PerNs { | ||
43 | PerNs { types: None, values: None, macros: Some(macro_) } | ||
44 | } | ||
45 | |||
46 | pub fn is_none(&self) -> bool { | ||
47 | self.types.is_none() && self.values.is_none() && self.macros.is_none() | ||
48 | } | ||
49 | |||
50 | pub fn take_types(self) -> Option<ModuleDefId> { | ||
51 | self.types | ||
52 | } | ||
53 | |||
54 | pub fn take_values(self) -> Option<ModuleDefId> { | ||
55 | self.values | ||
56 | } | ||
57 | |||
58 | pub fn take_macros(self) -> Option<MacroDefId> { | ||
59 | self.macros | ||
60 | } | ||
61 | |||
62 | pub fn or(self, other: PerNs) -> PerNs { | ||
63 | PerNs { | ||
64 | types: self.types.or(other.types), | ||
65 | values: self.values.or(other.values), | ||
66 | macros: self.macros.or(other.macros), | ||
67 | } | ||
68 | } | ||
69 | } | ||