@@ -11,7 +11,12 @@ use lib::*;
11
11
use ser:: { self , Impossible , Serialize , SerializeMap , SerializeStruct , Serializer } ;
12
12
13
13
#[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
14
- use self :: content:: { SerializeStructVariantAsMapValue , SerializeTupleVariantAsMapValue } ;
14
+ use self :: content:: {
15
+ SerializeStructVariantAsMapValue ,
16
+ SerializeTupleVariantAsMapValue ,
17
+ ContentSerializer ,
18
+ Content ,
19
+ } ;
15
20
16
21
/// Used to check that serde(getter) attributes return the expected type.
17
22
/// Not public API.
@@ -461,7 +466,7 @@ mod content {
461
466
}
462
467
463
468
#[ derive( Debug ) ]
464
- enum Content {
469
+ pub enum Content {
465
470
Bool ( bool ) ,
466
471
467
472
U8 ( u8 ) ,
@@ -586,12 +591,12 @@ mod content {
586
591
}
587
592
}
588
593
589
- struct ContentSerializer < E > {
594
+ pub struct ContentSerializer < E > {
590
595
error : PhantomData < E > ,
591
596
}
592
597
593
598
impl < E > ContentSerializer < E > {
594
- fn new ( ) -> Self {
599
+ pub fn new ( ) -> Self {
595
600
ContentSerializer { error : PhantomData }
596
601
}
597
602
}
@@ -806,7 +811,7 @@ mod content {
806
811
}
807
812
}
808
813
809
- struct SerializeSeq < E > {
814
+ pub struct SerializeSeq < E > {
810
815
elements : Vec < Content > ,
811
816
error : PhantomData < E > ,
812
817
}
@@ -832,7 +837,7 @@ mod content {
832
837
}
833
838
}
834
839
835
- struct SerializeTuple < E > {
840
+ pub struct SerializeTuple < E > {
836
841
elements : Vec < Content > ,
837
842
error : PhantomData < E > ,
838
843
}
@@ -858,7 +863,7 @@ mod content {
858
863
}
859
864
}
860
865
861
- struct SerializeTupleStruct < E > {
866
+ pub struct SerializeTupleStruct < E > {
862
867
name : & ' static str ,
863
868
fields : Vec < Content > ,
864
869
error : PhantomData < E > ,
@@ -885,7 +890,7 @@ mod content {
885
890
}
886
891
}
887
892
888
- struct SerializeTupleVariant < E > {
893
+ pub struct SerializeTupleVariant < E > {
889
894
name : & ' static str ,
890
895
variant_index : u32 ,
891
896
variant : & ' static str ,
@@ -919,7 +924,7 @@ mod content {
919
924
}
920
925
}
921
926
922
- struct SerializeMap < E > {
927
+ pub struct SerializeMap < E > {
923
928
entries : Vec < ( Content , Content ) > ,
924
929
key : Option < Content > ,
925
930
error : PhantomData < E > ,
@@ -969,7 +974,7 @@ mod content {
969
974
}
970
975
}
971
976
972
- struct SerializeStruct < E > {
977
+ pub struct SerializeStruct < E > {
973
978
name : & ' static str ,
974
979
fields : Vec < ( & ' static str , Content ) > ,
975
980
error : PhantomData < E > ,
@@ -996,7 +1001,7 @@ mod content {
996
1001
}
997
1002
}
998
1003
999
- struct SerializeStructVariant < E > {
1004
+ pub struct SerializeStructVariant < E > {
1000
1005
name : & ' static str ,
1001
1006
variant_index : u32 ,
1002
1007
variant : & ' static str ,
@@ -1059,7 +1064,7 @@ impl<'a, M> Serializer for FlatMapSerializer<'a, M>
1059
1064
type SerializeMap = FlatMapSerializeMap < ' a , M > ;
1060
1065
type SerializeStruct = FlatMapSerializeStruct < ' a , M > ;
1061
1066
type SerializeTupleVariant = Impossible < Self :: Ok , M :: Error > ;
1062
- type SerializeStructVariant = Impossible < Self :: Ok , M :: Error > ;
1067
+ type SerializeStructVariant = FlatMapSerializeStructVariantAsMapValue < ' a , M > ;
1063
1068
1064
1069
fn serialize_bool ( self , _: bool ) -> Result < Self :: Ok , Self :: Error > {
1065
1070
Err ( self . bad_type ( Unsupported :: Boolean ) )
@@ -1212,10 +1217,11 @@ impl<'a, M> Serializer for FlatMapSerializer<'a, M>
1212
1217
self ,
1213
1218
_: & ' static str ,
1214
1219
_: u32 ,
1215
- _ : & ' static str ,
1220
+ inner_variant : & ' static str ,
1216
1221
_: usize ,
1217
1222
) -> Result < Self :: SerializeStructVariant , Self :: Error > {
1218
- Err ( self . bad_type ( Unsupported :: Enum ) )
1223
+ try!( self . 0 . serialize_key ( inner_variant) ) ;
1224
+ Ok ( FlatMapSerializeStructVariantAsMapValue :: new ( self . 0 , inner_variant) )
1219
1225
}
1220
1226
}
1221
1227
@@ -1269,3 +1275,44 @@ impl<'a, M> ser::SerializeStruct for FlatMapSerializeStruct<'a, M>
1269
1275
Ok ( ( ) )
1270
1276
}
1271
1277
}
1278
+
1279
+ #[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
1280
+ pub struct FlatMapSerializeStructVariantAsMapValue < ' a , M : ' a > {
1281
+ map : & ' a mut M ,
1282
+ name : & ' static str ,
1283
+ fields : Vec < ( & ' static str , Content ) > ,
1284
+ }
1285
+
1286
+ impl < ' a , M > FlatMapSerializeStructVariantAsMapValue < ' a , M >
1287
+ where M : SerializeMap + ' a
1288
+ {
1289
+ fn new ( map : & ' a mut M , name : & ' static str ) -> FlatMapSerializeStructVariantAsMapValue < ' a , M > {
1290
+ FlatMapSerializeStructVariantAsMapValue {
1291
+ map : map,
1292
+ name : name,
1293
+ fields : Vec :: new ( ) ,
1294
+ }
1295
+ }
1296
+ }
1297
+
1298
+ #[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
1299
+ impl < ' a , M > ser:: SerializeStructVariant for FlatMapSerializeStructVariantAsMapValue < ' a , M >
1300
+ where M : SerializeMap + ' a
1301
+ {
1302
+ type Ok = ( ) ;
1303
+ type Error = M :: Error ;
1304
+
1305
+ fn serialize_field < T : ?Sized > ( & mut self , key : & ' static str , value : & T ) -> Result < ( ) , Self :: Error >
1306
+ where
1307
+ T : Serialize ,
1308
+ {
1309
+ let value = try!( value. serialize ( ContentSerializer :: < M :: Error > :: new ( ) ) ) ;
1310
+ self . fields . push ( ( key, value) ) ;
1311
+ Ok ( ( ) )
1312
+ }
1313
+
1314
+ fn end ( self ) -> Result < ( ) , Self :: Error > {
1315
+ try!( self . map . serialize_value ( & Content :: Struct ( self . name , self . fields ) ) ) ;
1316
+ Ok ( ( ) )
1317
+ }
1318
+ }
0 commit comments