aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/source_binder.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-01-16 15:27:21 +0000
committerAleksey Kladov <[email protected]>2020-01-16 15:27:21 +0000
commita3d6ddbe694498a1bf69c6253422efb89431164e (patch)
treeced08cf46f2f822abc630b6935ec5ecf4ee4dc0d /crates/ra_hir/src/source_binder.rs
parent8691ae8ac04ef9dc089a377770da86a952b0e4c1 (diff)
More natural trait setup
Diffstat (limited to 'crates/ra_hir/src/source_binder.rs')
-rw-r--r--crates/ra_hir/src/source_binder.rs45
1 files changed, 34 insertions, 11 deletions
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index 66930e492..c02175c06 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -52,11 +52,7 @@ impl<DB: HirDatabase> SourceBinder<'_, DB> {
52 SourceAnalyzer::new_for_resolver(resolver, src) 52 SourceAnalyzer::new_for_resolver(resolver, src)
53 } 53 }
54 54
55 pub fn to_def<D, T>(&mut self, src: InFile<T>) -> Option<D> 55 pub fn to_def<T: ToDef>(&mut self, src: InFile<T>) -> Option<T::Def> {
56 where
57 D: From<T::ID>,
58 T: ToId,
59 {
60 let id: T::ID = self.to_id(src)?; 56 let id: T::ID = self.to_id(src)?;
61 Some(id.into()) 57 Some(id.into())
62 } 58 }
@@ -114,6 +110,39 @@ impl<DB: HirDatabase> SourceBinder<'_, DB> {
114 } 110 }
115} 111}
116 112
113pub 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
119pub trait ToDef: ToId {
120 type Def: From<Self::ID>;
121}
122
123macro_rules! to_def_impls {
124 ($(($def:path, $ast:path)),* ,) => {$(
125 impl ToDef for $ast {
126 type Def = $def;
127 }
128 )*}
129}
130
131to_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
117#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] 146#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
118enum ChildContainer { 147enum ChildContainer {
119 DefWithBodyId(DefWithBodyId), 148 DefWithBodyId(DefWithBodyId),
@@ -133,12 +162,6 @@ impl_froms! {
133 VariantId, 162 VariantId,
134} 163}
135 164
136pub 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
142pub trait ToIdByKey: Sized + AstNode + 'static { 165pub trait ToIdByKey: Sized + AstNode + 'static {
143 type ID: Sized + Copy + 'static; 166 type ID: Sized + Copy + 'static;
144 const KEY: Key<Self, Self::ID>; 167 const KEY: Key<Self, Self::ID>;