aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/has_source.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-03-13 15:05:46 +0000
committerAleksey Kladov <[email protected]>2020-03-16 16:42:30 +0000
commit9faea2364dee4fbc9391ad233c570b70256ef002 (patch)
tree160af959553ce57fdfcbc0a6c79bafcc3611aeea /crates/ra_hir/src/has_source.rs
parent648df02953a6ebf87a5876668eceba208687e8a7 (diff)
Use `dyn Trait` for working with databse
It improves compile time in `--release` mode quite a bit, it doesn't really slow things down and, conceptually, it seems closer to what we want the physical architecture to look like (we don't want to monomorphise EVERYTHING in a single leaf crate).
Diffstat (limited to 'crates/ra_hir/src/has_source.rs')
-rw-r--r--crates/ra_hir/src/has_source.rs64
1 files changed, 32 insertions, 32 deletions
diff --git a/crates/ra_hir/src/has_source.rs b/crates/ra_hir/src/has_source.rs
index f121e1eff..129764e0a 100644
--- a/crates/ra_hir/src/has_source.rs
+++ b/crates/ra_hir/src/has_source.rs
@@ -9,7 +9,7 @@ use hir_def::{
9use ra_syntax::ast; 9use ra_syntax::ast;
10 10
11use crate::{ 11use crate::{
12 db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplDef, MacroDef, Module, 12 db::HirDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplDef, MacroDef, Module,
13 Static, Struct, StructField, Trait, TypeAlias, TypeParam, Union, 13 Static, Struct, StructField, Trait, TypeAlias, TypeParam, Union,
14}; 14};
15 15
@@ -17,31 +17,31 @@ pub use hir_expand::InFile;
17 17
18pub trait HasSource { 18pub trait HasSource {
19 type Ast; 19 type Ast;
20 fn source(self, db: &impl DefDatabase) -> InFile<Self::Ast>; 20 fn source(self, db: &dyn HirDatabase) -> InFile<Self::Ast>;
21} 21}
22 22
23/// NB: Module is !HasSource, because it has two source nodes at the same time: 23/// NB: Module is !HasSource, because it has two source nodes at the same time:
24/// definition and declaration. 24/// definition and declaration.
25impl Module { 25impl Module {
26 /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. 26 /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
27 pub fn definition_source(self, db: &impl DefDatabase) -> InFile<ModuleSource> { 27 pub fn definition_source(self, db: &dyn HirDatabase) -> InFile<ModuleSource> {
28 let def_map = db.crate_def_map(self.id.krate); 28 let def_map = db.crate_def_map(self.id.krate);
29 def_map[self.id.local_id].definition_source(db) 29 def_map[self.id.local_id].definition_source(db.upcast())
30 } 30 }
31 31
32 /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. 32 /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
33 /// `None` for the crate root. 33 /// `None` for the crate root.
34 pub fn declaration_source(self, db: &impl DefDatabase) -> Option<InFile<ast::Module>> { 34 pub fn declaration_source(self, db: &dyn HirDatabase) -> Option<InFile<ast::Module>> {
35 let def_map = db.crate_def_map(self.id.krate); 35 let def_map = db.crate_def_map(self.id.krate);
36 def_map[self.id.local_id].declaration_source(db) 36 def_map[self.id.local_id].declaration_source(db.upcast())
37 } 37 }
38} 38}
39 39
40impl HasSource for StructField { 40impl HasSource for StructField {
41 type Ast = FieldSource; 41 type Ast = FieldSource;
42 fn source(self, db: &impl DefDatabase) -> InFile<FieldSource> { 42 fn source(self, db: &dyn HirDatabase) -> InFile<FieldSource> {
43 let var = VariantId::from(self.parent); 43 let var = VariantId::from(self.parent);
44 let src = var.child_source(db); 44 let src = var.child_source(db.upcast());
45 src.map(|it| match it[self.id].clone() { 45 src.map(|it| match it[self.id].clone() {
46 Either::Left(it) => FieldSource::Pos(it), 46 Either::Left(it) => FieldSource::Pos(it),
47 Either::Right(it) => FieldSource::Named(it), 47 Either::Right(it) => FieldSource::Named(it),
@@ -50,78 +50,78 @@ impl HasSource for StructField {
50} 50}
51impl HasSource for Struct { 51impl HasSource for Struct {
52 type Ast = ast::StructDef; 52 type Ast = ast::StructDef;
53 fn source(self, db: &impl DefDatabase) -> InFile<ast::StructDef> { 53 fn source(self, db: &dyn HirDatabase) -> InFile<ast::StructDef> {
54 self.id.lookup(db).source(db) 54 self.id.lookup(db.upcast()).source(db.upcast())
55 } 55 }
56} 56}
57impl HasSource for Union { 57impl HasSource for Union {
58 type Ast = ast::UnionDef; 58 type Ast = ast::UnionDef;
59 fn source(self, db: &impl DefDatabase) -> InFile<ast::UnionDef> { 59 fn source(self, db: &dyn HirDatabase) -> InFile<ast::UnionDef> {
60 self.id.lookup(db).source(db) 60 self.id.lookup(db.upcast()).source(db.upcast())
61 } 61 }
62} 62}
63impl HasSource for Enum { 63impl HasSource for Enum {
64 type Ast = ast::EnumDef; 64 type Ast = ast::EnumDef;
65 fn source(self, db: &impl DefDatabase) -> InFile<ast::EnumDef> { 65 fn source(self, db: &dyn HirDatabase) -> InFile<ast::EnumDef> {
66 self.id.lookup(db).source(db) 66 self.id.lookup(db.upcast()).source(db.upcast())
67 } 67 }
68} 68}
69impl HasSource for EnumVariant { 69impl HasSource for EnumVariant {
70 type Ast = ast::EnumVariant; 70 type Ast = ast::EnumVariant;
71 fn source(self, db: &impl DefDatabase) -> InFile<ast::EnumVariant> { 71 fn source(self, db: &dyn HirDatabase) -> InFile<ast::EnumVariant> {
72 self.parent.id.child_source(db).map(|map| map[self.id].clone()) 72 self.parent.id.child_source(db.upcast()).map(|map| map[self.id].clone())
73 } 73 }
74} 74}
75impl HasSource for Function { 75impl HasSource for Function {
76 type Ast = ast::FnDef; 76 type Ast = ast::FnDef;
77 fn source(self, db: &impl DefDatabase) -> InFile<ast::FnDef> { 77 fn source(self, db: &dyn HirDatabase) -> InFile<ast::FnDef> {
78 self.id.lookup(db).source(db) 78 self.id.lookup(db.upcast()).source(db.upcast())
79 } 79 }
80} 80}
81impl HasSource for Const { 81impl HasSource for Const {
82 type Ast = ast::ConstDef; 82 type Ast = ast::ConstDef;
83 fn source(self, db: &impl DefDatabase) -> InFile<ast::ConstDef> { 83 fn source(self, db: &dyn HirDatabase) -> InFile<ast::ConstDef> {
84 self.id.lookup(db).source(db) 84 self.id.lookup(db.upcast()).source(db.upcast())
85 } 85 }
86} 86}
87impl HasSource for Static { 87impl HasSource for Static {
88 type Ast = ast::StaticDef; 88 type Ast = ast::StaticDef;
89 fn source(self, db: &impl DefDatabase) -> InFile<ast::StaticDef> { 89 fn source(self, db: &dyn HirDatabase) -> InFile<ast::StaticDef> {
90 self.id.lookup(db).source(db) 90 self.id.lookup(db.upcast()).source(db.upcast())
91 } 91 }
92} 92}
93impl HasSource for Trait { 93impl HasSource for Trait {
94 type Ast = ast::TraitDef; 94 type Ast = ast::TraitDef;
95 fn source(self, db: &impl DefDatabase) -> InFile<ast::TraitDef> { 95 fn source(self, db: &dyn HirDatabase) -> InFile<ast::TraitDef> {
96 self.id.lookup(db).source(db) 96 self.id.lookup(db.upcast()).source(db.upcast())
97 } 97 }
98} 98}
99impl HasSource for TypeAlias { 99impl HasSource for TypeAlias {
100 type Ast = ast::TypeAliasDef; 100 type Ast = ast::TypeAliasDef;
101 fn source(self, db: &impl DefDatabase) -> InFile<ast::TypeAliasDef> { 101 fn source(self, db: &dyn HirDatabase) -> InFile<ast::TypeAliasDef> {
102 self.id.lookup(db).source(db) 102 self.id.lookup(db.upcast()).source(db.upcast())
103 } 103 }
104} 104}
105impl HasSource for MacroDef { 105impl HasSource for MacroDef {
106 type Ast = ast::MacroCall; 106 type Ast = ast::MacroCall;
107 fn source(self, db: &impl DefDatabase) -> InFile<ast::MacroCall> { 107 fn source(self, db: &dyn HirDatabase) -> InFile<ast::MacroCall> {
108 InFile { 108 InFile {
109 file_id: self.id.ast_id.expect("MacroDef without ast_id").file_id, 109 file_id: self.id.ast_id.expect("MacroDef without ast_id").file_id,
110 value: self.id.ast_id.expect("MacroDef without ast_id").to_node(db), 110 value: self.id.ast_id.expect("MacroDef without ast_id").to_node(db.upcast()),
111 } 111 }
112 } 112 }
113} 113}
114impl HasSource for ImplDef { 114impl HasSource for ImplDef {
115 type Ast = ast::ImplDef; 115 type Ast = ast::ImplDef;
116 fn source(self, db: &impl DefDatabase) -> InFile<ast::ImplDef> { 116 fn source(self, db: &dyn HirDatabase) -> InFile<ast::ImplDef> {
117 self.id.lookup(db).source(db) 117 self.id.lookup(db.upcast()).source(db.upcast())
118 } 118 }
119} 119}
120 120
121impl HasSource for TypeParam { 121impl HasSource for TypeParam {
122 type Ast = Either<ast::TraitDef, ast::TypeParam>; 122 type Ast = Either<ast::TraitDef, ast::TypeParam>;
123 fn source(self, db: &impl DefDatabase) -> InFile<Self::Ast> { 123 fn source(self, db: &dyn HirDatabase) -> InFile<Self::Ast> {
124 let child_source = self.id.parent.child_source(db); 124 let child_source = self.id.parent.child_source(db.upcast());
125 child_source.map(|it| it[self.id.local_id].clone()) 125 child_source.map(|it| it[self.id.local_id].clone())
126 } 126 }
127} 127}