@@ -31,7 +31,8 @@ use std::cmp::Ordering;
31
31
use std:: collections:: { BTreeMap , VecDeque } ;
32
32
use std:: default:: Default ;
33
33
use std:: error;
34
- use std:: fmt:: { self , Formatter , Write as FmtWrite } ;
34
+
35
+ use std:: fmt:: { self , Formatter , Write } ;
35
36
use std:: ffi:: OsStr ;
36
37
use std:: fs:: { self , File } ;
37
38
use std:: io:: prelude:: * ;
@@ -42,7 +43,8 @@ use std::sync::Arc;
42
43
use std:: rc:: Rc ;
43
44
44
45
use errors;
45
- use serialize:: json:: { ToJson , Json , as_json} ;
46
+ use serde:: { Serialize , Serializer } ;
47
+ use serde:: ser:: SerializeSeq ;
46
48
use syntax:: ast;
47
49
use syntax:: edition:: Edition ;
48
50
use syntax:: print:: pprust;
@@ -303,19 +305,22 @@ struct IndexItem {
303
305
search_type : Option < IndexItemFunctionType > ,
304
306
}
305
307
306
- impl ToJson for IndexItem {
307
- fn to_json ( & self ) -> Json {
308
+ impl Serialize for IndexItem {
309
+ fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
310
+ where
311
+ S : Serializer ,
312
+ {
308
313
assert_eq ! ( self . parent. is_some( ) , self . parent_idx. is_some( ) ) ;
309
314
310
- let mut data = Vec :: with_capacity ( 6 ) ;
311
- data . push ( ( self . ty as usize ) . to_json ( ) ) ;
312
- data . push ( self . name . to_json ( ) ) ;
313
- data . push ( self . path . to_json ( ) ) ;
314
- data . push ( self . desc . to_json ( ) ) ;
315
- data . push ( self . parent_idx . to_json ( ) ) ;
316
- data . push ( self . search_type . to_json ( ) ) ;
317
-
318
- Json :: Array ( data )
315
+ (
316
+ self . ty ,
317
+ & self . name ,
318
+ & self . path ,
319
+ & self . desc ,
320
+ self . parent_idx ,
321
+ & self . search_type ,
322
+ )
323
+ . serialize ( serializer )
319
324
}
320
325
}
321
326
@@ -326,18 +331,20 @@ struct Type {
326
331
generics : Option < Vec < String > > ,
327
332
}
328
333
329
- impl ToJson for Type {
330
- fn to_json ( & self ) -> Json {
331
- match self . name {
332
- Some ( ref name ) => {
333
- let mut data = Vec :: with_capacity ( 2 ) ;
334
- data . push ( name. to_json ( ) ) ;
335
- if let Some ( ref generics ) = self . generics {
336
- data . push ( generics . to_json ( ) ) ;
337
- }
338
- Json :: Array ( data )
334
+ impl Serialize for Type {
335
+ fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
336
+ where
337
+ S : Serializer ,
338
+ {
339
+ if let Some ( name ) = & self . name {
340
+ let mut seq = serializer . serialize_seq ( None ) ? ;
341
+ seq . serialize_element ( & name ) ? ;
342
+ if let Some ( generics ) = & self . generics {
343
+ seq . serialize_element ( & generics ) ? ;
339
344
}
340
- None => Json :: Null ,
345
+ seq. end ( )
346
+ } else {
347
+ serializer. serialize_none ( )
341
348
}
342
349
}
343
350
}
@@ -349,26 +356,29 @@ struct IndexItemFunctionType {
349
356
output : Option < Vec < Type > > ,
350
357
}
351
358
352
- impl ToJson for IndexItemFunctionType {
353
- fn to_json ( & self ) -> Json {
359
+ impl Serialize for IndexItemFunctionType {
360
+ fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
361
+ where
362
+ S : Serializer ,
363
+ {
354
364
// If we couldn't figure out a type, just write `null`.
355
365
let mut iter = self . inputs . iter ( ) ;
356
366
if match self . output {
357
367
Some ( ref output) => iter. chain ( output. iter ( ) ) . any ( |ref i| i. name . is_none ( ) ) ,
358
368
None => iter. any ( |ref i| i. name . is_none ( ) ) ,
359
369
} {
360
- Json :: Null
370
+ serializer . serialize_none ( )
361
371
} else {
362
- let mut data = Vec :: with_capacity ( 2 ) ;
363
- data . push ( self . inputs . to_json ( ) ) ;
364
- if let Some ( ref output) = self . output {
372
+ let mut seq = serializer . serialize_seq ( None ) ? ;
373
+ seq . serialize_element ( & self . inputs ) ? ;
374
+ if let Some ( output) = & self . output {
365
375
if output. len ( ) > 1 {
366
- data . push ( output. to_json ( ) ) ;
376
+ seq . serialize_element ( & output) ? ;
367
377
} else {
368
- data . push ( output[ 0 ] . to_json ( ) ) ;
378
+ seq . serialize_element ( & output[ 0 ] ) ? ;
369
379
}
370
380
}
371
- Json :: Array ( data )
381
+ seq . end ( )
372
382
}
373
383
}
374
384
}
@@ -596,7 +606,7 @@ fn write_shared(
596
606
// To avoid theme switch latencies as much as possible, we put everything theme related
597
607
// at the beginning of the html files into another js file.
598
608
let theme_js = format ! (
599
- r#"var themes = document.getElementById("theme-choices");
609
+ r#"var themes = document.getElementById("theme-choices");
600
610
var themePicker = document.getElementById("theme-picker");
601
611
602
612
function showThemeButtonState() {{
@@ -642,8 +652,8 @@ themePicker.onblur = handleThemeButtonsBlur;
642
652
}};
643
653
but.onblur = handleThemeButtonsBlur;
644
654
themes.appendChild(but);
645
- }});"# ,
646
- as_json ( & themes ) ) ;
655
+ }});"# , serde_json :: to_string ( & themes ) . unwrap ( ) ) ;
656
+
647
657
write_minify ( & cx. shared . fs , cx. path ( "theme.js" ) ,
648
658
& theme_js,
649
659
options. enable_minification ) ?;
@@ -932,32 +942,48 @@ themePicker.onblur = handleThemeButtonsBlur;
932
942
}
933
943
} ;
934
944
935
- let mut have_impls = false ;
936
- let mut implementors = format ! ( r#"implementors["{}"] = ["# , krate. name) ;
937
- for imp in imps {
938
- // If the trait and implementation are in the same crate, then
939
- // there's no need to emit information about it (there's inlining
940
- // going on). If they're in different crates then the crate defining
941
- // the trait will be interested in our implementation.
942
- if imp. impl_item . def_id . krate == did. krate { continue }
943
- // If the implementation is from another crate then that crate
944
- // should add it.
945
- if !imp. impl_item . def_id . is_local ( ) { continue }
946
- have_impls = true ;
947
- write ! ( implementors, "{{text:{},synthetic:{},types:{}}}," ,
948
- as_json( & imp. inner_impl( ) . print( ) . to_string( ) ) ,
949
- imp. inner_impl( ) . synthetic,
950
- as_json( & collect_paths_for_type( imp. inner_impl( ) . for_. clone( ) ) ) ) . unwrap ( ) ;
945
+ #[ derive( Serialize ) ]
946
+ struct Implementor {
947
+ text : String ,
948
+ synthetic : bool ,
949
+ types : Vec < String > ,
951
950
}
952
- implementors. push_str ( "];" ) ;
951
+
952
+ let implementors = imps
953
+ . iter ( )
954
+ . filter_map ( |imp| {
955
+ // If the trait and implementation are in the same crate, then
956
+ // there's no need to emit information about it (there's inlining
957
+ // going on). If they're in different crates then the crate defining
958
+ // the trait will be interested in our implementation.
959
+ //
960
+ // If the implementation is from another crate then that crate
961
+ // should add it.
962
+ if imp. impl_item . def_id . krate == did. krate || !imp. impl_item . def_id . is_local ( ) {
963
+ None
964
+ } else {
965
+ Some ( Implementor {
966
+ text : imp. inner_impl ( ) . print ( ) . to_string ( ) ,
967
+ synthetic : imp. inner_impl ( ) . synthetic ,
968
+ types : collect_paths_for_type ( imp. inner_impl ( ) . for_ . clone ( ) ) ,
969
+ } )
970
+ }
971
+ } )
972
+ . collect :: < Vec < _ > > ( ) ;
953
973
954
974
// Only create a js file if we have impls to add to it. If the trait is
955
975
// documented locally though we always create the file to avoid dead
956
976
// links.
957
- if !have_impls && !cx. cache . paths . contains_key ( & did) {
977
+ if implementors . is_empty ( ) && !cx. cache . paths . contains_key ( & did) {
958
978
continue ;
959
979
}
960
980
981
+ let implementors = format ! (
982
+ r#"implementors["{}"] = {};"# ,
983
+ krate. name,
984
+ serde_json:: to_string( & implementors) . unwrap( )
985
+ ) ;
986
+
961
987
let mut mydst = dst. clone ( ) ;
962
988
for part in & remote_path[ ..remote_path. len ( ) - 1 ] {
963
989
mydst. push ( part) ;
@@ -1456,7 +1482,7 @@ impl Context {
1456
1482
if !self . render_redirect_pages {
1457
1483
let items = self . build_sidebar_items ( & m) ;
1458
1484
let js_dst = self . dst . join ( "sidebar-items.js" ) ;
1459
- let v = format ! ( "initSidebarItems({});" , as_json ( & items) ) ;
1485
+ let v = format ! ( "initSidebarItems({});" , serde_json :: to_string ( & items) . unwrap ( ) ) ;
1460
1486
scx. fs . write ( & js_dst, & v) ?;
1461
1487
}
1462
1488
@@ -2558,8 +2584,11 @@ fn item_trait(
2558
2584
write_loading_content ( w, "</div>" ) ;
2559
2585
}
2560
2586
}
2561
- write ! ( w, r#"<script type="text/javascript">window.inlined_types=new Set({});</script>"# ,
2562
- as_json( & synthetic_types) ) ;
2587
+ write ! (
2588
+ w,
2589
+ r#"<script type="text/javascript">window.inlined_types=new Set({});</script>"# ,
2590
+ serde_json:: to_string( & synthetic_types) . unwrap( ) ,
2591
+ ) ;
2563
2592
2564
2593
write ! ( w, r#"<script type="text/javascript" async
2565
2594
src="{root_path}/implementors/{path}/{ty}.{name}.js">
0 commit comments