@@ -329,6 +329,10 @@ pub struct Cache {
329
329
// yet when its implementation methods are being indexed. Caches such methods
330
330
// and their parent id here and indexes them at the end of crate parsing.
331
331
orphan_impl_items : Vec < ( DefId , clean:: Item ) > ,
332
+
333
+ /// Aliases added through `#[doc(alias = "...")]`. Since a few items can have the same alias,
334
+ /// we need the alias element to have an array of items.
335
+ aliases : FxHashMap < String , Vec < IndexItem > > ,
332
336
}
333
337
334
338
/// Temporary storage for data obtained during `RustdocVisitor::clean()`.
@@ -369,6 +373,7 @@ struct Sidebar<'a> { cx: &'a Context, item: &'a clean::Item, }
369
373
370
374
/// Struct representing one entry in the JS search index. These are all emitted
371
375
/// by hand to a large JS file at the end of cache-creation.
376
+ #[ derive( Debug ) ]
372
377
struct IndexItem {
373
378
ty : ItemType ,
374
379
name : String ,
@@ -396,6 +401,7 @@ impl ToJson for IndexItem {
396
401
}
397
402
398
403
/// A type used for the search index.
404
+ #[ derive( Debug ) ]
399
405
struct Type {
400
406
name : Option < String > ,
401
407
generics : Option < Vec < String > > ,
@@ -418,9 +424,10 @@ impl ToJson for Type {
418
424
}
419
425
420
426
/// Full type of functions/methods in the search index.
427
+ #[ derive( Debug ) ]
421
428
struct IndexItemFunctionType {
422
429
inputs : Vec < Type > ,
423
- output : Option < Type >
430
+ output : Option < Type > ,
424
431
}
425
432
426
433
impl ToJson for IndexItemFunctionType {
@@ -609,6 +616,7 @@ pub fn run(mut krate: clean::Crate,
609
616
owned_box_did,
610
617
masked_crates : mem:: replace ( & mut krate. masked_crates , FxHashSet ( ) ) ,
611
618
typarams : external_typarams,
619
+ aliases : FxHashMap ( ) ,
612
620
} ;
613
621
614
622
// Cache where all our extern crates are located
@@ -847,8 +855,7 @@ themePicker.onclick = function() {{
847
855
write ( cx. dst . join ( "COPYRIGHT.txt" ) ,
848
856
include_bytes ! ( "static/COPYRIGHT.txt" ) ) ?;
849
857
850
- fn collect ( path : & Path , krate : & str ,
851
- key : & str ) -> io:: Result < Vec < String > > {
858
+ fn collect ( path : & Path , krate : & str , key : & str ) -> io:: Result < Vec < String > > {
852
859
let mut ret = Vec :: new ( ) ;
853
860
if path. exists ( ) {
854
861
for line in BufReader :: new ( File :: open ( path) ?) . lines ( ) {
@@ -865,6 +872,40 @@ themePicker.onclick = function() {{
865
872
Ok ( ret)
866
873
}
867
874
875
+ fn show_item ( item : & IndexItem , krate : & str ) -> String {
876
+ format ! ( "{{'crate':'{}','ty':{},'name':'{}','path':'{}'{}}}" ,
877
+ krate, item. ty as usize , item. name, item. path,
878
+ if let Some ( p) = item. parent_idx {
879
+ format!( ",'parent':{}" , p)
880
+ } else {
881
+ String :: new( )
882
+ } )
883
+ }
884
+
885
+ let dst = cx. dst . join ( "aliases.js" ) ;
886
+ {
887
+ let mut all_aliases = try_err ! ( collect( & dst, & krate. name, "ALIASES" ) , & dst) ;
888
+ let mut w = try_err ! ( File :: create( & dst) , & dst) ;
889
+ let mut output = String :: with_capacity ( 100 ) ;
890
+ for ( alias, items) in & cache. aliases {
891
+ if items. is_empty ( ) {
892
+ continue
893
+ }
894
+ output. push_str ( & format ! ( "\" {}\" :[{}]," ,
895
+ alias,
896
+ items. iter( )
897
+ . map( |v| show_item( v, & krate. name) )
898
+ . collect:: <Vec <_>>( )
899
+ . join( "," ) ) ) ;
900
+ }
901
+ all_aliases. push ( format ! ( "ALIASES['{}'] = {{{}}};" , krate. name, output) ) ;
902
+ all_aliases. sort ( ) ;
903
+ try_err ! ( writeln!( & mut w, "var ALIASES = {{}};" ) , & dst) ;
904
+ for aliases in & all_aliases {
905
+ try_err ! ( writeln!( & mut w, "{}" , aliases) , & dst) ;
906
+ }
907
+ }
908
+
868
909
// Update the search index
869
910
let dst = cx. dst . join ( "search-index.js" ) ;
870
911
let mut all_indexes = try_err ! ( collect( & dst, & krate. name, "searchIndex" ) , & dst) ;
@@ -1251,13 +1292,13 @@ impl DocFolder for Cache {
1251
1292
// `public_items` map, so we can skip inserting into the
1252
1293
// paths map if there was already an entry present and we're
1253
1294
// not a public item.
1254
- if
1255
- !self . paths . contains_key ( & item. def_id ) ||
1256
- self . access_levels . is_public ( item. def_id )
1295
+ if !self . paths . contains_key ( & item. def_id ) ||
1296
+ self . access_levels . is_public ( item. def_id )
1257
1297
{
1258
1298
self . paths . insert ( item. def_id ,
1259
1299
( self . stack . clone ( ) , item. type_ ( ) ) ) ;
1260
1300
}
1301
+ self . add_aliases ( & item) ;
1261
1302
}
1262
1303
// Link variants to their parent enum because pages aren't emitted
1263
1304
// for each variant.
@@ -1268,6 +1309,7 @@ impl DocFolder for Cache {
1268
1309
}
1269
1310
1270
1311
clean:: PrimitiveItem ( ..) if item. visibility . is_some ( ) => {
1312
+ self . add_aliases ( & item) ;
1271
1313
self . paths . insert ( item. def_id , ( self . stack . clone ( ) ,
1272
1314
item. type_ ( ) ) ) ;
1273
1315
}
@@ -1372,6 +1414,36 @@ impl<'a> Cache {
1372
1414
}
1373
1415
}
1374
1416
}
1417
+
1418
+ fn add_aliases ( & mut self , item : & clean:: Item ) {
1419
+ if item. def_id . index == CRATE_DEF_INDEX {
1420
+ return
1421
+ }
1422
+ if let Some ( ref item_name) = item. name {
1423
+ let path = self . paths . get ( & item. def_id )
1424
+ . map ( |p| p. 0 . join ( "::" ) . to_string ( ) )
1425
+ . unwrap_or ( "std" . to_owned ( ) ) ;
1426
+ for alias in item. attrs . lists ( "doc" )
1427
+ . filter ( |a| a. check_name ( "alias" ) )
1428
+ . filter_map ( |a| a. value_str ( )
1429
+ . map ( |s| s. to_string ( ) . replace ( "\" " , "" ) ) )
1430
+ . filter ( |v| !v. is_empty ( ) )
1431
+ . collect :: < FxHashSet < _ > > ( )
1432
+ . into_iter ( ) {
1433
+ self . aliases . entry ( alias)
1434
+ . or_insert ( Vec :: with_capacity ( 1 ) )
1435
+ . push ( IndexItem {
1436
+ ty : item. type_ ( ) ,
1437
+ name : item_name. to_string ( ) ,
1438
+ path : path. clone ( ) ,
1439
+ desc : String :: new ( ) ,
1440
+ parent : None ,
1441
+ parent_idx : None ,
1442
+ search_type : get_index_search_type ( & item) ,
1443
+ } ) ;
1444
+ }
1445
+ }
1446
+ }
1375
1447
}
1376
1448
1377
1449
#[ derive( Debug , Eq , PartialEq , Hash ) ]
0 commit comments