diff options
Diffstat (limited to 'crates/ra_hir/src/source_id.rs')
-rw-r--r-- | crates/ra_hir/src/source_id.rs | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/crates/ra_hir/src/source_id.rs b/crates/ra_hir/src/source_id.rs index 1dadd76c5..2c855897a 100644 --- a/crates/ra_hir/src/source_id.rs +++ b/crates/ra_hir/src/source_id.rs | |||
@@ -1,11 +1,11 @@ | |||
1 | use std::{marker::PhantomData, sync::Arc}; | 1 | use std::{marker::PhantomData, sync::Arc, hash::{Hash, Hasher}}; |
2 | 2 | ||
3 | use ra_arena::{Arena, RawId, impl_arena_id}; | 3 | use ra_arena::{Arena, RawId, impl_arena_id}; |
4 | use ra_syntax::{SyntaxNodePtr, TreeArc, SyntaxNode, SourceFile, AstNode, ast}; | 4 | use ra_syntax::{SyntaxNodePtr, TreeArc, SyntaxNode, SourceFile, AstNode, ast}; |
5 | 5 | ||
6 | use crate::{HirFileId, DefDatabase}; | 6 | use crate::{HirFileId, DefDatabase}; |
7 | 7 | ||
8 | #[derive(Debug, PartialEq, Eq, Hash)] | 8 | #[derive(Debug)] |
9 | pub(crate) struct AstId<N: AstNode> { | 9 | pub(crate) struct AstId<N: AstNode> { |
10 | file_id: HirFileId, | 10 | file_id: HirFileId, |
11 | file_ast_id: FileAstId<N>, | 11 | file_ast_id: FileAstId<N>, |
@@ -16,9 +16,20 @@ impl<N: AstNode> Clone for AstId<N> { | |||
16 | *self | 16 | *self |
17 | } | 17 | } |
18 | } | 18 | } |
19 | |||
20 | impl<N: AstNode> Copy for AstId<N> {} | 19 | impl<N: AstNode> Copy for AstId<N> {} |
21 | 20 | ||
21 | impl<N: AstNode> PartialEq for AstId<N> { | ||
22 | fn eq(&self, other: &Self) -> bool { | ||
23 | (self.file_id, self.file_ast_id) == (other.file_id, other.file_ast_id) | ||
24 | } | ||
25 | } | ||
26 | impl<N: AstNode> Eq for AstId<N> {} | ||
27 | impl<N: AstNode> Hash for AstId<N> { | ||
28 | fn hash<H: Hasher>(&self, hasher: &mut H) { | ||
29 | (self.file_id, self.file_ast_id).hash(hasher); | ||
30 | } | ||
31 | } | ||
32 | |||
22 | impl<N: AstNode> AstId<N> { | 33 | impl<N: AstNode> AstId<N> { |
23 | pub(crate) fn file_id(&self) -> HirFileId { | 34 | pub(crate) fn file_id(&self) -> HirFileId { |
24 | self.file_id | 35 | self.file_id |
@@ -30,7 +41,7 @@ impl<N: AstNode> AstId<N> { | |||
30 | } | 41 | } |
31 | } | 42 | } |
32 | 43 | ||
33 | #[derive(Debug, PartialEq, Eq, Hash)] | 44 | #[derive(Debug)] |
34 | pub(crate) struct FileAstId<N: AstNode> { | 45 | pub(crate) struct FileAstId<N: AstNode> { |
35 | raw: SourceFileItemId, | 46 | raw: SourceFileItemId, |
36 | _ty: PhantomData<N>, | 47 | _ty: PhantomData<N>, |
@@ -41,9 +52,20 @@ impl<N: AstNode> Clone for FileAstId<N> { | |||
41 | *self | 52 | *self |
42 | } | 53 | } |
43 | } | 54 | } |
44 | |||
45 | impl<N: AstNode> Copy for FileAstId<N> {} | 55 | impl<N: AstNode> Copy for FileAstId<N> {} |
46 | 56 | ||
57 | impl<N: AstNode> PartialEq for FileAstId<N> { | ||
58 | fn eq(&self, other: &Self) -> bool { | ||
59 | self.raw == other.raw | ||
60 | } | ||
61 | } | ||
62 | impl<N: AstNode> Eq for FileAstId<N> {} | ||
63 | impl<N: AstNode> Hash for FileAstId<N> { | ||
64 | fn hash<H: Hasher>(&self, hasher: &mut H) { | ||
65 | self.raw.hash(hasher); | ||
66 | } | ||
67 | } | ||
68 | |||
47 | impl<N: AstNode> FileAstId<N> { | 69 | impl<N: AstNode> FileAstId<N> { |
48 | pub(crate) fn with_file_id(self, file_id: HirFileId) -> AstId<N> { | 70 | pub(crate) fn with_file_id(self, file_id: HirFileId) -> AstId<N> { |
49 | AstId { file_id, file_ast_id: self } | 71 | AstId { file_id, file_ast_id: self } |
@@ -116,14 +138,6 @@ impl SourceFileItems { | |||
116 | fn alloc(&mut self, item: &SyntaxNode) -> SourceFileItemId { | 138 | fn alloc(&mut self, item: &SyntaxNode) -> SourceFileItemId { |
117 | self.arena.alloc(SyntaxNodePtr::new(item)) | 139 | self.arena.alloc(SyntaxNodePtr::new(item)) |
118 | } | 140 | } |
119 | pub(crate) fn id_of(&self, file_id: HirFileId, item: &SyntaxNode) -> SourceFileItemId { | ||
120 | assert_eq!( | ||
121 | self.file_id, file_id, | ||
122 | "SourceFileItems: wrong file, expected {:?}, got {:?}", | ||
123 | self.file_id, file_id | ||
124 | ); | ||
125 | self.id_of_unchecked(item) | ||
126 | } | ||
127 | pub(crate) fn id_of_unchecked(&self, item: &SyntaxNode) -> SourceFileItemId { | 141 | pub(crate) fn id_of_unchecked(&self, item: &SyntaxNode) -> SourceFileItemId { |
128 | let ptr = SyntaxNodePtr::new(item); | 142 | let ptr = SyntaxNodePtr::new(item); |
129 | if let Some((id, _)) = self.arena.iter().find(|(_id, i)| **i == ptr) { | 143 | if let Some((id, _)) = self.arena.iter().find(|(_id, i)| **i == ptr) { |