aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/impl_block.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/impl_block.rs')
-rw-r--r--crates/ra_hir/src/impl_block.rs43
1 files changed, 23 insertions, 20 deletions
diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs
index b2fbee8d7..4d8bdf33a 100644
--- a/crates/ra_hir/src/impl_block.rs
+++ b/crates/ra_hir/src/impl_block.rs
@@ -38,9 +38,9 @@ impl ImplSourceMap {
38 } 38 }
39} 39}
40 40
41#[derive(Debug, Clone, PartialEq, Eq)] 41#[derive(Debug, Clone, Copy, PartialEq, Eq)]
42pub struct ImplBlock { 42pub struct ImplBlock {
43 module_impl_blocks: Arc<ModuleImplBlocks>, 43 module: Module,
44 impl_id: ImplId, 44 impl_id: ImplId,
45} 45}
46 46
@@ -50,42 +50,45 @@ impl ImplBlock {
50 item: ImplItem, 50 item: ImplItem,
51 ) -> Option<ImplBlock> { 51 ) -> Option<ImplBlock> {
52 let impl_id = *module_impl_blocks.impls_by_def.get(&item)?; 52 let impl_id = *module_impl_blocks.impls_by_def.get(&item)?;
53 Some(ImplBlock { module_impl_blocks, impl_id }) 53 Some(ImplBlock { module: module_impl_blocks.module, impl_id })
54 } 54 }
55 55
56 pub(crate) fn from_id(module_impl_blocks: Arc<ModuleImplBlocks>, impl_id: ImplId) -> ImplBlock { 56 pub(crate) fn from_id(module: Module, impl_id: ImplId) -> ImplBlock {
57 ImplBlock { module_impl_blocks, impl_id } 57 ImplBlock { module, impl_id }
58 } 58 }
59 59
60 pub fn id(&self) -> ImplId { 60 /// Returns the syntax of the impl block
61 self.impl_id 61 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::ImplBlock>) {
62 let source_map = db.impls_in_module_source_map(self.module);
63 let (file_id, source) = self.module.definition_source(db);
64 (file_id, source_map.get(&source, self.impl_id))
62 } 65 }
63 66
64 fn impl_data(&self) -> &ImplData { 67 pub fn id(&self) -> ImplId {
65 &self.module_impl_blocks.impls[self.impl_id] 68 self.impl_id
66 } 69 }
67 70
68 pub fn module(&self) -> Module { 71 pub fn module(&self) -> Module {
69 self.module_impl_blocks.module 72 self.module
70 } 73 }
71 74
72 pub fn target_trait_ref(&self) -> Option<&TypeRef> { 75 pub fn target_trait_ref(&self, db: &impl HirDatabase) -> Option<TypeRef> {
73 self.impl_data().target_trait() 76 db.impls_in_module(self.module).impls[self.impl_id].target_trait().cloned()
74 } 77 }
75 78
76 pub fn target_type(&self) -> &TypeRef { 79 pub fn target_type(&self, db: &impl HirDatabase) -> TypeRef {
77 self.impl_data().target_type() 80 db.impls_in_module(self.module).impls[self.impl_id].target_type().clone()
78 } 81 }
79 82
80 pub fn target_ty(&self, db: &impl HirDatabase) -> Ty { 83 pub fn target_ty(&self, db: &impl HirDatabase) -> Ty {
81 Ty::from_hir(db, &self.resolver(db), self.target_type()) 84 Ty::from_hir(db, &self.resolver(db), &self.target_type(db))
82 } 85 }
83 86
84 pub fn target_trait(&self, db: &impl HirDatabase) -> Option<Trait> { 87 pub fn target_trait(&self, db: &impl HirDatabase) -> Option<Trait> {
85 if let Some(TypeRef::Path(path)) = self.target_trait_ref() { 88 if let Some(TypeRef::Path(path)) = self.target_trait_ref(db) {
86 let resolver = self.resolver(db); 89 let resolver = self.resolver(db);
87 if let Some(Resolution::Def(ModuleDef::Trait(tr))) = 90 if let Some(Resolution::Def(ModuleDef::Trait(tr))) =
88 resolver.resolve_path(db, path).take_types() 91 resolver.resolve_path(db, &path).take_types()
89 { 92 {
90 return Some(tr); 93 return Some(tr);
91 } 94 }
@@ -93,8 +96,8 @@ impl ImplBlock {
93 None 96 None
94 } 97 }
95 98
96 pub fn items(&self) -> &[ImplItem] { 99 pub fn items(&self, db: &impl HirDatabase) -> Vec<ImplItem> {
97 self.impl_data().items() 100 db.impls_in_module(self.module).impls[self.impl_id].items().to_vec()
98 } 101 }
99 102
100 pub fn resolver(&self, db: &impl HirDatabase) -> Resolver { 103 pub fn resolver(&self, db: &impl HirDatabase) -> Resolver {
@@ -181,7 +184,7 @@ impl_arena_id!(ImplId);
181/// we don't need to do the second step again. 184/// we don't need to do the second step again.
182#[derive(Debug, PartialEq, Eq)] 185#[derive(Debug, PartialEq, Eq)]
183pub struct ModuleImplBlocks { 186pub struct ModuleImplBlocks {
184 module: Module, 187 pub(crate) module: Module,
185 pub(crate) impls: Arena<ImplId, ImplData>, 188 pub(crate) impls: Arena<ImplId, ImplData>,
186 impls_by_def: FxHashMap<ImplItem, ImplId>, 189 impls_by_def: FxHashMap<ImplItem, ImplId>,
187} 190}