@@ -18,7 +18,28 @@ use syntax::{ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr};
1818
1919/// See crates\hir-expand\src\ast_id_map.rs
2020/// 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+ }
2243
2344/// `AstId` points to an AST node in a specific file.
2445pub struct FileAstId < N : AstIdNode > {
@@ -47,7 +68,7 @@ impl<N: AstIdNode> Hash for FileAstId<N> {
4768
4869impl < N : AstIdNode > fmt:: Debug for FileAstId < N > {
4970 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)
5172 }
5273}
5374
@@ -176,7 +197,10 @@ impl AstIdMap {
176197 let ptr = ptr. syntax_node_ptr ( ) ;
177198 let hash = hash_ptr ( & ptr) ;
178199 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+ } ,
180204 None => panic ! (
181205 "Can't find {:?} in AstIdMap:\n {:?}" ,
182206 ptr,
@@ -186,18 +210,19 @@ impl AstIdMap {
186210 }
187211
188212 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 ( )
190215 }
191216
192217 pub fn get_erased ( & self , id : ErasedFileAstId ) -> SyntaxNodePtr {
193- self . arena [ id ]
218+ self . arena [ Idx :: from_raw ( RawIdx :: from_u32 ( id . into_raw ( ) ) ) ]
194219 }
195220
196221 fn erased_ast_id ( & self , item : & SyntaxNode ) -> ErasedFileAstId {
197222 let ptr = SyntaxNodePtr :: new ( item) ;
198223 let hash = hash_ptr ( & ptr) ;
199224 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 ( ) ) ,
201226 None => panic ! (
202227 "Can't find {:?} in AstIdMap:\n {:?}" ,
203228 item,
@@ -207,7 +232,7 @@ impl AstIdMap {
207232 }
208233
209234 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 ( ) )
211236 }
212237}
213238
0 commit comments