aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/per_ns.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def/src/per_ns.rs')
-rw-r--r--crates/ra_hir_def/src/per_ns.rs74
1 files changed, 74 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..717ed1ef9
--- /dev/null
+++ b/crates/ra_hir_def/src/per_ns.rs
@@ -0,0 +1,74 @@
1//! FIXME: write short doc here
2
3use hir_expand::MacroDefId;
4
5use crate::ModuleDefId;
6
7#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
8pub struct PerNs {
9 pub types: Option<ModuleDefId>,
10 pub values: Option<ModuleDefId>,
11 /// Since macros has different type, many methods simply ignore it.
12 /// We can only use special method like `get_macros` to access it.
13 pub macros: Option<MacroDefId>,
14}
15
16impl Default for PerNs {
17 fn default() -> Self {
18 PerNs { types: None, values: None, macros: None }
19 }
20}
21
22impl PerNs {
23 pub fn none() -> PerNs {
24 PerNs { types: None, values: None, macros: None }
25 }
26
27 pub fn values(t: ModuleDefId) -> PerNs {
28 PerNs { types: None, values: Some(t), macros: None }
29 }
30
31 pub fn types(t: ModuleDefId) -> PerNs {
32 PerNs { types: Some(t), values: None, macros: None }
33 }
34
35 pub fn both(types: ModuleDefId, values: ModuleDefId) -> PerNs {
36 PerNs { types: Some(types), values: Some(values), macros: None }
37 }
38
39 pub fn macros(macro_: MacroDefId) -> PerNs {
40 PerNs { types: None, values: None, macros: Some(macro_) }
41 }
42
43 pub fn is_none(&self) -> bool {
44 self.types.is_none() && self.values.is_none() && self.macros.is_none()
45 }
46
47 pub fn is_all(&self) -> bool {
48 self.types.is_some() && self.values.is_some() && self.macros.is_some()
49 }
50
51 pub fn take_types(self) -> Option<ModuleDefId> {
52 self.types
53 }
54
55 pub fn take_values(self) -> Option<ModuleDefId> {
56 self.values
57 }
58
59 pub fn get_macros(&self) -> Option<MacroDefId> {
60 self.macros
61 }
62
63 pub fn only_macros(&self) -> PerNs {
64 PerNs { types: None, values: None, macros: self.macros }
65 }
66
67 pub fn or(self, other: PerNs) -> PerNs {
68 PerNs {
69 types: self.types.or(other.types),
70 values: self.values.or(other.values),
71 macros: self.macros.or(other.macros),
72 }
73 }
74}