@@ -946,25 +946,28 @@ impl<'a, W: fmt::Write> ser::Serializer for &'a mut Serializer<W> {
946946 self . start_indent ( ) ?;
947947 }
948948
949- Ok ( Compound :: new ( self , false ) )
949+ Ok ( Compound :: new ( self , Some ( ']' ) ) )
950950 }
951951
952952 fn serialize_tuple ( self , len : usize ) -> Result < Self :: SerializeTuple > {
953953 let old_newtype_variant = self . newtype_variant ;
954954 self . newtype_variant = false ;
955955 self . implicit_some_depth = 0 ;
956956
957- if !old_newtype_variant {
957+ let closing = if old_newtype_variant {
958+ None
959+ } else {
958960 self . output . write_char ( '(' ) ?;
959- }
961+ Some ( ')' )
962+ } ;
960963
961964 if self . separate_tuple_members ( ) {
962965 self . is_empty = Some ( len == 0 ) ;
963966
964967 self . start_indent ( ) ?;
965968 }
966969
967- Ok ( Compound :: new ( self , old_newtype_variant ) )
970+ Ok ( Compound :: new ( self , closing ) )
968971 }
969972
970973 fn serialize_tuple_struct (
@@ -1001,7 +1004,7 @@ impl<'a, W: fmt::Write> ser::Serializer for &'a mut Serializer<W> {
10011004 self . start_indent ( ) ?;
10021005 }
10031006
1004- Ok ( Compound :: new ( self , false ) )
1007+ Ok ( Compound :: new ( self , Some ( ')' ) ) )
10051008 }
10061009
10071010 fn serialize_map ( self , len : Option < usize > ) -> Result < Self :: SerializeMap > {
@@ -1018,31 +1021,37 @@ impl<'a, W: fmt::Write> ser::Serializer for &'a mut Serializer<W> {
10181021 self . start_indent ( ) ?;
10191022 }
10201023
1021- Ok ( Compound :: new ( self , false ) )
1024+ Ok ( Compound :: new ( self , Some ( '}' ) ) )
10221025 }
10231026
10241027 fn serialize_struct ( self , name : & ' static str , len : usize ) -> Result < Self :: SerializeStruct > {
10251028 let old_newtype_variant = self . newtype_variant ;
10261029 self . newtype_variant = false ;
10271030 self . implicit_some_depth = 0 ;
10281031
1029- if old_newtype_variant {
1032+ let closing = if self . extensions ( ) . contains ( Extensions :: BRACED_STRUCTS ) {
1033+ self . write_identifier ( name) ?;
1034+ self . output . write_char ( '{' ) ?;
1035+ Some ( '}' )
1036+ } else if old_newtype_variant {
10301037 self . validate_identifier ( name) ?;
1038+ None
10311039 } else {
10321040 if self . struct_names ( ) {
10331041 self . write_identifier ( name) ?;
10341042 } else {
10351043 self . validate_identifier ( name) ?;
10361044 }
10371045 self . output . write_char ( '(' ) ?;
1038- }
1046+ Some ( ')' )
1047+ } ;
10391048
10401049 if !self . compact_structs ( ) {
10411050 self . is_empty = Some ( len == 0 ) ;
10421051 self . start_indent ( ) ?;
10431052 }
10441053
1045- Ok ( Compound :: new ( self , old_newtype_variant ) )
1054+ Ok ( Compound :: new ( self , closing ) )
10461055 }
10471056
10481057 fn serialize_struct_variant (
@@ -1057,14 +1066,21 @@ impl<'a, W: fmt::Write> ser::Serializer for &'a mut Serializer<W> {
10571066
10581067 self . validate_identifier ( name) ?;
10591068 self . write_identifier ( variant) ?;
1060- self . output . write_char ( '(' ) ?;
1069+
1070+ let closing = if self . extensions ( ) . contains ( Extensions :: BRACED_STRUCTS ) {
1071+ self . output . write_char ( '{' ) ?;
1072+ '}'
1073+ } else {
1074+ self . output . write_char ( '(' ) ?;
1075+ ')'
1076+ } ;
10611077
10621078 if !self . compact_structs ( ) {
10631079 self . is_empty = Some ( len == 0 ) ;
10641080 self . start_indent ( ) ?;
10651081 }
10661082
1067- Ok ( Compound :: new ( self , false ) )
1083+ Ok ( Compound :: new ( self , Some ( closing ) ) )
10681084 }
10691085}
10701086
@@ -1077,16 +1093,16 @@ enum State {
10771093pub struct Compound < ' a , W : fmt:: Write > {
10781094 ser : & ' a mut Serializer < W > ,
10791095 state : State ,
1080- newtype_variant : bool ,
1096+ closing : Option < char > ,
10811097 sequence_index : usize ,
10821098}
10831099
10841100impl < ' a , W : fmt:: Write > Compound < ' a , W > {
1085- fn new ( ser : & ' a mut Serializer < W > , newtype_variant : bool ) -> Self {
1101+ fn new ( ser : & ' a mut Serializer < W > , closing : Option < char > ) -> Self {
10861102 Compound {
10871103 ser,
10881104 state : State :: First ,
1089- newtype_variant ,
1105+ closing ,
10901106 sequence_index : 0 ,
10911107 }
10921108 }
@@ -1151,8 +1167,10 @@ impl<'a, W: fmt::Write> ser::SerializeSeq for Compound<'a, W> {
11511167 self . ser . end_indent ( ) ?;
11521168 }
11531169
1154- // seq always disables `self.newtype_variant`
1155- self . ser . output . write_char ( ']' ) ?;
1170+ if let Some ( closing) = self . closing {
1171+ self . ser . output . write_char ( closing) ?;
1172+ }
1173+
11561174 Ok ( ( ) )
11571175 }
11581176}
@@ -1200,8 +1218,8 @@ impl<'a, W: fmt::Write> ser::SerializeTuple for Compound<'a, W> {
12001218 self . ser . end_indent ( ) ?;
12011219 }
12021220
1203- if ! self . newtype_variant {
1204- self . ser . output . write_char ( ')' ) ?;
1221+ if let Some ( closing ) = self . closing {
1222+ self . ser . output . write_char ( closing ) ?;
12051223 }
12061224
12071225 Ok ( ( ) )
@@ -1299,8 +1317,10 @@ impl<'a, W: fmt::Write> ser::SerializeMap for Compound<'a, W> {
12991317 self . ser . end_indent ( ) ?;
13001318 }
13011319
1302- // map always disables `self.newtype_variant`
1303- self . ser . output . write_char ( '}' ) ?;
1320+ if let Some ( closing) = self . closing {
1321+ self . ser . output . write_char ( closing) ?;
1322+ }
1323+
13041324 Ok ( ( ) )
13051325 }
13061326}
@@ -1357,9 +1377,10 @@ impl<'a, W: fmt::Write> ser::SerializeStruct for Compound<'a, W> {
13571377 self . ser . end_indent ( ) ?;
13581378 }
13591379
1360- if ! self . newtype_variant {
1361- self . ser . output . write_char ( ')' ) ?;
1380+ if let Some ( closing ) = self . closing {
1381+ self . ser . output . write_char ( closing ) ?;
13621382 }
1383+
13631384 Ok ( ( ) )
13641385 }
13651386}
0 commit comments