rust-analyzer/crates/ra_hir_def/src/per_ns.rs

80 lines
2.3 KiB
Rust
Raw Normal View History

2019-11-24 18:00:50 +00:00
//! In rust, it is possible to have a value, a type and a macro with the same
//! name without conflicts.
//!
//! `PerNs` (per namespace) captures this.
2019-10-31 15:45:10 +00:00
use hir_expand::MacroDefId;
use crate::{visibility::ResolvedVisibility, ModuleDefId};
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
2019-09-13 13:38:59 +00:00
pub struct PerNs {
pub types: Option<(ModuleDefId, ResolvedVisibility)>,
pub values: Option<(ModuleDefId, ResolvedVisibility)>,
pub macros: Option<(MacroDefId, ResolvedVisibility)>,
2019-03-16 14:17:50 +00:00
}
2019-09-13 13:38:59 +00:00
impl Default for PerNs {
2019-03-16 14:17:50 +00:00
fn default() -> Self {
PerNs { types: None, values: None, macros: None }
2019-03-16 14:17:50 +00:00
}
}
2019-09-13 13:38:59 +00:00
impl PerNs {
pub fn none() -> PerNs {
PerNs { types: None, values: None, macros: None }
2019-03-16 14:17:50 +00:00
}
pub fn values(t: ModuleDefId, v: ResolvedVisibility) -> PerNs {
PerNs { types: None, values: Some((t, v)), macros: None }
2019-03-16 14:17:50 +00:00
}
pub fn types(t: ModuleDefId, v: ResolvedVisibility) -> PerNs {
PerNs { types: Some((t, v)), values: None, macros: None }
2019-03-16 14:17:50 +00:00
}
pub fn both(types: ModuleDefId, values: ModuleDefId, v: ResolvedVisibility) -> PerNs {
PerNs { types: Some((types, v)), values: Some((values, v)), macros: None }
2019-03-16 14:17:50 +00:00
}
pub fn macros(macro_: MacroDefId, v: ResolvedVisibility) -> PerNs {
PerNs { types: None, values: None, macros: Some((macro_, v)) }
2019-03-16 14:17:50 +00:00
}
pub fn is_none(&self) -> bool {
self.types.is_none() && self.values.is_none() && self.macros.is_none()
2019-03-16 14:17:50 +00:00
}
2019-10-31 15:45:10 +00:00
pub fn take_types(self) -> Option<ModuleDefId> {
self.types.map(|it| it.0)
}
pub fn take_types_vis(self) -> Option<(ModuleDefId, ResolvedVisibility)> {
self.types
2019-03-16 14:17:50 +00:00
}
2019-10-31 15:45:10 +00:00
pub fn take_values(self) -> Option<ModuleDefId> {
self.values.map(|it| it.0)
2019-03-16 14:17:50 +00:00
}
2019-11-24 14:00:10 +00:00
pub fn take_macros(self) -> Option<MacroDefId> {
self.macros.map(|it| it.0)
}
pub fn with_visibility(self, vis: ResolvedVisibility) -> PerNs {
PerNs {
types: self.types.map(|(it, _)| (it, vis)),
values: self.values.map(|(it, _)| (it, vis)),
macros: self.macros.map(|(it, _)| (it, vis)),
}
}
2019-09-13 13:38:59 +00:00
pub fn or(self, other: PerNs) -> PerNs {
PerNs {
types: self.types.or(other.types),
values: self.values.or(other.values),
macros: self.macros.or(other.macros),
}
2019-03-16 14:17:50 +00:00
}
}