@@ -58,7 +58,7 @@ use rustc_span::{
58
58
symbol:: { sym, Symbol } ,
59
59
BytePos , FileName , RealFileName ,
60
60
} ;
61
- use serde:: ser:: { SerializeMap , SerializeSeq } ;
61
+ use serde:: ser:: SerializeMap ;
62
62
use serde:: { Serialize , Serializer } ;
63
63
64
64
use crate :: clean:: { self , ItemId , RenderedLink , SelfTy } ;
@@ -123,115 +123,157 @@ pub(crate) struct IndexItem {
123
123
}
124
124
125
125
/// A type used for the search index.
126
- #[ derive( Debug ) ]
126
+ #[ derive( Debug , Eq , PartialEq ) ]
127
127
pub ( crate ) struct RenderType {
128
128
id : Option < RenderTypeId > ,
129
129
generics : Option < Vec < RenderType > > ,
130
130
bindings : Option < Vec < ( RenderTypeId , Vec < RenderType > ) > > ,
131
131
}
132
132
133
- impl Serialize for RenderType {
134
- fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
135
- where
136
- S : Serializer ,
137
- {
138
- let id = match & self . id {
139
- // 0 is a sentinel, everything else is one-indexed
140
- None => 0 ,
141
- // concrete type
142
- Some ( RenderTypeId :: Index ( idx) ) if * idx >= 0 => idx + 1 ,
143
- // generic type parameter
144
- Some ( RenderTypeId :: Index ( idx) ) => * idx,
145
- _ => panic ! ( "must convert render types to indexes before serializing" ) ,
146
- } ;
133
+ impl RenderType {
134
+ pub fn write_to_string ( & self , string : & mut String ) {
135
+ // 0 is a sentinel, everything else is one-indexed
136
+ let id = self . id . unwrap_or ( RenderTypeId :: Index ( 0 ) ) ;
147
137
if self . generics . is_some ( ) || self . bindings . is_some ( ) {
148
- let mut seq = serializer. serialize_seq ( None ) ?;
149
- seq. serialize_element ( & id) ?;
150
- seq. serialize_element ( self . generics . as_ref ( ) . map ( Vec :: as_slice) . unwrap_or_default ( ) ) ?;
138
+ string. push ( '{' ) ;
139
+ id. write_to_string ( string) ;
140
+ string. push ( '{' ) ;
141
+ for generic in & self . generics . as_ref ( ) . map ( Vec :: as_slice) . unwrap_or_default ( ) [ ..] {
142
+ generic. write_to_string ( string) ;
143
+ }
144
+ string. push ( '}' ) ;
151
145
if self . bindings . is_some ( ) {
152
- seq. serialize_element (
153
- self . bindings . as_ref ( ) . map ( Vec :: as_slice) . unwrap_or_default ( ) ,
154
- ) ?;
146
+ string. push ( '{' ) ;
147
+ for binding in & self . bindings . as_ref ( ) . map ( Vec :: as_slice) . unwrap_or_default ( ) [ ..] {
148
+ string. push ( '{' ) ;
149
+ binding. 0 . write_to_string ( string) ;
150
+ string. push ( '{' ) ;
151
+ for constraint in & binding. 1 [ ..] {
152
+ constraint. write_to_string ( string) ;
153
+ }
154
+ string. push ( '}' ) ;
155
+ string. push ( '}' ) ;
156
+ }
157
+ string. push ( '}' ) ;
155
158
}
156
- seq . end ( )
159
+ string . push ( '}' ) ;
157
160
} else {
158
- id. serialize ( serializer )
161
+ id. write_to_string ( string )
159
162
}
160
163
}
161
164
}
162
165
163
- #[ derive( Clone , Copy , Debug ) ]
166
+ #[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
164
167
pub ( crate ) enum RenderTypeId {
165
168
DefId ( DefId ) ,
166
169
Primitive ( clean:: PrimitiveType ) ,
167
170
AssociatedType ( Symbol ) ,
168
171
Index ( isize ) ,
169
172
}
170
173
171
- impl Serialize for RenderTypeId {
172
- fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
173
- where
174
- S : Serializer ,
175
- {
176
- let id = match & self {
174
+ impl RenderTypeId {
175
+ pub fn write_to_string ( & self , string : & mut String ) {
176
+ // (sign, value)
177
+ let ( sign, id) : ( bool , u32 ) = match & self {
177
178
// 0 is a sentinel, everything else is one-indexed
178
179
// concrete type
179
- RenderTypeId :: Index ( idx) if * idx >= 0 => idx + 1 ,
180
+ RenderTypeId :: Index ( idx) if * idx >= 0 => ( false , ( idx + 1isize ) . try_into ( ) . unwrap ( ) ) ,
180
181
// generic type parameter
181
- RenderTypeId :: Index ( idx) => * idx,
182
+ RenderTypeId :: Index ( idx) => ( true , ( - * idx) . try_into ( ) . unwrap ( ) ) ,
182
183
_ => panic ! ( "must convert render types to indexes before serializing" ) ,
183
184
} ;
184
- id. serialize ( serializer)
185
+ // zig-zag notation
186
+ let value: u32 = ( id << 1 ) | ( if sign { 1 } else { 0 } ) ;
187
+ // encode
188
+ let mut shift: u32 = 28 ;
189
+ let mut mask: u32 = 0xF0_00_00_00 ;
190
+ while shift < 32 {
191
+ let hexit = ( value & mask) >> shift;
192
+ if hexit != 0 || shift == 0 {
193
+ let hex =
194
+ char:: try_from ( if shift == 0 { '`' } else { '@' } as u32 + hexit) . unwrap ( ) ;
195
+ string. push ( hex) ;
196
+ }
197
+ shift = shift. wrapping_sub ( 4 ) ;
198
+ mask = mask >> 4 ;
199
+ }
185
200
}
186
201
}
187
202
188
203
/// Full type of functions/methods in the search index.
189
- #[ derive( Debug ) ]
204
+ #[ derive( Debug , Eq , PartialEq ) ]
190
205
pub ( crate ) struct IndexItemFunctionType {
191
206
inputs : Vec < RenderType > ,
192
207
output : Vec < RenderType > ,
193
208
where_clause : Vec < Vec < RenderType > > ,
194
209
}
195
210
196
- impl Serialize for IndexItemFunctionType {
197
- fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
198
- where
199
- S : Serializer ,
200
- {
211
+ impl IndexItemFunctionType {
212
+ pub fn write_to_string < ' a > (
213
+ & ' a self ,
214
+ string : & mut String ,
215
+ backref_queue : & mut VecDeque < & ' a IndexItemFunctionType > ,
216
+ ) {
217
+ assert ! ( backref_queue. len( ) < 16 ) ;
201
218
// If we couldn't figure out a type, just write `0`.
202
219
let has_missing = self
203
220
. inputs
204
221
. iter ( )
205
222
. chain ( self . output . iter ( ) )
206
223
. any ( |i| i. id . is_none ( ) && i. generics . is_none ( ) ) ;
207
224
if has_missing {
208
- 0 . serialize ( serializer)
225
+ string. push ( '@' ) ;
226
+ } else if let Some ( idx) = backref_queue. iter ( ) . position ( |other| * other == self ) {
227
+ string. push (
228
+ char:: try_from ( '0' as u32 + u32:: try_from ( idx) . unwrap ( ) )
229
+ . expect ( "last possible value is '?'" ) ,
230
+ ) ;
209
231
} else {
210
- let mut seq = serializer. serialize_seq ( None ) ?;
232
+ backref_queue. push_front ( self ) ;
233
+ if backref_queue. len ( ) >= 16 {
234
+ backref_queue. pop_back ( ) ;
235
+ }
236
+ string. push ( '{' ) ;
211
237
match & self . inputs [ ..] {
212
238
[ one] if one. generics . is_none ( ) && one. bindings . is_none ( ) => {
213
- seq. serialize_element ( one) ?
239
+ one. write_to_string ( string) ;
240
+ }
241
+ _ => {
242
+ string. push ( '{' ) ;
243
+ for item in & self . inputs [ ..] {
244
+ item. write_to_string ( string) ;
245
+ }
246
+ string. push ( '}' ) ;
214
247
}
215
- _ => seq. serialize_element ( & self . inputs ) ?,
216
248
}
217
249
match & self . output [ ..] {
218
250
[ ] if self . where_clause . is_empty ( ) => { }
219
251
[ one] if one. generics . is_none ( ) && one. bindings . is_none ( ) => {
220
- seq. serialize_element ( one) ?
252
+ one. write_to_string ( string) ;
253
+ }
254
+ _ => {
255
+ string. push ( '{' ) ;
256
+ for item in & self . output [ ..] {
257
+ item. write_to_string ( string) ;
258
+ }
259
+ string. push ( '}' ) ;
221
260
}
222
- _ => seq. serialize_element ( & self . output ) ?,
223
261
}
224
262
for constraint in & self . where_clause {
225
263
if let [ one] = & constraint[ ..]
226
264
&& one. generics . is_none ( )
227
265
&& one. bindings . is_none ( )
228
266
{
229
- seq . serialize_element ( one ) ? ;
267
+ one . write_to_string ( string ) ;
230
268
} else {
231
- seq. serialize_element ( constraint) ?;
269
+ string. push ( '{' ) ;
270
+ for item in & constraint[ ..] {
271
+ item. write_to_string ( string) ;
272
+ }
273
+ string. push ( '}' ) ;
232
274
}
233
275
}
234
- seq . end ( )
276
+ string . push ( '}' ) ;
235
277
}
236
278
}
237
279
}
0 commit comments