Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,20 @@ impl ModuleDef {
acc
}

pub fn as_trait(self) -> Option<Trait> {
match self {
ModuleDef::Trait(it) => Some(it),
_ => None,
}
}

pub fn as_macro(self) -> Option<Macro> {
match self {
ModuleDef::Macro(it) => Some(it),
_ => None,
}
}

pub fn as_def_with_body(self) -> Option<DefWithBody> {
match self {
ModuleDef::Function(it) => Some(it.into()),
Expand Down Expand Up @@ -2997,6 +3011,13 @@ impl ItemInNs {
}
}

pub fn as_macro(self) -> Option<Macro> {
match self {
ItemInNs::Types(_) | ItemInNs::Values(_) => None,
ItemInNs::Macros(id) => Some(id),
}
}

/// Returns the crate defining this item (or `None` if `self` is built-in).
pub fn krate(&self, db: &dyn HirDatabase) -> Option<Crate> {
match self {
Expand Down
7 changes: 7 additions & 0 deletions crates/hir/src/semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ impl PathResolution {
PathResolution::SelfType(impl_def) => Some(TypeNs::SelfType((*impl_def).into())),
}
}

pub fn as_module_def(&self) -> Option<ModuleDef> {
match self {
PathResolution::Def(it) => Some(*it),
_ => None,
}
}
}

#[derive(Debug)]
Expand Down
Loading