Skip to content

Commit a3d6ddb

Browse files
committed
More natural trait setup
1 parent 8691ae8 commit a3d6ddb

File tree

4 files changed

+49
-27
lines changed

4 files changed

+49
-27
lines changed

crates/ra_assists/src/assists/add_new.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,12 @@ fn find_struct_impl(
140140

141141
let struct_ty = {
142142
let src = InFile { file_id: ctx.frange.file_id.into(), value: strukt.clone() };
143-
sb.to_def::<hir::Struct, _>(src)?.ty(db)
143+
sb.to_def(src)?.ty(db)
144144
};
145145

146146
let block = module.descendants().filter_map(ast::ImplBlock::cast).find_map(|impl_blk| {
147147
let src = InFile { file_id: ctx.frange.file_id.into(), value: impl_blk.clone() };
148-
let blk = sb.to_def::<hir::ImplBlock, _>(src)?;
148+
let blk = sb.to_def(src)?;
149149

150150
let same_ty = blk.target_ty(db) == struct_ty;
151151
let not_trait_impl = blk.target_trait(db).is_none();

crates/ra_hir/src/from_source.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ use ra_syntax::{
1414

1515
use crate::{
1616
db::{DefDatabase, HirDatabase},
17-
Const, DefWithBody, Enum, Function, ImplBlock, InFile, Local, Module, SourceBinder, Static,
18-
Struct, Trait, TypeAlias, TypeParam,
17+
DefWithBody, InFile, Local, Module, SourceBinder, TypeParam,
1918
};
2019

2120
impl Local {
@@ -25,9 +24,9 @@ impl Local {
2524
let parent: DefWithBody = src.value.syntax().ancestors().find_map(|it| {
2625
let res = match_ast! {
2726
match it {
28-
ast::ConstDef(value) => { sb.to_def::<Const, _>(InFile { value, file_id})?.into() },
29-
ast::StaticDef(value) => { sb.to_def::<Static, _>(InFile { value, file_id})?.into() },
30-
ast::FnDef(value) => { sb.to_def::<Function, _>(InFile { value, file_id})?.into() },
27+
ast::ConstDef(value) => { sb.to_def(InFile { value, file_id})?.into() },
28+
ast::StaticDef(value) => { sb.to_def(InFile { value, file_id})?.into() },
29+
ast::FnDef(value) => { sb.to_def(InFile { value, file_id})?.into() },
3130
_ => return None,
3231
}
3332
};
@@ -47,12 +46,12 @@ impl TypeParam {
4746
let parent: GenericDefId = src.value.syntax().ancestors().find_map(|it| {
4847
let res = match_ast! {
4948
match it {
50-
ast::FnDef(value) => { sb.to_def::<Function, _>(InFile { value, file_id})?.id.into() },
51-
ast::StructDef(value) => { sb.to_def::<Struct, _>(InFile { value, file_id})?.id.into() },
52-
ast::EnumDef(value) => { sb.to_def::<Enum, _>(InFile { value, file_id})?.id.into() },
53-
ast::TraitDef(value) => { sb.to_def::<Trait, _>(InFile { value, file_id})?.id.into() },
54-
ast::TypeAliasDef(value) => { sb.to_def::<TypeAlias, _>(InFile { value, file_id})?.id.into() },
55-
ast::ImplBlock(value) => { sb.to_def::<ImplBlock, _>(InFile { value, file_id})?.id.into() },
49+
ast::FnDef(value) => { sb.to_def(InFile { value, file_id})?.id.into() },
50+
ast::StructDef(value) => { sb.to_def(InFile { value, file_id})?.id.into() },
51+
ast::EnumDef(value) => { sb.to_def(InFile { value, file_id})?.id.into() },
52+
ast::TraitDef(value) => { sb.to_def(InFile { value, file_id})?.id.into() },
53+
ast::TypeAliasDef(value) => { sb.to_def(InFile { value, file_id})?.id.into() },
54+
ast::ImplBlock(value) => { sb.to_def(InFile { value, file_id})?.id.into() },
5655
_ => return None,
5756
}
5857
};

crates/ra_hir/src/source_binder.rs

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,7 @@ impl<DB: HirDatabase> SourceBinder<'_, DB> {
5252
SourceAnalyzer::new_for_resolver(resolver, src)
5353
}
5454

55-
pub fn to_def<D, T>(&mut self, src: InFile<T>) -> Option<D>
56-
where
57-
D: From<T::ID>,
58-
T: ToId,
59-
{
55+
pub fn to_def<T: ToDef>(&mut self, src: InFile<T>) -> Option<T::Def> {
6056
let id: T::ID = self.to_id(src)?;
6157
Some(id.into())
6258
}
@@ -114,6 +110,39 @@ impl<DB: HirDatabase> SourceBinder<'_, DB> {
114110
}
115111
}
116112

113+
pub trait ToId: Sized + AstNode + 'static {
114+
type ID: Sized + Copy + 'static;
115+
fn to_id<DB: HirDatabase>(sb: &mut SourceBinder<'_, DB>, src: InFile<Self>)
116+
-> Option<Self::ID>;
117+
}
118+
119+
pub trait ToDef: ToId {
120+
type Def: From<Self::ID>;
121+
}
122+
123+
macro_rules! to_def_impls {
124+
($(($def:path, $ast:path)),* ,) => {$(
125+
impl ToDef for $ast {
126+
type Def = $def;
127+
}
128+
)*}
129+
}
130+
131+
to_def_impls![
132+
(crate::Struct, ast::StructDef),
133+
(crate::Enum, ast::EnumDef),
134+
(crate::Union, ast::UnionDef),
135+
(crate::Trait, ast::TraitDef),
136+
(crate::ImplBlock, ast::ImplBlock),
137+
(crate::TypeAlias, ast::TypeAliasDef),
138+
(crate::Const, ast::ConstDef),
139+
(crate::Static, ast::StaticDef),
140+
(crate::Function, ast::FnDef),
141+
(crate::StructField, ast::RecordFieldDef),
142+
(crate::EnumVariant, ast::EnumVariant),
143+
(crate::MacroDef, ast::MacroCall), // this one is dubious, not all calls are macros
144+
];
145+
117146
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
118147
enum ChildContainer {
119148
DefWithBodyId(DefWithBodyId),
@@ -133,12 +162,6 @@ impl_froms! {
133162
VariantId,
134163
}
135164

136-
pub trait ToId: Sized + AstNode + 'static {
137-
type ID: Sized + Copy + 'static;
138-
fn to_id<DB: HirDatabase>(sb: &mut SourceBinder<'_, DB>, src: InFile<Self>)
139-
-> Option<Self::ID>;
140-
}
141-
142165
pub trait ToIdByKey: Sized + AstNode + 'static {
143166
type ID: Sized + Copy + 'static;
144167
const KEY: Key<Self, Self::ID>;

crates/ra_ide/src/impls.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ fn impls_for_def(
4444
let ty = match node {
4545
ast::NominalDef::StructDef(def) => {
4646
let src = hir::InFile { file_id: position.file_id.into(), value: def.clone() };
47-
sb.to_def::<hir::Struct, _>(src)?.ty(sb.db)
47+
sb.to_def(src)?.ty(sb.db)
4848
}
4949
ast::NominalDef::EnumDef(def) => {
5050
let src = hir::InFile { file_id: position.file_id.into(), value: def.clone() };
51-
sb.to_def::<hir::Enum, _>(src)?.ty(sb.db)
51+
sb.to_def(src)?.ty(sb.db)
5252
}
5353
ast::NominalDef::UnionDef(def) => {
5454
let src = hir::InFile { file_id: position.file_id.into(), value: def.clone() };
55-
sb.to_def::<hir::Union, _>(src)?.ty(sb.db)
55+
sb.to_def(src)?.ty(sb.db)
5656
}
5757
};
5858

0 commit comments

Comments
 (0)