1 // Copyright 2017 Serde Developers
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
11 use ser
::{self, Impossible, Serialize, SerializeMap, SerializeStruct, Serializer}
;
13 #[cfg(any(feature = "std", feature = "alloc"))]
15 Content
, ContentSerializer
, SerializeStructVariantAsMapValue
, SerializeTupleVariantAsMapValue
,
18 /// Used to check that serde(getter) attributes return the expected type.
20 pub fn constrain
<T
: ?Sized
>(t
: &T
) -> &T
{
25 pub fn serialize_tagged_newtype
<S
, T
>(
27 type_ident
: &'
static str,
28 variant_ident
: &'
static str,
30 variant_name
: &'
static str,
32 ) -> Result
<S
::Ok
, S
::Error
>
37 value
.serialize(TaggedSerializer
{
38 type_ident
: type_ident
,
39 variant_ident
: variant_ident
,
41 variant_name
: variant_name
,
46 struct TaggedSerializer
<S
> {
47 type_ident
: &'
static str,
48 variant_ident
: &'
static str,
50 variant_name
: &'
static str,
63 #[cfg(any(feature = "std", feature = "alloc"))]
71 impl Display
for Unsupported
{
72 fn fmt(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
74 Unsupported
::Boolean
=> formatter
.write_str("a boolean"),
75 Unsupported
::Integer
=> formatter
.write_str("an integer"),
76 Unsupported
::Float
=> formatter
.write_str("a float"),
77 Unsupported
::Char
=> formatter
.write_str("a char"),
78 Unsupported
::String
=> formatter
.write_str("a string"),
79 Unsupported
::ByteArray
=> formatter
.write_str("a byte array"),
80 Unsupported
::Optional
=> formatter
.write_str("an optional"),
81 Unsupported
::Unit
=> formatter
.write_str("unit"),
82 #[cfg(any(feature = "std", feature = "alloc"))]
83 Unsupported
::UnitStruct
=> formatter
.write_str("unit struct"),
84 Unsupported
::Sequence
=> formatter
.write_str("a sequence"),
85 Unsupported
::Tuple
=> formatter
.write_str("a tuple"),
86 Unsupported
::TupleStruct
=> formatter
.write_str("a tuple struct"),
87 Unsupported
::Enum
=> formatter
.write_str("an enum"),
92 impl<S
> TaggedSerializer
<S
>
96 fn bad_type(self, what
: Unsupported
) -> S
::Error
{
97 ser
::Error
::custom(format_args
!(
98 "cannot serialize tagged newtype variant {}::{} containing {}",
99 self.type_ident
, self.variant_ident
, what
104 impl<S
> Serializer
for TaggedSerializer
<S
>
109 type Error
= S
::Error
;
111 type SerializeSeq
= Impossible
<S
::Ok
, S
::Error
>;
112 type SerializeTuple
= Impossible
<S
::Ok
, S
::Error
>;
113 type SerializeTupleStruct
= Impossible
<S
::Ok
, S
::Error
>;
114 type SerializeMap
= S
::SerializeMap
;
115 type SerializeStruct
= S
::SerializeStruct
;
117 #[cfg(not(any(feature = "std", feature = "alloc")))]
118 type SerializeTupleVariant
= Impossible
<S
::Ok
, S
::Error
>;
119 #[cfg(any(feature = "std", feature = "alloc"))]
120 type SerializeTupleVariant
= SerializeTupleVariantAsMapValue
<S
::SerializeMap
>;
122 #[cfg(not(any(feature = "std", feature = "alloc")))]
123 type SerializeStructVariant
= Impossible
<S
::Ok
, S
::Error
>;
124 #[cfg(any(feature = "std", feature = "alloc"))]
125 type SerializeStructVariant
= SerializeStructVariantAsMapValue
<S
::SerializeMap
>;
127 fn serialize_bool(self, _
: bool
) -> Result
<Self::Ok
, Self::Error
> {
128 Err(self.bad_type(Unsupported
::Boolean
))
131 fn serialize_i8(self, _
: i8) -> Result
<Self::Ok
, Self::Error
> {
132 Err(self.bad_type(Unsupported
::Integer
))
135 fn serialize_i16(self, _
: i16) -> Result
<Self::Ok
, Self::Error
> {
136 Err(self.bad_type(Unsupported
::Integer
))
139 fn serialize_i32(self, _
: i32) -> Result
<Self::Ok
, Self::Error
> {
140 Err(self.bad_type(Unsupported
::Integer
))
143 fn serialize_i64(self, _
: i64) -> Result
<Self::Ok
, Self::Error
> {
144 Err(self.bad_type(Unsupported
::Integer
))
147 fn serialize_u8(self, _
: u8) -> Result
<Self::Ok
, Self::Error
> {
148 Err(self.bad_type(Unsupported
::Integer
))
151 fn serialize_u16(self, _
: u16) -> Result
<Self::Ok
, Self::Error
> {
152 Err(self.bad_type(Unsupported
::Integer
))
155 fn serialize_u32(self, _
: u32) -> Result
<Self::Ok
, Self::Error
> {
156 Err(self.bad_type(Unsupported
::Integer
))
159 fn serialize_u64(self, _
: u64) -> Result
<Self::Ok
, Self::Error
> {
160 Err(self.bad_type(Unsupported
::Integer
))
163 fn serialize_f32(self, _
: f32) -> Result
<Self::Ok
, Self::Error
> {
164 Err(self.bad_type(Unsupported
::Float
))
167 fn serialize_f64(self, _
: f64) -> Result
<Self::Ok
, Self::Error
> {
168 Err(self.bad_type(Unsupported
::Float
))
171 fn serialize_char(self, _
: char) -> Result
<Self::Ok
, Self::Error
> {
172 Err(self.bad_type(Unsupported
::Char
))
175 fn serialize_str(self, _
: &str) -> Result
<Self::Ok
, Self::Error
> {
176 Err(self.bad_type(Unsupported
::String
))
179 fn serialize_bytes(self, _
: &[u8]) -> Result
<Self::Ok
, Self::Error
> {
180 Err(self.bad_type(Unsupported
::ByteArray
))
183 fn serialize_none(self) -> Result
<Self::Ok
, Self::Error
> {
184 Err(self.bad_type(Unsupported
::Optional
))
187 fn serialize_some
<T
: ?Sized
>(self, _
: &T
) -> Result
<Self::Ok
, Self::Error
>
191 Err(self.bad_type(Unsupported
::Optional
))
194 fn serialize_unit(self) -> Result
<Self::Ok
, Self::Error
> {
195 Err(self.bad_type(Unsupported
::Unit
))
198 fn serialize_unit_struct(self, _
: &'
static str) -> Result
<Self::Ok
, Self::Error
> {
199 let mut map
= try
!(self.delegate
.serialize_map(Some(1)));
200 try
!(map
.serialize_entry(self.tag
, self.variant_name
));
204 fn serialize_unit_variant(
208 inner_variant
: &'
static str,
209 ) -> Result
<Self::Ok
, Self::Error
> {
210 let mut map
= try
!(self.delegate
.serialize_map(Some(2)));
211 try
!(map
.serialize_entry(self.tag
, self.variant_name
));
212 try
!(map
.serialize_entry(inner_variant
, &()));
216 fn serialize_newtype_struct
<T
: ?Sized
>(
220 ) -> Result
<Self::Ok
, Self::Error
>
224 value
.serialize(self)
227 fn serialize_newtype_variant
<T
: ?Sized
>(
231 inner_variant
: &'
static str,
233 ) -> Result
<Self::Ok
, Self::Error
>
237 let mut map
= try
!(self.delegate
.serialize_map(Some(2)));
238 try
!(map
.serialize_entry(self.tag
, self.variant_name
));
239 try
!(map
.serialize_entry(inner_variant
, inner_value
));
243 fn serialize_seq(self, _
: Option
<usize>) -> Result
<Self::SerializeSeq
, Self::Error
> {
244 Err(self.bad_type(Unsupported
::Sequence
))
247 fn serialize_tuple(self, _
: usize) -> Result
<Self::SerializeTuple
, Self::Error
> {
248 Err(self.bad_type(Unsupported
::Tuple
))
251 fn serialize_tuple_struct(
255 ) -> Result
<Self::SerializeTupleStruct
, Self::Error
> {
256 Err(self.bad_type(Unsupported
::TupleStruct
))
259 #[cfg(not(any(feature = "std", feature = "alloc")))]
260 fn serialize_tuple_variant(
266 ) -> Result
<Self::SerializeTupleVariant
, Self::Error
> {
267 // Lack of push-based serialization means we need to buffer the content
268 // of the tuple variant, so it requires std.
269 Err(self.bad_type(Unsupported
::Enum
))
272 #[cfg(any(feature = "std", feature = "alloc"))]
273 fn serialize_tuple_variant(
277 inner_variant
: &'
static str,
279 ) -> Result
<Self::SerializeTupleVariant
, Self::Error
> {
280 let mut map
= try
!(self.delegate
.serialize_map(Some(2)));
281 try
!(map
.serialize_entry(self.tag
, self.variant_name
));
282 try
!(map
.serialize_key(inner_variant
));
283 Ok(SerializeTupleVariantAsMapValue
::new(
290 fn serialize_map(self, len
: Option
<usize>) -> Result
<Self::SerializeMap
, Self::Error
> {
291 let mut map
= try
!(self.delegate
.serialize_map(len
.map(|len
| len
+ 1)));
292 try
!(map
.serialize_entry(self.tag
, self.variant_name
));
300 ) -> Result
<Self::SerializeStruct
, Self::Error
> {
301 let mut state
= try
!(self.delegate
.serialize_struct(name
, len
+ 1));
302 try
!(state
.serialize_field(self.tag
, self.variant_name
));
306 #[cfg(not(any(feature = "std", feature = "alloc")))]
307 fn serialize_struct_variant(
313 ) -> Result
<Self::SerializeStructVariant
, Self::Error
> {
314 // Lack of push-based serialization means we need to buffer the content
315 // of the struct variant, so it requires std.
316 Err(self.bad_type(Unsupported
::Enum
))
319 #[cfg(any(feature = "std", feature = "alloc"))]
320 fn serialize_struct_variant(
324 inner_variant
: &'
static str,
326 ) -> Result
<Self::SerializeStructVariant
, Self::Error
> {
327 let mut map
= try
!(self.delegate
.serialize_map(Some(2)));
328 try
!(map
.serialize_entry(self.tag
, self.variant_name
));
329 try
!(map
.serialize_key(inner_variant
));
330 Ok(SerializeStructVariantAsMapValue
::new(
337 #[cfg(not(any(feature = "std", feature = "alloc")))]
338 fn collect_str
<T
: ?Sized
>(self, _
: &T
) -> Result
<Self::Ok
, Self::Error
>
342 Err(self.bad_type(Unsupported
::String
))
346 /// Used only by Serde doc tests. Not public API.
351 impl ser
::Error
for Error
{
352 fn custom
<T
>(_
: T
) -> Self
360 #[cfg(feature = "std")]
361 impl error
::Error
for Error
{
362 fn description(&self) -> &str {
367 impl Display
for Error
{
368 fn fmt(&self, _
: &mut fmt
::Formatter
) -> fmt
::Result
{
373 #[cfg(any(feature = "std", feature = "alloc"))]
377 use ser
::{self, Serialize, Serializer}
;
379 pub struct SerializeTupleVariantAsMapValue
<M
> {
382 fields
: Vec
<Content
>,
385 impl<M
> SerializeTupleVariantAsMapValue
<M
> {
386 pub fn new(map
: M
, name
: &'
static str, len
: usize) -> Self {
387 SerializeTupleVariantAsMapValue
{
390 fields
: Vec
::with_capacity(len
),
395 impl<M
> ser
::SerializeTupleVariant
for SerializeTupleVariantAsMapValue
<M
>
397 M
: ser
::SerializeMap
,
400 type Error
= M
::Error
;
402 fn serialize_field
<T
: ?Sized
>(&mut self, value
: &T
) -> Result
<(), M
::Error
>
406 let value
= try
!(value
.serialize(ContentSerializer
::<M
::Error
>::new()));
407 self.fields
.push(value
);
411 fn end(mut self) -> Result
<M
::Ok
, M
::Error
> {
414 .serialize_value(&Content
::TupleStruct(self.name
, self.fields
))
420 pub struct SerializeStructVariantAsMapValue
<M
> {
423 fields
: Vec
<(&'
static str, Content
)>,
426 impl<M
> SerializeStructVariantAsMapValue
<M
> {
427 pub fn new(map
: M
, name
: &'
static str, len
: usize) -> Self {
428 SerializeStructVariantAsMapValue
{
431 fields
: Vec
::with_capacity(len
),
436 impl<M
> ser
::SerializeStructVariant
for SerializeStructVariantAsMapValue
<M
>
438 M
: ser
::SerializeMap
,
441 type Error
= M
::Error
;
443 fn serialize_field
<T
: ?Sized
>(
447 ) -> Result
<(), M
::Error
>
451 let value
= try
!(value
.serialize(ContentSerializer
::<M
::Error
>::new()));
452 self.fields
.push((key
, value
));
456 fn end(mut self) -> Result
<M
::Ok
, M
::Error
> {
459 .serialize_value(&Content
::Struct(self.name
, self.fields
))
490 UnitStruct(&'
static str),
491 UnitVariant(&'
static str, u32, &'
static str),
492 NewtypeStruct(&'
static str, Box
<Content
>),
493 NewtypeVariant(&'
static str, u32, &'
static str, Box
<Content
>),
497 TupleStruct(&'
static str, Vec
<Content
>),
498 TupleVariant(&'
static str, u32, &'
static str, Vec
<Content
>),
499 Map(Vec
<(Content
, Content
)>),
500 Struct(&'
static str, Vec
<(&'
static str, Content
)>),
505 Vec
<(&'
static str, Content
)>,
509 impl Serialize
for Content
{
510 fn serialize
<S
>(&self, serializer
: S
) -> Result
<S
::Ok
, S
::Error
>
515 Content
::Bool(b
) => serializer
.serialize_bool(b
),
516 Content
::U8(u
) => serializer
.serialize_u8(u
),
517 Content
::U16(u
) => serializer
.serialize_u16(u
),
518 Content
::U32(u
) => serializer
.serialize_u32(u
),
519 Content
::U64(u
) => serializer
.serialize_u64(u
),
520 Content
::I8(i
) => serializer
.serialize_i8(i
),
521 Content
::I16(i
) => serializer
.serialize_i16(i
),
522 Content
::I32(i
) => serializer
.serialize_i32(i
),
523 Content
::I64(i
) => serializer
.serialize_i64(i
),
524 Content
::F32(f
) => serializer
.serialize_f32(f
),
525 Content
::F64(f
) => serializer
.serialize_f64(f
),
526 Content
::Char(c
) => serializer
.serialize_char(c
),
527 Content
::String(ref s
) => serializer
.serialize_str(s
),
528 Content
::Bytes(ref b
) => serializer
.serialize_bytes(b
),
529 Content
::None
=> serializer
.serialize_none(),
530 Content
::Some(ref c
) => serializer
.serialize_some(&**c
),
531 Content
::Unit
=> serializer
.serialize_unit(),
532 Content
::UnitStruct(n
) => serializer
.serialize_unit_struct(n
),
533 Content
::UnitVariant(n
, i
, v
) => serializer
.serialize_unit_variant(n
, i
, v
),
534 Content
::NewtypeStruct(n
, ref c
) => serializer
.serialize_newtype_struct(n
, &**c
),
535 Content
::NewtypeVariant(n
, i
, v
, ref c
) => {
536 serializer
.serialize_newtype_variant(n
, i
, v
, &**c
)
538 Content
::Seq(ref elements
) => elements
.serialize(serializer
),
539 Content
::Tuple(ref elements
) => {
540 use ser
::SerializeTuple
;
541 let mut tuple
= try
!(serializer
.serialize_tuple(elements
.len()));
543 try
!(tuple
.serialize_element(e
));
547 Content
::TupleStruct(n
, ref fields
) => {
548 use ser
::SerializeTupleStruct
;
549 let mut ts
= try
!(serializer
.serialize_tuple_struct(n
, fields
.len()));
551 try
!(ts
.serialize_field(f
));
555 Content
::TupleVariant(n
, i
, v
, ref fields
) => {
556 use ser
::SerializeTupleVariant
;
557 let mut tv
= try
!(serializer
.serialize_tuple_variant(n
, i
, v
, fields
.len()));
559 try
!(tv
.serialize_field(f
));
563 Content
::Map(ref entries
) => {
564 use ser
::SerializeMap
;
565 let mut map
= try
!(serializer
.serialize_map(Some(entries
.len())));
566 for &(ref k
, ref v
) in entries
{
567 try
!(map
.serialize_entry(k
, v
));
571 Content
::Struct(n
, ref fields
) => {
572 use ser
::SerializeStruct
;
573 let mut s
= try
!(serializer
.serialize_struct(n
, fields
.len()));
574 for &(k
, ref v
) in fields
{
575 try
!(s
.serialize_field(k
, v
));
579 Content
::StructVariant(n
, i
, v
, ref fields
) => {
580 use ser
::SerializeStructVariant
;
581 let mut sv
= try
!(serializer
.serialize_struct_variant(n
, i
, v
, fields
.len()));
582 for &(k
, ref v
) in fields
{
583 try
!(sv
.serialize_field(k
, v
));
591 pub struct ContentSerializer
<E
> {
592 error
: PhantomData
<E
>,
595 impl<E
> ContentSerializer
<E
> {
596 pub fn new() -> Self {
597 ContentSerializer { error: PhantomData }
601 impl<E
> Serializer
for ContentSerializer
<E
>
608 type SerializeSeq
= SerializeSeq
<E
>;
609 type SerializeTuple
= SerializeTuple
<E
>;
610 type SerializeTupleStruct
= SerializeTupleStruct
<E
>;
611 type SerializeTupleVariant
= SerializeTupleVariant
<E
>;
612 type SerializeMap
= SerializeMap
<E
>;
613 type SerializeStruct
= SerializeStruct
<E
>;
614 type SerializeStructVariant
= SerializeStructVariant
<E
>;
616 fn serialize_bool(self, v
: bool
) -> Result
<Content
, E
> {
620 fn serialize_i8(self, v
: i8) -> Result
<Content
, E
> {
624 fn serialize_i16(self, v
: i16) -> Result
<Content
, E
> {
628 fn serialize_i32(self, v
: i32) -> Result
<Content
, E
> {
632 fn serialize_i64(self, v
: i64) -> Result
<Content
, E
> {
636 fn serialize_u8(self, v
: u8) -> Result
<Content
, E
> {
640 fn serialize_u16(self, v
: u16) -> Result
<Content
, E
> {
644 fn serialize_u32(self, v
: u32) -> Result
<Content
, E
> {
648 fn serialize_u64(self, v
: u64) -> Result
<Content
, E
> {
652 fn serialize_f32(self, v
: f32) -> Result
<Content
, E
> {
656 fn serialize_f64(self, v
: f64) -> Result
<Content
, E
> {
660 fn serialize_char(self, v
: char) -> Result
<Content
, E
> {
664 fn serialize_str(self, value
: &str) -> Result
<Content
, E
> {
665 Ok(Content
::String(value
.to_owned()))
668 fn serialize_bytes(self, value
: &[u8]) -> Result
<Content
, E
> {
669 Ok(Content
::Bytes(value
.to_owned()))
672 fn serialize_none(self) -> Result
<Content
, E
> {
676 fn serialize_some
<T
: ?Sized
>(self, value
: &T
) -> Result
<Content
, E
>
680 Ok(Content
::Some(Box
::new(try
!(value
.serialize(self)))))
683 fn serialize_unit(self) -> Result
<Content
, E
> {
687 fn serialize_unit_struct(self, name
: &'
static str) -> Result
<Content
, E
> {
688 Ok(Content
::UnitStruct(name
))
691 fn serialize_unit_variant(
695 variant
: &'
static str,
696 ) -> Result
<Content
, E
> {
697 Ok(Content
::UnitVariant(name
, variant_index
, variant
))
700 fn serialize_newtype_struct
<T
: ?Sized
>(
704 ) -> Result
<Content
, E
>
708 Ok(Content
::NewtypeStruct(
710 Box
::new(try
!(value
.serialize(self))),
714 fn serialize_newtype_variant
<T
: ?Sized
>(
718 variant
: &'
static str,
720 ) -> Result
<Content
, E
>
724 Ok(Content
::NewtypeVariant(
728 Box
::new(try
!(value
.serialize(self))),
732 fn serialize_seq(self, len
: Option
<usize>) -> Result
<Self::SerializeSeq
, E
> {
734 elements
: Vec
::with_capacity(len
.unwrap_or(0)),
739 fn serialize_tuple(self, len
: usize) -> Result
<Self::SerializeTuple
, E
> {
741 elements
: Vec
::with_capacity(len
),
746 fn serialize_tuple_struct(
750 ) -> Result
<Self::SerializeTupleStruct
, E
> {
751 Ok(SerializeTupleStruct
{
753 fields
: Vec
::with_capacity(len
),
758 fn serialize_tuple_variant(
762 variant
: &'
static str,
764 ) -> Result
<Self::SerializeTupleVariant
, E
> {
765 Ok(SerializeTupleVariant
{
767 variant_index
: variant_index
,
769 fields
: Vec
::with_capacity(len
),
774 fn serialize_map(self, len
: Option
<usize>) -> Result
<Self::SerializeMap
, E
> {
776 entries
: Vec
::with_capacity(len
.unwrap_or(0)),
786 ) -> Result
<Self::SerializeStruct
, E
> {
789 fields
: Vec
::with_capacity(len
),
794 fn serialize_struct_variant(
798 variant
: &'
static str,
800 ) -> Result
<Self::SerializeStructVariant
, E
> {
801 Ok(SerializeStructVariant
{
803 variant_index
: variant_index
,
805 fields
: Vec
::with_capacity(len
),
811 pub struct SerializeSeq
<E
> {
812 elements
: Vec
<Content
>,
813 error
: PhantomData
<E
>,
816 impl<E
> ser
::SerializeSeq
for SerializeSeq
<E
>
823 fn serialize_element
<T
: ?Sized
>(&mut self, value
: &T
) -> Result
<(), E
>
827 let value
= try
!(value
.serialize(ContentSerializer
::<E
>::new()));
828 self.elements
.push(value
);
832 fn end(self) -> Result
<Content
, E
> {
833 Ok(Content
::Seq(self.elements
))
837 pub struct SerializeTuple
<E
> {
838 elements
: Vec
<Content
>,
839 error
: PhantomData
<E
>,
842 impl<E
> ser
::SerializeTuple
for SerializeTuple
<E
>
849 fn serialize_element
<T
: ?Sized
>(&mut self, value
: &T
) -> Result
<(), E
>
853 let value
= try
!(value
.serialize(ContentSerializer
::<E
>::new()));
854 self.elements
.push(value
);
858 fn end(self) -> Result
<Content
, E
> {
859 Ok(Content
::Tuple(self.elements
))
863 pub struct SerializeTupleStruct
<E
> {
865 fields
: Vec
<Content
>,
866 error
: PhantomData
<E
>,
869 impl<E
> ser
::SerializeTupleStruct
for SerializeTupleStruct
<E
>
876 fn serialize_field
<T
: ?Sized
>(&mut self, value
: &T
) -> Result
<(), E
>
880 let value
= try
!(value
.serialize(ContentSerializer
::<E
>::new()));
881 self.fields
.push(value
);
885 fn end(self) -> Result
<Content
, E
> {
886 Ok(Content
::TupleStruct(self.name
, self.fields
))
890 pub struct SerializeTupleVariant
<E
> {
893 variant
: &'
static str,
894 fields
: Vec
<Content
>,
895 error
: PhantomData
<E
>,
898 impl<E
> ser
::SerializeTupleVariant
for SerializeTupleVariant
<E
>
905 fn serialize_field
<T
: ?Sized
>(&mut self, value
: &T
) -> Result
<(), E
>
909 let value
= try
!(value
.serialize(ContentSerializer
::<E
>::new()));
910 self.fields
.push(value
);
914 fn end(self) -> Result
<Content
, E
> {
915 Ok(Content
::TupleVariant(
924 pub struct SerializeMap
<E
> {
925 entries
: Vec
<(Content
, Content
)>,
926 key
: Option
<Content
>,
927 error
: PhantomData
<E
>,
930 impl<E
> ser
::SerializeMap
for SerializeMap
<E
>
937 fn serialize_key
<T
: ?Sized
>(&mut self, key
: &T
) -> Result
<(), E
>
941 let key
= try
!(key
.serialize(ContentSerializer
::<E
>::new()));
942 self.key
= Some(key
);
946 fn serialize_value
<T
: ?Sized
>(&mut self, value
: &T
) -> Result
<(), E
>
953 .expect("serialize_value called before serialize_key");
954 let value
= try
!(value
.serialize(ContentSerializer
::<E
>::new()));
955 self.entries
.push((key
, value
));
959 fn end(self) -> Result
<Content
, E
> {
960 Ok(Content
::Map(self.entries
))
963 fn serialize_entry
<K
: ?Sized
, V
: ?Sized
>(&mut self, key
: &K
, value
: &V
) -> Result
<(), E
>
968 let key
= try
!(key
.serialize(ContentSerializer
::<E
>::new()));
969 let value
= try
!(value
.serialize(ContentSerializer
::<E
>::new()));
970 self.entries
.push((key
, value
));
975 pub struct SerializeStruct
<E
> {
977 fields
: Vec
<(&'
static str, Content
)>,
978 error
: PhantomData
<E
>,
981 impl<E
> ser
::SerializeStruct
for SerializeStruct
<E
>
988 fn serialize_field
<T
: ?Sized
>(&mut self, key
: &'
static str, value
: &T
) -> Result
<(), E
>
992 let value
= try
!(value
.serialize(ContentSerializer
::<E
>::new()));
993 self.fields
.push((key
, value
));
997 fn end(self) -> Result
<Content
, E
> {
998 Ok(Content
::Struct(self.name
, self.fields
))
1002 pub struct SerializeStructVariant
<E
> {
1005 variant
: &'
static str,
1006 fields
: Vec
<(&'
static str, Content
)>,
1007 error
: PhantomData
<E
>,
1010 impl<E
> ser
::SerializeStructVariant
for SerializeStructVariant
<E
>
1017 fn serialize_field
<T
: ?Sized
>(&mut self, key
: &'
static str, value
: &T
) -> Result
<(), E
>
1021 let value
= try
!(value
.serialize(ContentSerializer
::<E
>::new()));
1022 self.fields
.push((key
, value
));
1026 fn end(self) -> Result
<Content
, E
> {
1027 Ok(Content
::StructVariant(
1037 #[cfg(any(feature = "std", feature = "alloc"))]
1038 pub struct FlatMapSerializer
<'a
, M
: 'a
>(pub &'a
mut M
);
1040 #[cfg(any(feature = "std", feature = "alloc"))]
1041 impl<'a
, M
> FlatMapSerializer
<'a
, M
>
1043 M
: SerializeMap
+ 'a
,
1045 fn bad_type(self, what
: Unsupported
) -> M
::Error
{
1046 ser
::Error
::custom(format_args
!(
1047 "can only flatten structs and maps (got {})",
1053 #[cfg(any(feature = "std", feature = "alloc"))]
1054 impl<'a
, M
> Serializer
for FlatMapSerializer
<'a
, M
>
1056 M
: SerializeMap
+ 'a
,
1059 type Error
= M
::Error
;
1061 type SerializeSeq
= Impossible
<Self::Ok
, M
::Error
>;
1062 type SerializeTuple
= Impossible
<Self::Ok
, M
::Error
>;
1063 type SerializeTupleStruct
= Impossible
<Self::Ok
, M
::Error
>;
1064 type SerializeMap
= FlatMapSerializeMap
<'a
, M
>;
1065 type SerializeStruct
= FlatMapSerializeStruct
<'a
, M
>;
1066 type SerializeTupleVariant
= Impossible
<Self::Ok
, M
::Error
>;
1067 type SerializeStructVariant
= FlatMapSerializeStructVariantAsMapValue
<'a
, M
>;
1069 fn serialize_bool(self, _
: bool
) -> Result
<Self::Ok
, Self::Error
> {
1070 Err(self.bad_type(Unsupported
::Boolean
))
1073 fn serialize_i8(self, _
: i8) -> Result
<Self::Ok
, Self::Error
> {
1074 Err(self.bad_type(Unsupported
::Integer
))
1077 fn serialize_i16(self, _
: i16) -> Result
<Self::Ok
, Self::Error
> {
1078 Err(self.bad_type(Unsupported
::Integer
))
1081 fn serialize_i32(self, _
: i32) -> Result
<Self::Ok
, Self::Error
> {
1082 Err(self.bad_type(Unsupported
::Integer
))
1085 fn serialize_i64(self, _
: i64) -> Result
<Self::Ok
, Self::Error
> {
1086 Err(self.bad_type(Unsupported
::Integer
))
1089 fn serialize_u8(self, _
: u8) -> Result
<Self::Ok
, Self::Error
> {
1090 Err(self.bad_type(Unsupported
::Integer
))
1093 fn serialize_u16(self, _
: u16) -> Result
<Self::Ok
, Self::Error
> {
1094 Err(self.bad_type(Unsupported
::Integer
))
1097 fn serialize_u32(self, _
: u32) -> Result
<Self::Ok
, Self::Error
> {
1098 Err(self.bad_type(Unsupported
::Integer
))
1101 fn serialize_u64(self, _
: u64) -> Result
<Self::Ok
, Self::Error
> {
1102 Err(self.bad_type(Unsupported
::Integer
))
1105 fn serialize_f32(self, _
: f32) -> Result
<Self::Ok
, Self::Error
> {
1106 Err(self.bad_type(Unsupported
::Float
))
1109 fn serialize_f64(self, _
: f64) -> Result
<Self::Ok
, Self::Error
> {
1110 Err(self.bad_type(Unsupported
::Float
))
1113 fn serialize_char(self, _
: char) -> Result
<Self::Ok
, Self::Error
> {
1114 Err(self.bad_type(Unsupported
::Char
))
1117 fn serialize_str(self, _
: &str) -> Result
<Self::Ok
, Self::Error
> {
1118 Err(self.bad_type(Unsupported
::String
))
1121 fn serialize_bytes(self, _
: &[u8]) -> Result
<Self::Ok
, Self::Error
> {
1122 Err(self.bad_type(Unsupported
::ByteArray
))
1125 fn serialize_none(self) -> Result
<Self::Ok
, Self::Error
> {
1129 fn serialize_some
<T
: ?Sized
>(self, value
: &T
) -> Result
<Self::Ok
, Self::Error
>
1133 value
.serialize(self)
1136 fn serialize_unit(self) -> Result
<Self::Ok
, Self::Error
> {
1137 Err(self.bad_type(Unsupported
::Unit
))
1140 fn serialize_unit_struct(self, _
: &'
static str) -> Result
<Self::Ok
, Self::Error
> {
1141 Err(self.bad_type(Unsupported
::UnitStruct
))
1144 fn serialize_unit_variant(
1149 ) -> Result
<Self::Ok
, Self::Error
> {
1150 Err(self.bad_type(Unsupported
::Enum
))
1153 fn serialize_newtype_struct
<T
: ?Sized
>(
1157 ) -> Result
<Self::Ok
, Self::Error
>
1161 value
.serialize(self)
1164 fn serialize_newtype_variant
<T
: ?Sized
>(
1168 variant
: &'
static str,
1170 ) -> Result
<Self::Ok
, Self::Error
>
1174 try
!(self.0.serialize_key(variant
));
1175 self.0.serialize_value(value
)
1178 fn serialize_seq(self, _
: Option
<usize>) -> Result
<Self::SerializeSeq
, Self::Error
> {
1179 Err(self.bad_type(Unsupported
::Sequence
))
1182 fn serialize_tuple(self, _
: usize) -> Result
<Self::SerializeTuple
, Self::Error
> {
1183 Err(self.bad_type(Unsupported
::Tuple
))
1186 fn serialize_tuple_struct(
1190 ) -> Result
<Self::SerializeTupleStruct
, Self::Error
> {
1191 Err(self.bad_type(Unsupported
::TupleStruct
))
1194 fn serialize_tuple_variant(
1200 ) -> Result
<Self::SerializeTupleVariant
, Self::Error
> {
1201 Err(self.bad_type(Unsupported
::Enum
))
1204 fn serialize_map(self, _
: Option
<usize>) -> Result
<Self::SerializeMap
, Self::Error
> {
1205 Ok(FlatMapSerializeMap(self.0))
1208 fn serialize_struct(
1212 ) -> Result
<Self::SerializeStruct
, Self::Error
> {
1213 Ok(FlatMapSerializeStruct(self.0))
1216 fn serialize_struct_variant(
1220 inner_variant
: &'
static str,
1222 ) -> Result
<Self::SerializeStructVariant
, Self::Error
> {
1223 try
!(self.0.serialize_key(inner_variant
));
1224 Ok(FlatMapSerializeStructVariantAsMapValue
::new(
1231 #[cfg(any(feature = "std", feature = "alloc"))]
1232 pub struct FlatMapSerializeMap
<'a
, M
: 'a
>(&'a
mut M
);
1234 #[cfg(any(feature = "std", feature = "alloc"))]
1235 impl<'a
, M
> ser
::SerializeMap
for FlatMapSerializeMap
<'a
, M
>
1237 M
: SerializeMap
+ 'a
,
1240 type Error
= M
::Error
;
1242 fn serialize_key
<T
: ?Sized
>(&mut self, key
: &T
) -> Result
<(), Self::Error
>
1246 self.0.serialize_key(key
)
1249 fn serialize_value
<T
: ?Sized
>(&mut self, value
: &T
) -> Result
<(), Self::Error
>
1253 self.0.serialize_value(value
)
1256 fn end(self) -> Result
<(), Self::Error
> {
1261 #[cfg(any(feature = "std", feature = "alloc"))]
1262 pub struct FlatMapSerializeStruct
<'a
, M
: 'a
>(&'a
mut M
);
1264 #[cfg(any(feature = "std", feature = "alloc"))]
1265 impl<'a
, M
> ser
::SerializeStruct
for FlatMapSerializeStruct
<'a
, M
>
1267 M
: SerializeMap
+ 'a
,
1270 type Error
= M
::Error
;
1272 fn serialize_field
<T
: ?Sized
>(
1276 ) -> Result
<(), Self::Error
>
1280 self.0.serialize_entry(key
, value
)
1283 fn end(self) -> Result
<(), Self::Error
> {
1288 #[cfg(any(feature = "std", feature = "alloc"))]
1289 pub struct FlatMapSerializeStructVariantAsMapValue
<'a
, M
: 'a
> {
1292 fields
: Vec
<(&'
static str, Content
)>,
1295 #[cfg(any(feature = "std", feature = "alloc"))]
1296 impl<'a
, M
> FlatMapSerializeStructVariantAsMapValue
<'a
, M
>
1298 M
: SerializeMap
+ 'a
,
1300 fn new(map
: &'a
mut M
, name
: &'
static str) -> FlatMapSerializeStructVariantAsMapValue
<'a
, M
> {
1301 FlatMapSerializeStructVariantAsMapValue
{
1309 #[cfg(any(feature = "std", feature = "alloc"))]
1310 impl<'a
, M
> ser
::SerializeStructVariant
for FlatMapSerializeStructVariantAsMapValue
<'a
, M
>
1312 M
: SerializeMap
+ 'a
,
1315 type Error
= M
::Error
;
1317 fn serialize_field
<T
: ?Sized
>(
1321 ) -> Result
<(), Self::Error
>
1325 let value
= try
!(value
.serialize(ContentSerializer
::<M
::Error
>::new()));
1326 self.fields
.push((key
, value
));
1330 fn end(self) -> Result
<(), Self::Error
> {
1333 .serialize_value(&Content
::Struct(self.name
, self.fields
))