3 use ser
::{self, Impossible, Serialize, SerializeMap, SerializeStruct, Serializer}
;
5 #[cfg(any(feature = "std", feature = "alloc"))]
7 Content
, ContentSerializer
, SerializeStructVariantAsMapValue
, SerializeTupleVariantAsMapValue
,
10 /// Used to check that serde(getter) attributes return the expected type.
12 pub fn constrain
<T
: ?Sized
>(t
: &T
) -> &T
{
17 pub fn serialize_tagged_newtype
<S
, T
>(
19 type_ident
: &'
static str,
20 variant_ident
: &'
static str,
22 variant_name
: &'
static str,
24 ) -> Result
<S
::Ok
, S
::Error
>
29 value
.serialize(TaggedSerializer
{
30 type_ident
: type_ident
,
31 variant_ident
: variant_ident
,
33 variant_name
: variant_name
,
38 struct TaggedSerializer
<S
> {
39 type_ident
: &'
static str,
40 variant_ident
: &'
static str,
42 variant_name
: &'
static str,
55 #[cfg(any(feature = "std", feature = "alloc"))]
63 impl Display
for Unsupported
{
64 fn fmt(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
66 Unsupported
::Boolean
=> formatter
.write_str("a boolean"),
67 Unsupported
::Integer
=> formatter
.write_str("an integer"),
68 Unsupported
::Float
=> formatter
.write_str("a float"),
69 Unsupported
::Char
=> formatter
.write_str("a char"),
70 Unsupported
::String
=> formatter
.write_str("a string"),
71 Unsupported
::ByteArray
=> formatter
.write_str("a byte array"),
72 Unsupported
::Optional
=> formatter
.write_str("an optional"),
73 Unsupported
::Unit
=> formatter
.write_str("unit"),
74 #[cfg(any(feature = "std", feature = "alloc"))]
75 Unsupported
::UnitStruct
=> formatter
.write_str("unit struct"),
76 Unsupported
::Sequence
=> formatter
.write_str("a sequence"),
77 Unsupported
::Tuple
=> formatter
.write_str("a tuple"),
78 Unsupported
::TupleStruct
=> formatter
.write_str("a tuple struct"),
79 Unsupported
::Enum
=> formatter
.write_str("an enum"),
84 impl<S
> TaggedSerializer
<S
>
88 fn bad_type(self, what
: Unsupported
) -> S
::Error
{
89 ser
::Error
::custom(format_args
!(
90 "cannot serialize tagged newtype variant {}::{} containing {}",
91 self.type_ident
, self.variant_ident
, what
96 impl<S
> Serializer
for TaggedSerializer
<S
>
101 type Error
= S
::Error
;
103 type SerializeSeq
= Impossible
<S
::Ok
, S
::Error
>;
104 type SerializeTuple
= Impossible
<S
::Ok
, S
::Error
>;
105 type SerializeTupleStruct
= Impossible
<S
::Ok
, S
::Error
>;
106 type SerializeMap
= S
::SerializeMap
;
107 type SerializeStruct
= S
::SerializeStruct
;
109 #[cfg(not(any(feature = "std", feature = "alloc")))]
110 type SerializeTupleVariant
= Impossible
<S
::Ok
, S
::Error
>;
111 #[cfg(any(feature = "std", feature = "alloc"))]
112 type SerializeTupleVariant
= SerializeTupleVariantAsMapValue
<S
::SerializeMap
>;
114 #[cfg(not(any(feature = "std", feature = "alloc")))]
115 type SerializeStructVariant
= Impossible
<S
::Ok
, S
::Error
>;
116 #[cfg(any(feature = "std", feature = "alloc"))]
117 type SerializeStructVariant
= SerializeStructVariantAsMapValue
<S
::SerializeMap
>;
119 fn serialize_bool(self, _
: bool
) -> Result
<Self::Ok
, Self::Error
> {
120 Err(self.bad_type(Unsupported
::Boolean
))
123 fn serialize_i8(self, _
: i8) -> Result
<Self::Ok
, Self::Error
> {
124 Err(self.bad_type(Unsupported
::Integer
))
127 fn serialize_i16(self, _
: i16) -> Result
<Self::Ok
, Self::Error
> {
128 Err(self.bad_type(Unsupported
::Integer
))
131 fn serialize_i32(self, _
: i32) -> Result
<Self::Ok
, Self::Error
> {
132 Err(self.bad_type(Unsupported
::Integer
))
135 fn serialize_i64(self, _
: i64) -> Result
<Self::Ok
, Self::Error
> {
136 Err(self.bad_type(Unsupported
::Integer
))
139 fn serialize_u8(self, _
: u8) -> Result
<Self::Ok
, Self::Error
> {
140 Err(self.bad_type(Unsupported
::Integer
))
143 fn serialize_u16(self, _
: u16) -> Result
<Self::Ok
, Self::Error
> {
144 Err(self.bad_type(Unsupported
::Integer
))
147 fn serialize_u32(self, _
: u32) -> Result
<Self::Ok
, Self::Error
> {
148 Err(self.bad_type(Unsupported
::Integer
))
151 fn serialize_u64(self, _
: u64) -> Result
<Self::Ok
, Self::Error
> {
152 Err(self.bad_type(Unsupported
::Integer
))
155 fn serialize_f32(self, _
: f32) -> Result
<Self::Ok
, Self::Error
> {
156 Err(self.bad_type(Unsupported
::Float
))
159 fn serialize_f64(self, _
: f64) -> Result
<Self::Ok
, Self::Error
> {
160 Err(self.bad_type(Unsupported
::Float
))
163 fn serialize_char(self, _
: char) -> Result
<Self::Ok
, Self::Error
> {
164 Err(self.bad_type(Unsupported
::Char
))
167 fn serialize_str(self, _
: &str) -> Result
<Self::Ok
, Self::Error
> {
168 Err(self.bad_type(Unsupported
::String
))
171 fn serialize_bytes(self, _
: &[u8]) -> Result
<Self::Ok
, Self::Error
> {
172 Err(self.bad_type(Unsupported
::ByteArray
))
175 fn serialize_none(self) -> Result
<Self::Ok
, Self::Error
> {
176 Err(self.bad_type(Unsupported
::Optional
))
179 fn serialize_some
<T
: ?Sized
>(self, _
: &T
) -> Result
<Self::Ok
, Self::Error
>
183 Err(self.bad_type(Unsupported
::Optional
))
186 fn serialize_unit(self) -> Result
<Self::Ok
, Self::Error
> {
187 Err(self.bad_type(Unsupported
::Unit
))
190 fn serialize_unit_struct(self, _
: &'
static str) -> Result
<Self::Ok
, Self::Error
> {
191 let mut map
= try
!(self.delegate
.serialize_map(Some(1)));
192 try
!(map
.serialize_entry(self.tag
, self.variant_name
));
196 fn serialize_unit_variant(
200 inner_variant
: &'
static str,
201 ) -> Result
<Self::Ok
, Self::Error
> {
202 let mut map
= try
!(self.delegate
.serialize_map(Some(2)));
203 try
!(map
.serialize_entry(self.tag
, self.variant_name
));
204 try
!(map
.serialize_entry(inner_variant
, &()));
208 fn serialize_newtype_struct
<T
: ?Sized
>(
212 ) -> Result
<Self::Ok
, Self::Error
>
216 value
.serialize(self)
219 fn serialize_newtype_variant
<T
: ?Sized
>(
223 inner_variant
: &'
static str,
225 ) -> Result
<Self::Ok
, Self::Error
>
229 let mut map
= try
!(self.delegate
.serialize_map(Some(2)));
230 try
!(map
.serialize_entry(self.tag
, self.variant_name
));
231 try
!(map
.serialize_entry(inner_variant
, inner_value
));
235 fn serialize_seq(self, _
: Option
<usize>) -> Result
<Self::SerializeSeq
, Self::Error
> {
236 Err(self.bad_type(Unsupported
::Sequence
))
239 fn serialize_tuple(self, _
: usize) -> Result
<Self::SerializeTuple
, Self::Error
> {
240 Err(self.bad_type(Unsupported
::Tuple
))
243 fn serialize_tuple_struct(
247 ) -> Result
<Self::SerializeTupleStruct
, Self::Error
> {
248 Err(self.bad_type(Unsupported
::TupleStruct
))
251 #[cfg(not(any(feature = "std", feature = "alloc")))]
252 fn serialize_tuple_variant(
258 ) -> Result
<Self::SerializeTupleVariant
, Self::Error
> {
259 // Lack of push-based serialization means we need to buffer the content
260 // of the tuple variant, so it requires std.
261 Err(self.bad_type(Unsupported
::Enum
))
264 #[cfg(any(feature = "std", feature = "alloc"))]
265 fn serialize_tuple_variant(
269 inner_variant
: &'
static str,
271 ) -> Result
<Self::SerializeTupleVariant
, Self::Error
> {
272 let mut map
= try
!(self.delegate
.serialize_map(Some(2)));
273 try
!(map
.serialize_entry(self.tag
, self.variant_name
));
274 try
!(map
.serialize_key(inner_variant
));
275 Ok(SerializeTupleVariantAsMapValue
::new(
282 fn serialize_map(self, len
: Option
<usize>) -> Result
<Self::SerializeMap
, Self::Error
> {
283 let mut map
= try
!(self.delegate
.serialize_map(len
.map(|len
| len
+ 1)));
284 try
!(map
.serialize_entry(self.tag
, self.variant_name
));
292 ) -> Result
<Self::SerializeStruct
, Self::Error
> {
293 let mut state
= try
!(self.delegate
.serialize_struct(name
, len
+ 1));
294 try
!(state
.serialize_field(self.tag
, self.variant_name
));
298 #[cfg(not(any(feature = "std", feature = "alloc")))]
299 fn serialize_struct_variant(
305 ) -> Result
<Self::SerializeStructVariant
, Self::Error
> {
306 // Lack of push-based serialization means we need to buffer the content
307 // of the struct variant, so it requires std.
308 Err(self.bad_type(Unsupported
::Enum
))
311 #[cfg(any(feature = "std", feature = "alloc"))]
312 fn serialize_struct_variant(
316 inner_variant
: &'
static str,
318 ) -> Result
<Self::SerializeStructVariant
, Self::Error
> {
319 let mut map
= try
!(self.delegate
.serialize_map(Some(2)));
320 try
!(map
.serialize_entry(self.tag
, self.variant_name
));
321 try
!(map
.serialize_key(inner_variant
));
322 Ok(SerializeStructVariantAsMapValue
::new(
329 #[cfg(not(any(feature = "std", feature = "alloc")))]
330 fn collect_str
<T
: ?Sized
>(self, _
: &T
) -> Result
<Self::Ok
, Self::Error
>
334 Err(self.bad_type(Unsupported
::String
))
338 #[cfg(any(feature = "std", feature = "alloc"))]
342 use ser
::{self, Serialize, Serializer}
;
344 pub struct SerializeTupleVariantAsMapValue
<M
> {
347 fields
: Vec
<Content
>,
350 impl<M
> SerializeTupleVariantAsMapValue
<M
> {
351 pub fn new(map
: M
, name
: &'
static str, len
: usize) -> Self {
352 SerializeTupleVariantAsMapValue
{
355 fields
: Vec
::with_capacity(len
),
360 impl<M
> ser
::SerializeTupleVariant
for SerializeTupleVariantAsMapValue
<M
>
362 M
: ser
::SerializeMap
,
365 type Error
= M
::Error
;
367 fn serialize_field
<T
: ?Sized
>(&mut self, value
: &T
) -> Result
<(), M
::Error
>
371 let value
= try
!(value
.serialize(ContentSerializer
::<M
::Error
>::new()));
372 self.fields
.push(value
);
376 fn end(mut self) -> Result
<M
::Ok
, M
::Error
> {
379 .serialize_value(&Content
::TupleStruct(self.name
, self.fields
)));
384 pub struct SerializeStructVariantAsMapValue
<M
> {
387 fields
: Vec
<(&'
static str, Content
)>,
390 impl<M
> SerializeStructVariantAsMapValue
<M
> {
391 pub fn new(map
: M
, name
: &'
static str, len
: usize) -> Self {
392 SerializeStructVariantAsMapValue
{
395 fields
: Vec
::with_capacity(len
),
400 impl<M
> ser
::SerializeStructVariant
for SerializeStructVariantAsMapValue
<M
>
402 M
: ser
::SerializeMap
,
405 type Error
= M
::Error
;
407 fn serialize_field
<T
: ?Sized
>(
411 ) -> Result
<(), M
::Error
>
415 let value
= try
!(value
.serialize(ContentSerializer
::<M
::Error
>::new()));
416 self.fields
.push((key
, value
));
420 fn end(mut self) -> Result
<M
::Ok
, M
::Error
> {
423 .serialize_value(&Content
::Struct(self.name
, self.fields
)));
452 UnitStruct(&'
static str),
453 UnitVariant(&'
static str, u32, &'
static str),
454 NewtypeStruct(&'
static str, Box
<Content
>),
455 NewtypeVariant(&'
static str, u32, &'
static str, Box
<Content
>),
459 TupleStruct(&'
static str, Vec
<Content
>),
460 TupleVariant(&'
static str, u32, &'
static str, Vec
<Content
>),
461 Map(Vec
<(Content
, Content
)>),
462 Struct(&'
static str, Vec
<(&'
static str, Content
)>),
467 Vec
<(&'
static str, Content
)>,
471 impl Serialize
for Content
{
472 fn serialize
<S
>(&self, serializer
: S
) -> Result
<S
::Ok
, S
::Error
>
477 Content
::Bool(b
) => serializer
.serialize_bool(b
),
478 Content
::U8(u
) => serializer
.serialize_u8(u
),
479 Content
::U16(u
) => serializer
.serialize_u16(u
),
480 Content
::U32(u
) => serializer
.serialize_u32(u
),
481 Content
::U64(u
) => serializer
.serialize_u64(u
),
482 Content
::I8(i
) => serializer
.serialize_i8(i
),
483 Content
::I16(i
) => serializer
.serialize_i16(i
),
484 Content
::I32(i
) => serializer
.serialize_i32(i
),
485 Content
::I64(i
) => serializer
.serialize_i64(i
),
486 Content
::F32(f
) => serializer
.serialize_f32(f
),
487 Content
::F64(f
) => serializer
.serialize_f64(f
),
488 Content
::Char(c
) => serializer
.serialize_char(c
),
489 Content
::String(ref s
) => serializer
.serialize_str(s
),
490 Content
::Bytes(ref b
) => serializer
.serialize_bytes(b
),
491 Content
::None
=> serializer
.serialize_none(),
492 Content
::Some(ref c
) => serializer
.serialize_some(&**c
),
493 Content
::Unit
=> serializer
.serialize_unit(),
494 Content
::UnitStruct(n
) => serializer
.serialize_unit_struct(n
),
495 Content
::UnitVariant(n
, i
, v
) => serializer
.serialize_unit_variant(n
, i
, v
),
496 Content
::NewtypeStruct(n
, ref c
) => serializer
.serialize_newtype_struct(n
, &**c
),
497 Content
::NewtypeVariant(n
, i
, v
, ref c
) => {
498 serializer
.serialize_newtype_variant(n
, i
, v
, &**c
)
500 Content
::Seq(ref elements
) => elements
.serialize(serializer
),
501 Content
::Tuple(ref elements
) => {
502 use ser
::SerializeTuple
;
503 let mut tuple
= try
!(serializer
.serialize_tuple(elements
.len()));
505 try
!(tuple
.serialize_element(e
));
509 Content
::TupleStruct(n
, ref fields
) => {
510 use ser
::SerializeTupleStruct
;
511 let mut ts
= try
!(serializer
.serialize_tuple_struct(n
, fields
.len()));
513 try
!(ts
.serialize_field(f
));
517 Content
::TupleVariant(n
, i
, v
, ref fields
) => {
518 use ser
::SerializeTupleVariant
;
519 let mut tv
= try
!(serializer
.serialize_tuple_variant(n
, i
, v
, fields
.len()));
521 try
!(tv
.serialize_field(f
));
525 Content
::Map(ref entries
) => {
526 use ser
::SerializeMap
;
527 let mut map
= try
!(serializer
.serialize_map(Some(entries
.len())));
528 for &(ref k
, ref v
) in entries
{
529 try
!(map
.serialize_entry(k
, v
));
533 Content
::Struct(n
, ref fields
) => {
534 use ser
::SerializeStruct
;
535 let mut s
= try
!(serializer
.serialize_struct(n
, fields
.len()));
536 for &(k
, ref v
) in fields
{
537 try
!(s
.serialize_field(k
, v
));
541 Content
::StructVariant(n
, i
, v
, ref fields
) => {
542 use ser
::SerializeStructVariant
;
543 let mut sv
= try
!(serializer
.serialize_struct_variant(n
, i
, v
, fields
.len()));
544 for &(k
, ref v
) in fields
{
545 try
!(sv
.serialize_field(k
, v
));
553 pub struct ContentSerializer
<E
> {
554 error
: PhantomData
<E
>,
557 impl<E
> ContentSerializer
<E
> {
558 pub fn new() -> Self {
559 ContentSerializer { error: PhantomData }
563 impl<E
> Serializer
for ContentSerializer
<E
>
570 type SerializeSeq
= SerializeSeq
<E
>;
571 type SerializeTuple
= SerializeTuple
<E
>;
572 type SerializeTupleStruct
= SerializeTupleStruct
<E
>;
573 type SerializeTupleVariant
= SerializeTupleVariant
<E
>;
574 type SerializeMap
= SerializeMap
<E
>;
575 type SerializeStruct
= SerializeStruct
<E
>;
576 type SerializeStructVariant
= SerializeStructVariant
<E
>;
578 fn serialize_bool(self, v
: bool
) -> Result
<Content
, E
> {
582 fn serialize_i8(self, v
: i8) -> Result
<Content
, E
> {
586 fn serialize_i16(self, v
: i16) -> Result
<Content
, E
> {
590 fn serialize_i32(self, v
: i32) -> Result
<Content
, E
> {
594 fn serialize_i64(self, v
: i64) -> Result
<Content
, E
> {
598 fn serialize_u8(self, v
: u8) -> Result
<Content
, E
> {
602 fn serialize_u16(self, v
: u16) -> Result
<Content
, E
> {
606 fn serialize_u32(self, v
: u32) -> Result
<Content
, E
> {
610 fn serialize_u64(self, v
: u64) -> Result
<Content
, E
> {
614 fn serialize_f32(self, v
: f32) -> Result
<Content
, E
> {
618 fn serialize_f64(self, v
: f64) -> Result
<Content
, E
> {
622 fn serialize_char(self, v
: char) -> Result
<Content
, E
> {
626 fn serialize_str(self, value
: &str) -> Result
<Content
, E
> {
627 Ok(Content
::String(value
.to_owned()))
630 fn serialize_bytes(self, value
: &[u8]) -> Result
<Content
, E
> {
631 Ok(Content
::Bytes(value
.to_owned()))
634 fn serialize_none(self) -> Result
<Content
, E
> {
638 fn serialize_some
<T
: ?Sized
>(self, value
: &T
) -> Result
<Content
, E
>
642 Ok(Content
::Some(Box
::new(try
!(value
.serialize(self)))))
645 fn serialize_unit(self) -> Result
<Content
, E
> {
649 fn serialize_unit_struct(self, name
: &'
static str) -> Result
<Content
, E
> {
650 Ok(Content
::UnitStruct(name
))
653 fn serialize_unit_variant(
657 variant
: &'
static str,
658 ) -> Result
<Content
, E
> {
659 Ok(Content
::UnitVariant(name
, variant_index
, variant
))
662 fn serialize_newtype_struct
<T
: ?Sized
>(
666 ) -> Result
<Content
, E
>
670 Ok(Content
::NewtypeStruct(
672 Box
::new(try
!(value
.serialize(self))),
676 fn serialize_newtype_variant
<T
: ?Sized
>(
680 variant
: &'
static str,
682 ) -> Result
<Content
, E
>
686 Ok(Content
::NewtypeVariant(
690 Box
::new(try
!(value
.serialize(self))),
694 fn serialize_seq(self, len
: Option
<usize>) -> Result
<Self::SerializeSeq
, E
> {
696 elements
: Vec
::with_capacity(len
.unwrap_or(0)),
701 fn serialize_tuple(self, len
: usize) -> Result
<Self::SerializeTuple
, E
> {
703 elements
: Vec
::with_capacity(len
),
708 fn serialize_tuple_struct(
712 ) -> Result
<Self::SerializeTupleStruct
, E
> {
713 Ok(SerializeTupleStruct
{
715 fields
: Vec
::with_capacity(len
),
720 fn serialize_tuple_variant(
724 variant
: &'
static str,
726 ) -> Result
<Self::SerializeTupleVariant
, E
> {
727 Ok(SerializeTupleVariant
{
729 variant_index
: variant_index
,
731 fields
: Vec
::with_capacity(len
),
736 fn serialize_map(self, len
: Option
<usize>) -> Result
<Self::SerializeMap
, E
> {
738 entries
: Vec
::with_capacity(len
.unwrap_or(0)),
748 ) -> Result
<Self::SerializeStruct
, E
> {
751 fields
: Vec
::with_capacity(len
),
756 fn serialize_struct_variant(
760 variant
: &'
static str,
762 ) -> Result
<Self::SerializeStructVariant
, E
> {
763 Ok(SerializeStructVariant
{
765 variant_index
: variant_index
,
767 fields
: Vec
::with_capacity(len
),
773 pub struct SerializeSeq
<E
> {
774 elements
: Vec
<Content
>,
775 error
: PhantomData
<E
>,
778 impl<E
> ser
::SerializeSeq
for SerializeSeq
<E
>
785 fn serialize_element
<T
: ?Sized
>(&mut self, value
: &T
) -> Result
<(), E
>
789 let value
= try
!(value
.serialize(ContentSerializer
::<E
>::new()));
790 self.elements
.push(value
);
794 fn end(self) -> Result
<Content
, E
> {
795 Ok(Content
::Seq(self.elements
))
799 pub struct SerializeTuple
<E
> {
800 elements
: Vec
<Content
>,
801 error
: PhantomData
<E
>,
804 impl<E
> ser
::SerializeTuple
for SerializeTuple
<E
>
811 fn serialize_element
<T
: ?Sized
>(&mut self, value
: &T
) -> Result
<(), E
>
815 let value
= try
!(value
.serialize(ContentSerializer
::<E
>::new()));
816 self.elements
.push(value
);
820 fn end(self) -> Result
<Content
, E
> {
821 Ok(Content
::Tuple(self.elements
))
825 pub struct SerializeTupleStruct
<E
> {
827 fields
: Vec
<Content
>,
828 error
: PhantomData
<E
>,
831 impl<E
> ser
::SerializeTupleStruct
for SerializeTupleStruct
<E
>
838 fn serialize_field
<T
: ?Sized
>(&mut self, value
: &T
) -> Result
<(), E
>
842 let value
= try
!(value
.serialize(ContentSerializer
::<E
>::new()));
843 self.fields
.push(value
);
847 fn end(self) -> Result
<Content
, E
> {
848 Ok(Content
::TupleStruct(self.name
, self.fields
))
852 pub struct SerializeTupleVariant
<E
> {
855 variant
: &'
static str,
856 fields
: Vec
<Content
>,
857 error
: PhantomData
<E
>,
860 impl<E
> ser
::SerializeTupleVariant
for SerializeTupleVariant
<E
>
867 fn serialize_field
<T
: ?Sized
>(&mut self, value
: &T
) -> Result
<(), E
>
871 let value
= try
!(value
.serialize(ContentSerializer
::<E
>::new()));
872 self.fields
.push(value
);
876 fn end(self) -> Result
<Content
, E
> {
877 Ok(Content
::TupleVariant(
886 pub struct SerializeMap
<E
> {
887 entries
: Vec
<(Content
, Content
)>,
888 key
: Option
<Content
>,
889 error
: PhantomData
<E
>,
892 impl<E
> ser
::SerializeMap
for SerializeMap
<E
>
899 fn serialize_key
<T
: ?Sized
>(&mut self, key
: &T
) -> Result
<(), E
>
903 let key
= try
!(key
.serialize(ContentSerializer
::<E
>::new()));
904 self.key
= Some(key
);
908 fn serialize_value
<T
: ?Sized
>(&mut self, value
: &T
) -> Result
<(), E
>
915 .expect("serialize_value called before serialize_key");
916 let value
= try
!(value
.serialize(ContentSerializer
::<E
>::new()));
917 self.entries
.push((key
, value
));
921 fn end(self) -> Result
<Content
, E
> {
922 Ok(Content
::Map(self.entries
))
925 fn serialize_entry
<K
: ?Sized
, V
: ?Sized
>(&mut self, key
: &K
, value
: &V
) -> Result
<(), E
>
930 let key
= try
!(key
.serialize(ContentSerializer
::<E
>::new()));
931 let value
= try
!(value
.serialize(ContentSerializer
::<E
>::new()));
932 self.entries
.push((key
, value
));
937 pub struct SerializeStruct
<E
> {
939 fields
: Vec
<(&'
static str, Content
)>,
940 error
: PhantomData
<E
>,
943 impl<E
> ser
::SerializeStruct
for SerializeStruct
<E
>
950 fn serialize_field
<T
: ?Sized
>(&mut self, key
: &'
static str, value
: &T
) -> Result
<(), E
>
954 let value
= try
!(value
.serialize(ContentSerializer
::<E
>::new()));
955 self.fields
.push((key
, value
));
959 fn end(self) -> Result
<Content
, E
> {
960 Ok(Content
::Struct(self.name
, self.fields
))
964 pub struct SerializeStructVariant
<E
> {
967 variant
: &'
static str,
968 fields
: Vec
<(&'
static str, Content
)>,
969 error
: PhantomData
<E
>,
972 impl<E
> ser
::SerializeStructVariant
for SerializeStructVariant
<E
>
979 fn serialize_field
<T
: ?Sized
>(&mut self, key
: &'
static str, value
: &T
) -> Result
<(), E
>
983 let value
= try
!(value
.serialize(ContentSerializer
::<E
>::new()));
984 self.fields
.push((key
, value
));
988 fn end(self) -> Result
<Content
, E
> {
989 Ok(Content
::StructVariant(
999 #[cfg(any(feature = "std", feature = "alloc"))]
1000 pub struct FlatMapSerializer
<'a
, M
: 'a
>(pub &'a
mut M
);
1002 #[cfg(any(feature = "std", feature = "alloc"))]
1003 impl<'a
, M
> FlatMapSerializer
<'a
, M
>
1005 M
: SerializeMap
+ 'a
,
1007 fn bad_type(what
: Unsupported
) -> M
::Error
{
1008 ser
::Error
::custom(format_args
!(
1009 "can only flatten structs and maps (got {})",
1015 #[cfg(any(feature = "std", feature = "alloc"))]
1016 impl<'a
, M
> Serializer
for FlatMapSerializer
<'a
, M
>
1018 M
: SerializeMap
+ 'a
,
1021 type Error
= M
::Error
;
1023 type SerializeSeq
= Impossible
<Self::Ok
, M
::Error
>;
1024 type SerializeTuple
= Impossible
<Self::Ok
, M
::Error
>;
1025 type SerializeTupleStruct
= Impossible
<Self::Ok
, M
::Error
>;
1026 type SerializeMap
= FlatMapSerializeMap
<'a
, M
>;
1027 type SerializeStruct
= FlatMapSerializeStruct
<'a
, M
>;
1028 type SerializeTupleVariant
= Impossible
<Self::Ok
, M
::Error
>;
1029 type SerializeStructVariant
= FlatMapSerializeStructVariantAsMapValue
<'a
, M
>;
1031 fn serialize_bool(self, _
: bool
) -> Result
<Self::Ok
, Self::Error
> {
1032 Err(Self::bad_type(Unsupported
::Boolean
))
1035 fn serialize_i8(self, _
: i8) -> Result
<Self::Ok
, Self::Error
> {
1036 Err(Self::bad_type(Unsupported
::Integer
))
1039 fn serialize_i16(self, _
: i16) -> Result
<Self::Ok
, Self::Error
> {
1040 Err(Self::bad_type(Unsupported
::Integer
))
1043 fn serialize_i32(self, _
: i32) -> Result
<Self::Ok
, Self::Error
> {
1044 Err(Self::bad_type(Unsupported
::Integer
))
1047 fn serialize_i64(self, _
: i64) -> Result
<Self::Ok
, Self::Error
> {
1048 Err(Self::bad_type(Unsupported
::Integer
))
1051 fn serialize_u8(self, _
: u8) -> Result
<Self::Ok
, Self::Error
> {
1052 Err(Self::bad_type(Unsupported
::Integer
))
1055 fn serialize_u16(self, _
: u16) -> Result
<Self::Ok
, Self::Error
> {
1056 Err(Self::bad_type(Unsupported
::Integer
))
1059 fn serialize_u32(self, _
: u32) -> Result
<Self::Ok
, Self::Error
> {
1060 Err(Self::bad_type(Unsupported
::Integer
))
1063 fn serialize_u64(self, _
: u64) -> Result
<Self::Ok
, Self::Error
> {
1064 Err(Self::bad_type(Unsupported
::Integer
))
1067 fn serialize_f32(self, _
: f32) -> Result
<Self::Ok
, Self::Error
> {
1068 Err(Self::bad_type(Unsupported
::Float
))
1071 fn serialize_f64(self, _
: f64) -> Result
<Self::Ok
, Self::Error
> {
1072 Err(Self::bad_type(Unsupported
::Float
))
1075 fn serialize_char(self, _
: char) -> Result
<Self::Ok
, Self::Error
> {
1076 Err(Self::bad_type(Unsupported
::Char
))
1079 fn serialize_str(self, _
: &str) -> Result
<Self::Ok
, Self::Error
> {
1080 Err(Self::bad_type(Unsupported
::String
))
1083 fn serialize_bytes(self, _
: &[u8]) -> Result
<Self::Ok
, Self::Error
> {
1084 Err(Self::bad_type(Unsupported
::ByteArray
))
1087 fn serialize_none(self) -> Result
<Self::Ok
, Self::Error
> {
1091 fn serialize_some
<T
: ?Sized
>(self, value
: &T
) -> Result
<Self::Ok
, Self::Error
>
1095 value
.serialize(self)
1098 fn serialize_unit(self) -> Result
<Self::Ok
, Self::Error
> {
1102 fn serialize_unit_struct(self, _
: &'
static str) -> Result
<Self::Ok
, Self::Error
> {
1103 Err(Self::bad_type(Unsupported
::UnitStruct
))
1106 fn serialize_unit_variant(
1111 ) -> Result
<Self::Ok
, Self::Error
> {
1112 Err(Self::bad_type(Unsupported
::Enum
))
1115 fn serialize_newtype_struct
<T
: ?Sized
>(
1119 ) -> Result
<Self::Ok
, Self::Error
>
1123 value
.serialize(self)
1126 fn serialize_newtype_variant
<T
: ?Sized
>(
1130 variant
: &'
static str,
1132 ) -> Result
<Self::Ok
, Self::Error
>
1136 try
!(self.0.serialize_key(variant
));
1137 self.0.serialize_value(value
)
1140 fn serialize_seq(self, _
: Option
<usize>) -> Result
<Self::SerializeSeq
, Self::Error
> {
1141 Err(Self::bad_type(Unsupported
::Sequence
))
1144 fn serialize_tuple(self, _
: usize) -> Result
<Self::SerializeTuple
, Self::Error
> {
1145 Err(Self::bad_type(Unsupported
::Tuple
))
1148 fn serialize_tuple_struct(
1152 ) -> Result
<Self::SerializeTupleStruct
, Self::Error
> {
1153 Err(Self::bad_type(Unsupported
::TupleStruct
))
1156 fn serialize_tuple_variant(
1162 ) -> Result
<Self::SerializeTupleVariant
, Self::Error
> {
1163 Err(Self::bad_type(Unsupported
::Enum
))
1166 fn serialize_map(self, _
: Option
<usize>) -> Result
<Self::SerializeMap
, Self::Error
> {
1167 Ok(FlatMapSerializeMap(self.0))
1170 fn serialize_struct(
1174 ) -> Result
<Self::SerializeStruct
, Self::Error
> {
1175 Ok(FlatMapSerializeStruct(self.0))
1178 fn serialize_struct_variant(
1182 inner_variant
: &'
static str,
1184 ) -> Result
<Self::SerializeStructVariant
, Self::Error
> {
1185 try
!(self.0.serialize_key(inner_variant
));
1186 Ok(FlatMapSerializeStructVariantAsMapValue
::new(
1193 #[cfg(any(feature = "std", feature = "alloc"))]
1194 pub struct FlatMapSerializeMap
<'a
, M
: 'a
>(&'a
mut M
);
1196 #[cfg(any(feature = "std", feature = "alloc"))]
1197 impl<'a
, M
> ser
::SerializeMap
for FlatMapSerializeMap
<'a
, M
>
1199 M
: SerializeMap
+ 'a
,
1202 type Error
= M
::Error
;
1204 fn serialize_key
<T
: ?Sized
>(&mut self, key
: &T
) -> Result
<(), Self::Error
>
1208 self.0.serialize_key(key
)
1211 fn serialize_value
<T
: ?Sized
>(&mut self, value
: &T
) -> Result
<(), Self::Error
>
1215 self.0.serialize_value(value
)
1218 fn serialize_entry
<K
: ?Sized
, V
: ?Sized
>(
1222 ) -> Result
<(), Self::Error
>
1227 self.0.serialize_entry(key
, value
)
1230 fn end(self) -> Result
<(), Self::Error
> {
1235 #[cfg(any(feature = "std", feature = "alloc"))]
1236 pub struct FlatMapSerializeStruct
<'a
, M
: 'a
>(&'a
mut M
);
1238 #[cfg(any(feature = "std", feature = "alloc"))]
1239 impl<'a
, M
> ser
::SerializeStruct
for FlatMapSerializeStruct
<'a
, M
>
1241 M
: SerializeMap
+ 'a
,
1244 type Error
= M
::Error
;
1246 fn serialize_field
<T
: ?Sized
>(
1250 ) -> Result
<(), Self::Error
>
1254 self.0.serialize_entry(key
, value
)
1257 fn end(self) -> Result
<(), Self::Error
> {
1262 #[cfg(any(feature = "std", feature = "alloc"))]
1263 pub struct FlatMapSerializeStructVariantAsMapValue
<'a
, M
: 'a
> {
1266 fields
: Vec
<(&'
static str, Content
)>,
1269 #[cfg(any(feature = "std", feature = "alloc"))]
1270 impl<'a
, M
> FlatMapSerializeStructVariantAsMapValue
<'a
, M
>
1272 M
: SerializeMap
+ 'a
,
1274 fn new(map
: &'a
mut M
, name
: &'
static str) -> FlatMapSerializeStructVariantAsMapValue
<'a
, M
> {
1275 FlatMapSerializeStructVariantAsMapValue
{
1283 #[cfg(any(feature = "std", feature = "alloc"))]
1284 impl<'a
, M
> ser
::SerializeStructVariant
for FlatMapSerializeStructVariantAsMapValue
<'a
, M
>
1286 M
: SerializeMap
+ 'a
,
1289 type Error
= M
::Error
;
1291 fn serialize_field
<T
: ?Sized
>(
1295 ) -> Result
<(), Self::Error
>
1299 let value
= try
!(value
.serialize(ContentSerializer
::<M
::Error
>::new()));
1300 self.fields
.push((key
, value
));
1304 fn end(self) -> Result
<(), Self::Error
> {
1307 .serialize_value(&Content
::Struct(self.name
, self.fields
)));