@@ -62,7 +62,7 @@ use rustc_middle::ty::{self, TyCtxt};
62
62
use rustc_span:: symbol:: { Symbol , sym} ;
63
63
use rustc_span:: { BytePos , DUMMY_SP , FileName , RealFileName } ;
64
64
use serde:: ser:: SerializeSeq as _;
65
- use serde:: { Deserialize , Serialize } ;
65
+ use serde:: { Deserialize , Deserializer , Serialize , Serializer } ;
66
66
use tracing:: { debug, info} ;
67
67
68
68
pub ( crate ) use self :: context:: * ;
@@ -428,69 +428,96 @@ impl IndexItemFunctionType {
428
428
impl Serialize for IndexItemFunctionType {
429
429
fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
430
430
where
431
- S : serde :: Serializer ,
431
+ S : Serializer ,
432
432
{
433
- let mut seq = serializer. serialize_seq ( Some ( 2 ) ) ?;
434
- let mut fn_type = String :: new ( ) ;
435
- self . write_to_string_without_param_names ( & mut fn_type) ;
436
- seq. serialize_element ( & fn_type) ?;
437
-
438
433
struct ParamNames < ' a > ( & ' a [ Option < Symbol > ] ) ;
439
434
440
435
impl < ' a > Serialize for ParamNames < ' a > {
441
436
fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
442
437
where
443
- S : serde :: Serializer ,
438
+ S : Serializer ,
444
439
{
445
440
serializer. collect_seq (
446
441
self . 0
447
442
. iter ( )
448
- . map ( |symbol| symbol. as_ref ( ) . map ( ToString :: to_string ) . unwrap_or_default ( ) ) ,
443
+ . map ( |symbol| symbol. as_ref ( ) . map ( Symbol :: as_str ) . unwrap_or_default ( ) ) ,
449
444
)
450
445
}
451
446
}
452
447
448
+ let mut seq = serializer. serialize_seq ( Some ( 2 ) ) ?;
449
+
450
+ let mut fn_type = String :: new ( ) ;
451
+ self . write_to_string_without_param_names ( & mut fn_type) ;
452
+ seq. serialize_element ( & fn_type) ?;
453
+
453
454
seq. serialize_element ( & ParamNames ( & self . param_names ) ) ?;
455
+
454
456
seq. end ( )
455
457
}
456
458
}
457
459
458
460
impl < ' de > Deserialize < ' de > for IndexItemFunctionType {
459
461
fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
460
462
where
461
- D : serde :: Deserializer < ' de > ,
463
+ D : Deserializer < ' de > ,
462
464
{
463
- use serde:: de:: { self , Error as _ } ;
465
+ use serde:: de:: { self , SeqAccess } ;
464
466
465
- struct FunctionDataVisitor ;
466
- impl < ' de > de:: Visitor < ' de > for FunctionDataVisitor {
467
- type Value = IndexItemFunctionType ;
468
- fn expecting ( & self , formatter : & mut std:: fmt:: Formatter < ' _ > ) -> fmt:: Result {
469
- write ! ( formatter, "fn data" )
470
- }
471
- fn visit_seq < A : de:: SeqAccess < ' de > > ( self , mut v : A ) -> Result < Self :: Value , A :: Error > {
472
- let ( mut function_signature, _) = v
473
- . next_element ( ) ?
474
- . map ( |fn_ : String | {
475
- IndexItemFunctionType :: read_from_string_without_param_names ( fn_. as_bytes ( ) )
476
- } )
477
- . ok_or_else ( || A :: Error :: missing_field ( "function_signature" ) ) ?;
478
- let param_names: Vec < Option < Symbol > > = v
479
- . next_element ( ) ?
480
- . map ( |param_names : Vec < String > | {
481
- param_names
482
- . into_iter ( )
483
- . map ( |symbol| {
484
- if symbol. is_empty ( ) { None } else { Some ( Symbol :: intern ( & symbol) ) }
485
- } )
486
- . collect ( )
487
- } )
488
- . ok_or_else ( || A :: Error :: missing_field ( "param_names" ) ) ?;
489
- function_signature. param_names = param_names;
490
- Ok ( function_signature)
467
+ #[ derive( Deserialize ) ]
468
+ struct Deserialized {
469
+ #[ serde( deserialize_with = "function_signature" ) ]
470
+ function_signature : IndexItemFunctionType ,
471
+ #[ serde( deserialize_with = "param_names" ) ]
472
+ param_names : Vec < Option < Symbol > > ,
473
+ }
474
+
475
+ fn function_signature < ' de , D : Deserializer < ' de > > (
476
+ deserializer : D ,
477
+ ) -> Result < IndexItemFunctionType , D :: Error > {
478
+ String :: deserialize ( deserializer) . map ( |sig| {
479
+ IndexItemFunctionType :: read_from_string_without_param_names ( sig. as_bytes ( ) ) . 0
480
+ } )
481
+ }
482
+
483
+ fn param_names < ' de , D : Deserializer < ' de > > (
484
+ deserializer : D ,
485
+ ) -> Result < Vec < Option < Symbol > > , D :: Error > {
486
+ struct Visitor ;
487
+
488
+ impl < ' de > de:: Visitor < ' de > for Visitor {
489
+ type Value = Vec < Option < Symbol > > ;
490
+
491
+ fn expecting ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
492
+ f. write_str ( "seq of param names" )
493
+ }
494
+
495
+ fn visit_seq < A > ( self , mut seq : A ) -> Result < Self :: Value , A :: Error >
496
+ where
497
+ A : SeqAccess < ' de > ,
498
+ {
499
+ let mut param_names = Vec :: with_capacity ( seq. size_hint ( ) . unwrap_or_default ( ) ) ;
500
+
501
+ while let Some ( symbol) = seq. next_element :: < String > ( ) ? {
502
+ param_names. push ( if symbol. is_empty ( ) {
503
+ None
504
+ } else {
505
+ Some ( Symbol :: intern ( & symbol) )
506
+ } ) ;
507
+ }
508
+
509
+ Ok ( param_names)
510
+ }
491
511
}
512
+
513
+ deserializer. deserialize_seq ( Visitor )
492
514
}
493
- deserializer. deserialize_any ( FunctionDataVisitor )
515
+
516
+ let Deserialized { mut function_signature, param_names } =
517
+ Deserialized :: deserialize ( deserializer) ?;
518
+ function_signature. param_names = param_names;
519
+
520
+ Ok ( function_signature)
494
521
}
495
522
}
496
523
0 commit comments