@@ -61,13 +61,29 @@ impl<N: AstNode> FileAstId<N> {
61
61
type ErasedFileAstId = Idx < SyntaxNodePtr > ;
62
62
63
63
/// Maps items' `SyntaxNode`s to `ErasedFileAstId`s and back.
64
- #[ derive( Debug , PartialEq , Eq , Default ) ]
64
+ #[ derive( Default ) ]
65
65
pub struct AstIdMap {
66
66
arena : Arena < SyntaxNodePtr > ,
67
- map : FxHashMap < SyntaxNodePtr , ErasedFileAstId > ,
67
+ /// Reversed mapping lazily derived from [`self.arena`].
68
+ ///
69
+ /// FIXE: Do not store `SyntaxNodePtr` twice.
70
+ map : once_cell:: sync:: OnceCell < FxHashMap < SyntaxNodePtr , ErasedFileAstId > > ,
68
71
_c : Count < Self > ,
69
72
}
70
73
74
+ impl fmt:: Debug for AstIdMap {
75
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
76
+ f. debug_struct ( "AstIdMap" ) . field ( "arena" , & self . arena ) . finish ( )
77
+ }
78
+ }
79
+
80
+ impl PartialEq for AstIdMap {
81
+ fn eq ( & self , other : & Self ) -> bool {
82
+ self . arena == other. arena
83
+ }
84
+ }
85
+ impl Eq for AstIdMap { }
86
+
71
87
impl AstIdMap {
72
88
pub ( crate ) fn from_source ( node : & SyntaxNode ) -> AstIdMap {
73
89
assert ! ( node. parent( ) . is_none( ) ) ;
@@ -91,9 +107,11 @@ impl AstIdMap {
91
107
}
92
108
}
93
109
} ) ;
94
- res. map . extend ( res. arena . iter ( ) . map ( |( idx, ptr) | ( ptr. clone ( ) , idx) ) ) ;
95
110
res
96
111
}
112
+ fn map ( & self ) -> & FxHashMap < SyntaxNodePtr , ErasedFileAstId > {
113
+ self . map . get_or_init ( || self . arena . iter ( ) . map ( |( idx, ptr) | ( ptr. clone ( ) , idx) ) . collect ( ) )
114
+ }
97
115
98
116
pub fn ast_id < N : AstNode > ( & self , item : & N ) -> FileAstId < N > {
99
117
let raw = self . erased_ast_id ( item. syntax ( ) ) ;
@@ -102,7 +120,7 @@ impl AstIdMap {
102
120
103
121
fn erased_ast_id ( & self , item : & SyntaxNode ) -> ErasedFileAstId {
104
122
let ptr = SyntaxNodePtr :: new ( item) ;
105
- * self . map . get ( & ptr) . unwrap_or_else ( || {
123
+ * self . map ( ) . get ( & ptr) . unwrap_or_else ( || {
106
124
panic ! (
107
125
"Can't find {:?} in AstIdMap:\n {:?}" ,
108
126
item,
0 commit comments