mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 20:42:04 +00:00
Deduplicate some code
This commit is contained in:
parent
bb0de88f24
commit
2ebf0c87c2
22 changed files with 299 additions and 234 deletions
|
@ -1910,7 +1910,7 @@ impl Function {
|
|||
return None;
|
||||
}
|
||||
let loc = self.id.lookup(db.upcast());
|
||||
let def_map = db.crate_def_map(loc.krate(db).into());
|
||||
let def_map = db.crate_def_map(HasModule::krate(&loc, db.upcast()));
|
||||
def_map.fn_as_proc_macro(self.id).map(|id| Macro { id: id.into() })
|
||||
}
|
||||
|
||||
|
@ -2516,11 +2516,13 @@ pub enum AssocItem {
|
|||
Const(Const),
|
||||
TypeAlias(TypeAlias),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum AssocItemContainer {
|
||||
Trait(Trait),
|
||||
Impl(Impl),
|
||||
}
|
||||
|
||||
pub trait AsAssocItem {
|
||||
fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem>;
|
||||
}
|
||||
|
@ -2530,16 +2532,19 @@ impl AsAssocItem for Function {
|
|||
as_assoc_item(db, AssocItem::Function, self.id)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsAssocItem for Const {
|
||||
fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem> {
|
||||
as_assoc_item(db, AssocItem::Const, self.id)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsAssocItem for TypeAlias {
|
||||
fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem> {
|
||||
as_assoc_item(db, AssocItem::TypeAlias, self.id)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsAssocItem for ModuleDef {
|
||||
fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem> {
|
||||
match self {
|
||||
|
@ -2550,6 +2555,7 @@ impl AsAssocItem for ModuleDef {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AsAssocItem for DefWithBody {
|
||||
fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem> {
|
||||
match self {
|
||||
|
@ -2560,15 +2566,14 @@ impl AsAssocItem for DefWithBody {
|
|||
}
|
||||
}
|
||||
|
||||
fn as_assoc_item<'db, ID, DEF, CTOR, AST>(
|
||||
fn as_assoc_item<'db, ID, DEF, AST>(
|
||||
db: &(dyn HirDatabase + 'db),
|
||||
ctor: CTOR,
|
||||
ctor: impl FnOnce(DEF) -> AssocItem,
|
||||
id: ID,
|
||||
) -> Option<AssocItem>
|
||||
where
|
||||
ID: Lookup<Database<'db> = dyn DefDatabase + 'db, Data = AssocItemLoc<AST>>,
|
||||
DEF: From<ID>,
|
||||
CTOR: FnOnce(DEF) -> AssocItem,
|
||||
AST: ItemTreeModItemNode,
|
||||
{
|
||||
match id.lookup(db.upcast()).container {
|
||||
|
@ -2609,27 +2614,34 @@ impl AssocItem {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn containing_trait(self, db: &dyn HirDatabase) -> Option<Trait> {
|
||||
pub fn container_trait(self, db: &dyn HirDatabase) -> Option<Trait> {
|
||||
match self.container(db) {
|
||||
AssocItemContainer::Trait(t) => Some(t),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn containing_trait_impl(self, db: &dyn HirDatabase) -> Option<Trait> {
|
||||
pub fn implemented_trait(self, db: &dyn HirDatabase) -> Option<Trait> {
|
||||
match self.container(db) {
|
||||
AssocItemContainer::Impl(i) => i.trait_(db),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn containing_trait_or_trait_impl(self, db: &dyn HirDatabase) -> Option<Trait> {
|
||||
pub fn container_or_implemented_trait(self, db: &dyn HirDatabase) -> Option<Trait> {
|
||||
match self.container(db) {
|
||||
AssocItemContainer::Trait(t) => Some(t),
|
||||
AssocItemContainer::Impl(i) => i.trait_(db),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn implementing_ty(self, db: &dyn HirDatabase) -> Option<Type> {
|
||||
match self.container(db) {
|
||||
AssocItemContainer::Impl(i) => Some(i.self_ty(db)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_function(self) -> Option<Function> {
|
||||
match self {
|
||||
Self::Function(v) => Some(v),
|
||||
|
@ -3321,7 +3333,7 @@ impl Impl {
|
|||
}
|
||||
|
||||
pub fn items(self, db: &dyn HirDatabase) -> Vec<AssocItem> {
|
||||
db.impl_data(self.id).items.iter().map(|it| (*it).into()).collect()
|
||||
db.impl_data(self.id).items.iter().map(|&it| it.into()).collect()
|
||||
}
|
||||
|
||||
pub fn is_negative(self, db: &dyn HirDatabase) -> bool {
|
||||
|
@ -3677,7 +3689,7 @@ impl Type {
|
|||
.and_then(|it| {
|
||||
let into_future_fn = it.as_function()?;
|
||||
let assoc_item = as_assoc_item(db, AssocItem::Function, into_future_fn)?;
|
||||
let into_future_trait = assoc_item.containing_trait_or_trait_impl(db)?;
|
||||
let into_future_trait = assoc_item.container_or_implemented_trait(db)?;
|
||||
Some(into_future_trait.id)
|
||||
})
|
||||
.or_else(|| {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue