@@ -18,7 +18,28 @@ use syntax::{ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr};
18
18
19
19
/// See crates\hir-expand\src\ast_id_map.rs
20
20
/// This is a type erased FileAstId.
21
- pub type ErasedFileAstId = la_arena:: Idx < syntax:: SyntaxNodePtr > ;
21
+ #[ derive( Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
22
+ pub struct ErasedFileAstId ( u32 ) ;
23
+
24
+ impl ErasedFileAstId {
25
+ pub const fn into_raw ( self ) -> u32 {
26
+ self . 0
27
+ }
28
+ pub const fn from_raw ( u32 : u32 ) -> Self {
29
+ Self ( u32)
30
+ }
31
+ }
32
+
33
+ impl fmt:: Display for ErasedFileAstId {
34
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
35
+ self . 0 . fmt ( f)
36
+ }
37
+ }
38
+ impl fmt:: Debug for ErasedFileAstId {
39
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
40
+ self . 0 . fmt ( f)
41
+ }
42
+ }
22
43
23
44
/// `AstId` points to an AST node in a specific file.
24
45
pub struct FileAstId < N : AstIdNode > {
@@ -47,7 +68,7 @@ impl<N: AstIdNode> Hash for FileAstId<N> {
47
68
48
69
impl < N : AstIdNode > fmt:: Debug for FileAstId < N > {
49
70
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
50
- write ! ( f, "FileAstId::<{}>({})" , type_name:: <N >( ) , self . raw. into_raw ( ) )
71
+ write ! ( f, "FileAstId::<{}>({})" , type_name:: <N >( ) , self . raw)
51
72
}
52
73
}
53
74
@@ -176,7 +197,10 @@ impl AstIdMap {
176
197
let ptr = ptr. syntax_node_ptr ( ) ;
177
198
let hash = hash_ptr ( & ptr) ;
178
199
match self . map . raw_entry ( ) . from_hash ( hash, |& idx| self . arena [ idx] == ptr) {
179
- Some ( ( & raw, & ( ) ) ) => FileAstId { raw, covariant : PhantomData } ,
200
+ Some ( ( & raw, & ( ) ) ) => FileAstId {
201
+ raw : ErasedFileAstId ( raw. into_raw ( ) . into_u32 ( ) ) ,
202
+ covariant : PhantomData ,
203
+ } ,
180
204
None => panic ! (
181
205
"Can't find {:?} in AstIdMap:\n {:?}" ,
182
206
ptr,
@@ -186,18 +210,19 @@ impl AstIdMap {
186
210
}
187
211
188
212
pub fn get < N : AstIdNode > ( & self , id : FileAstId < N > ) -> AstPtr < N > {
189
- AstPtr :: try_from_raw ( self . arena [ id. raw ] ) . unwrap ( )
213
+ AstPtr :: try_from_raw ( self . arena [ Idx :: from_raw ( RawIdx :: from_u32 ( id. raw . into_raw ( ) ) ) ] )
214
+ . unwrap ( )
190
215
}
191
216
192
217
pub fn get_erased ( & self , id : ErasedFileAstId ) -> SyntaxNodePtr {
193
- self . arena [ id ]
218
+ self . arena [ Idx :: from_raw ( RawIdx :: from_u32 ( id . into_raw ( ) ) ) ]
194
219
}
195
220
196
221
fn erased_ast_id ( & self , item : & SyntaxNode ) -> ErasedFileAstId {
197
222
let ptr = SyntaxNodePtr :: new ( item) ;
198
223
let hash = hash_ptr ( & ptr) ;
199
224
match self . map . raw_entry ( ) . from_hash ( hash, |& idx| self . arena [ idx] == ptr) {
200
- Some ( ( & idx, & ( ) ) ) => idx,
225
+ Some ( ( & idx, & ( ) ) ) => ErasedFileAstId ( idx. into_raw ( ) . into_u32 ( ) ) ,
201
226
None => panic ! (
202
227
"Can't find {:?} in AstIdMap:\n {:?}" ,
203
228
item,
@@ -207,7 +232,7 @@ impl AstIdMap {
207
232
}
208
233
209
234
fn alloc ( & mut self , item : & SyntaxNode ) -> ErasedFileAstId {
210
- self . arena . alloc ( SyntaxNodePtr :: new ( item) )
235
+ ErasedFileAstId ( self . arena . alloc ( SyntaxNodePtr :: new ( item) ) . into_raw ( ) . into_u32 ( ) )
211
236
}
212
237
}
213
238
0 commit comments