1 use lib::*;
2
3 use ser::{self, Impossible, Serialize, SerializeMap, SerializeStruct, Serializer};
4
5 #[cfg(any(feature = "std", feature = "alloc"))]
6 use self::content::{
7 Content, ContentSerializer, SerializeStructVariantAsMapValue, SerializeTupleVariantAsMapValue,
8 };
9
10 /// Used to check that serde(getter) attributes return the expected type.
11 /// Not public API.
constrain<T: ?Sized>(t: &T) -> &T12 pub fn constrain<T: ?Sized>(t: &T) -> &T {
13 t
14 }
15
16 /// Not public API.
serialize_tagged_newtype<S, T>( serializer: S, type_ident: &'static str, variant_ident: &'static str, tag: &'static str, variant_name: &'static str, value: &T, ) -> Result<S::Ok, S::Error> where S: Serializer, T: Serialize,17 pub fn serialize_tagged_newtype<S, T>(
18 serializer: S,
19 type_ident: &'static str,
20 variant_ident: &'static str,
21 tag: &'static str,
22 variant_name: &'static str,
23 value: &T,
24 ) -> Result<S::Ok, S::Error>
25 where
26 S: Serializer,
27 T: Serialize,
28 {
29 value.serialize(TaggedSerializer {
30 type_ident: type_ident,
31 variant_ident: variant_ident,
32 tag: tag,
33 variant_name: variant_name,
34 delegate: serializer,
35 })
36 }
37
38 struct TaggedSerializer<S> {
39 type_ident: &'static str,
40 variant_ident: &'static str,
41 tag: &'static str,
42 variant_name: &'static str,
43 delegate: S,
44 }
45
46 enum Unsupported {
47 Boolean,
48 Integer,
49 Float,
50 Char,
51 String,
52 ByteArray,
53 Optional,
54 Unit,
55 #[cfg(any(feature = "std", feature = "alloc"))]
56 UnitStruct,
57 Sequence,
58 Tuple,
59 TupleStruct,
60 Enum,
61 }
62
63 impl Display for Unsupported {
fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result64 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
65 match *self {
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"),
80 }
81 }
82 }
83
84 impl<S> TaggedSerializer<S>
85 where
86 S: Serializer,
87 {
bad_type(self, what: Unsupported) -> S::Error88 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
92 ))
93 }
94 }
95
96 impl<S> Serializer for TaggedSerializer<S>
97 where
98 S: Serializer,
99 {
100 type Ok = S::Ok;
101 type Error = S::Error;
102
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;
108
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>;
113
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>;
118
serialize_bool(self, _: bool) -> Result<Self::Ok, Self::Error>119 fn serialize_bool(self, _: bool) -> Result<Self::Ok, Self::Error> {
120 Err(self.bad_type(Unsupported::Boolean))
121 }
122
serialize_i8(self, _: i8) -> Result<Self::Ok, Self::Error>123 fn serialize_i8(self, _: i8) -> Result<Self::Ok, Self::Error> {
124 Err(self.bad_type(Unsupported::Integer))
125 }
126
serialize_i16(self, _: i16) -> Result<Self::Ok, Self::Error>127 fn serialize_i16(self, _: i16) -> Result<Self::Ok, Self::Error> {
128 Err(self.bad_type(Unsupported::Integer))
129 }
130
serialize_i32(self, _: i32) -> Result<Self::Ok, Self::Error>131 fn serialize_i32(self, _: i32) -> Result<Self::Ok, Self::Error> {
132 Err(self.bad_type(Unsupported::Integer))
133 }
134
serialize_i64(self, _: i64) -> Result<Self::Ok, Self::Error>135 fn serialize_i64(self, _: i64) -> Result<Self::Ok, Self::Error> {
136 Err(self.bad_type(Unsupported::Integer))
137 }
138
serialize_u8(self, _: u8) -> Result<Self::Ok, Self::Error>139 fn serialize_u8(self, _: u8) -> Result<Self::Ok, Self::Error> {
140 Err(self.bad_type(Unsupported::Integer))
141 }
142
serialize_u16(self, _: u16) -> Result<Self::Ok, Self::Error>143 fn serialize_u16(self, _: u16) -> Result<Self::Ok, Self::Error> {
144 Err(self.bad_type(Unsupported::Integer))
145 }
146
serialize_u32(self, _: u32) -> Result<Self::Ok, Self::Error>147 fn serialize_u32(self, _: u32) -> Result<Self::Ok, Self::Error> {
148 Err(self.bad_type(Unsupported::Integer))
149 }
150
serialize_u64(self, _: u64) -> Result<Self::Ok, Self::Error>151 fn serialize_u64(self, _: u64) -> Result<Self::Ok, Self::Error> {
152 Err(self.bad_type(Unsupported::Integer))
153 }
154
serialize_f32(self, _: f32) -> Result<Self::Ok, Self::Error>155 fn serialize_f32(self, _: f32) -> Result<Self::Ok, Self::Error> {
156 Err(self.bad_type(Unsupported::Float))
157 }
158
serialize_f64(self, _: f64) -> Result<Self::Ok, Self::Error>159 fn serialize_f64(self, _: f64) -> Result<Self::Ok, Self::Error> {
160 Err(self.bad_type(Unsupported::Float))
161 }
162
serialize_char(self, _: char) -> Result<Self::Ok, Self::Error>163 fn serialize_char(self, _: char) -> Result<Self::Ok, Self::Error> {
164 Err(self.bad_type(Unsupported::Char))
165 }
166
serialize_str(self, _: &str) -> Result<Self::Ok, Self::Error>167 fn serialize_str(self, _: &str) -> Result<Self::Ok, Self::Error> {
168 Err(self.bad_type(Unsupported::String))
169 }
170
serialize_bytes(self, _: &[u8]) -> Result<Self::Ok, Self::Error>171 fn serialize_bytes(self, _: &[u8]) -> Result<Self::Ok, Self::Error> {
172 Err(self.bad_type(Unsupported::ByteArray))
173 }
174
serialize_none(self) -> Result<Self::Ok, Self::Error>175 fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
176 Err(self.bad_type(Unsupported::Optional))
177 }
178
serialize_some<T: ?Sized>(self, _: &T) -> Result<Self::Ok, Self::Error> where T: Serialize,179 fn serialize_some<T: ?Sized>(self, _: &T) -> Result<Self::Ok, Self::Error>
180 where
181 T: Serialize,
182 {
183 Err(self.bad_type(Unsupported::Optional))
184 }
185
serialize_unit(self) -> Result<Self::Ok, Self::Error>186 fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
187 Err(self.bad_type(Unsupported::Unit))
188 }
189
serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error>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));
193 map.end()
194 }
195
serialize_unit_variant( self, _: &'static str, _: u32, inner_variant: &'static str, ) -> Result<Self::Ok, Self::Error>196 fn serialize_unit_variant(
197 self,
198 _: &'static str,
199 _: u32,
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, &()));
205 map.end()
206 }
207
serialize_newtype_struct<T: ?Sized>( self, _: &'static str, value: &T, ) -> Result<Self::Ok, Self::Error> where T: Serialize,208 fn serialize_newtype_struct<T: ?Sized>(
209 self,
210 _: &'static str,
211 value: &T,
212 ) -> Result<Self::Ok, Self::Error>
213 where
214 T: Serialize,
215 {
216 value.serialize(self)
217 }
218
serialize_newtype_variant<T: ?Sized>( self, _: &'static str, _: u32, inner_variant: &'static str, inner_value: &T, ) -> Result<Self::Ok, Self::Error> where T: Serialize,219 fn serialize_newtype_variant<T: ?Sized>(
220 self,
221 _: &'static str,
222 _: u32,
223 inner_variant: &'static str,
224 inner_value: &T,
225 ) -> Result<Self::Ok, Self::Error>
226 where
227 T: Serialize,
228 {
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));
232 map.end()
233 }
234
serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq, Self::Error>235 fn serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
236 Err(self.bad_type(Unsupported::Sequence))
237 }
238
serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, Self::Error>239 fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, Self::Error> {
240 Err(self.bad_type(Unsupported::Tuple))
241 }
242
serialize_tuple_struct( self, _: &'static str, _: usize, ) -> Result<Self::SerializeTupleStruct, Self::Error>243 fn serialize_tuple_struct(
244 self,
245 _: &'static str,
246 _: usize,
247 ) -> Result<Self::SerializeTupleStruct, Self::Error> {
248 Err(self.bad_type(Unsupported::TupleStruct))
249 }
250
251 #[cfg(not(any(feature = "std", feature = "alloc")))]
serialize_tuple_variant( self, _: &'static str, _: u32, _: &'static str, _: usize, ) -> Result<Self::SerializeTupleVariant, Self::Error>252 fn serialize_tuple_variant(
253 self,
254 _: &'static str,
255 _: u32,
256 _: &'static str,
257 _: usize,
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))
262 }
263
264 #[cfg(any(feature = "std", feature = "alloc"))]
serialize_tuple_variant( self, _: &'static str, _: u32, inner_variant: &'static str, len: usize, ) -> Result<Self::SerializeTupleVariant, Self::Error>265 fn serialize_tuple_variant(
266 self,
267 _: &'static str,
268 _: u32,
269 inner_variant: &'static str,
270 len: usize,
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(
276 map,
277 inner_variant,
278 len,
279 ))
280 }
281
serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error>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));
285 Ok(map)
286 }
287
serialize_struct( self, name: &'static str, len: usize, ) -> Result<Self::SerializeStruct, Self::Error>288 fn serialize_struct(
289 self,
290 name: &'static str,
291 len: usize,
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));
295 Ok(state)
296 }
297
298 #[cfg(not(any(feature = "std", feature = "alloc")))]
serialize_struct_variant( self, _: &'static str, _: u32, _: &'static str, _: usize, ) -> Result<Self::SerializeStructVariant, Self::Error>299 fn serialize_struct_variant(
300 self,
301 _: &'static str,
302 _: u32,
303 _: &'static str,
304 _: usize,
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))
309 }
310
311 #[cfg(any(feature = "std", feature = "alloc"))]
serialize_struct_variant( self, _: &'static str, _: u32, inner_variant: &'static str, len: usize, ) -> Result<Self::SerializeStructVariant, Self::Error>312 fn serialize_struct_variant(
313 self,
314 _: &'static str,
315 _: u32,
316 inner_variant: &'static str,
317 len: usize,
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(
323 map,
324 inner_variant,
325 len,
326 ))
327 }
328
329 #[cfg(not(any(feature = "std", feature = "alloc")))]
collect_str<T: ?Sized>(self, _: &T) -> Result<Self::Ok, Self::Error> where T: Display,330 fn collect_str<T: ?Sized>(self, _: &T) -> Result<Self::Ok, Self::Error>
331 where
332 T: Display,
333 {
334 Err(self.bad_type(Unsupported::String))
335 }
336 }
337
338 /// Used only by Serde doc tests. Not public API.
339 #[doc(hidden)]
340 #[derive(Debug)]
341 pub struct Error;
342
343 impl ser::Error for Error {
custom<T>(_: T) -> Self where T: Display,344 fn custom<T>(_: T) -> Self
345 where
346 T: Display,
347 {
348 unimplemented!()
349 }
350 }
351
352 #[cfg(feature = "std")]
353 impl error::Error for Error {
description(&self) -> &str354 fn description(&self) -> &str {
355 unimplemented!()
356 }
357 }
358
359 impl Display for Error {
fmt(&self, _: &mut fmt::Formatter) -> fmt::Result360 fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
361 unimplemented!()
362 }
363 }
364
365 #[cfg(any(feature = "std", feature = "alloc"))]
366 mod content {
367 use lib::*;
368
369 use ser::{self, Serialize, Serializer};
370
371 pub struct SerializeTupleVariantAsMapValue<M> {
372 map: M,
373 name: &'static str,
374 fields: Vec<Content>,
375 }
376
377 impl<M> SerializeTupleVariantAsMapValue<M> {
new(map: M, name: &'static str, len: usize) -> Self378 pub fn new(map: M, name: &'static str, len: usize) -> Self {
379 SerializeTupleVariantAsMapValue {
380 map: map,
381 name: name,
382 fields: Vec::with_capacity(len),
383 }
384 }
385 }
386
387 impl<M> ser::SerializeTupleVariant for SerializeTupleVariantAsMapValue<M>
388 where
389 M: ser::SerializeMap,
390 {
391 type Ok = M::Ok;
392 type Error = M::Error;
393
serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), M::Error> where T: Serialize,394 fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), M::Error>
395 where
396 T: Serialize,
397 {
398 let value = try!(value.serialize(ContentSerializer::<M::Error>::new()));
399 self.fields.push(value);
400 Ok(())
401 }
402
end(mut self) -> Result<M::Ok, M::Error>403 fn end(mut self) -> Result<M::Ok, M::Error> {
404 try!(self
405 .map
406 .serialize_value(&Content::TupleStruct(self.name, self.fields)));
407 self.map.end()
408 }
409 }
410
411 pub struct SerializeStructVariantAsMapValue<M> {
412 map: M,
413 name: &'static str,
414 fields: Vec<(&'static str, Content)>,
415 }
416
417 impl<M> SerializeStructVariantAsMapValue<M> {
new(map: M, name: &'static str, len: usize) -> Self418 pub fn new(map: M, name: &'static str, len: usize) -> Self {
419 SerializeStructVariantAsMapValue {
420 map: map,
421 name: name,
422 fields: Vec::with_capacity(len),
423 }
424 }
425 }
426
427 impl<M> ser::SerializeStructVariant for SerializeStructVariantAsMapValue<M>
428 where
429 M: ser::SerializeMap,
430 {
431 type Ok = M::Ok;
432 type Error = M::Error;
433
serialize_field<T: ?Sized>( &mut self, key: &'static str, value: &T, ) -> Result<(), M::Error> where T: Serialize,434 fn serialize_field<T: ?Sized>(
435 &mut self,
436 key: &'static str,
437 value: &T,
438 ) -> Result<(), M::Error>
439 where
440 T: Serialize,
441 {
442 let value = try!(value.serialize(ContentSerializer::<M::Error>::new()));
443 self.fields.push((key, value));
444 Ok(())
445 }
446
end(mut self) -> Result<M::Ok, M::Error>447 fn end(mut self) -> Result<M::Ok, M::Error> {
448 try!(self
449 .map
450 .serialize_value(&Content::Struct(self.name, self.fields)));
451 self.map.end()
452 }
453 }
454
455 #[derive(Debug)]
456 pub enum Content {
457 Bool(bool),
458
459 U8(u8),
460 U16(u16),
461 U32(u32),
462 U64(u64),
463
464 I8(i8),
465 I16(i16),
466 I32(i32),
467 I64(i64),
468
469 F32(f32),
470 F64(f64),
471
472 Char(char),
473 String(String),
474 Bytes(Vec<u8>),
475
476 None,
477 Some(Box<Content>),
478
479 Unit,
480 UnitStruct(&'static str),
481 UnitVariant(&'static str, u32, &'static str),
482 NewtypeStruct(&'static str, Box<Content>),
483 NewtypeVariant(&'static str, u32, &'static str, Box<Content>),
484
485 Seq(Vec<Content>),
486 Tuple(Vec<Content>),
487 TupleStruct(&'static str, Vec<Content>),
488 TupleVariant(&'static str, u32, &'static str, Vec<Content>),
489 Map(Vec<(Content, Content)>),
490 Struct(&'static str, Vec<(&'static str, Content)>),
491 StructVariant(
492 &'static str,
493 u32,
494 &'static str,
495 Vec<(&'static str, Content)>,
496 ),
497 }
498
499 impl Serialize for Content {
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,500 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
501 where
502 S: Serializer,
503 {
504 match *self {
505 Content::Bool(b) => serializer.serialize_bool(b),
506 Content::U8(u) => serializer.serialize_u8(u),
507 Content::U16(u) => serializer.serialize_u16(u),
508 Content::U32(u) => serializer.serialize_u32(u),
509 Content::U64(u) => serializer.serialize_u64(u),
510 Content::I8(i) => serializer.serialize_i8(i),
511 Content::I16(i) => serializer.serialize_i16(i),
512 Content::I32(i) => serializer.serialize_i32(i),
513 Content::I64(i) => serializer.serialize_i64(i),
514 Content::F32(f) => serializer.serialize_f32(f),
515 Content::F64(f) => serializer.serialize_f64(f),
516 Content::Char(c) => serializer.serialize_char(c),
517 Content::String(ref s) => serializer.serialize_str(s),
518 Content::Bytes(ref b) => serializer.serialize_bytes(b),
519 Content::None => serializer.serialize_none(),
520 Content::Some(ref c) => serializer.serialize_some(&**c),
521 Content::Unit => serializer.serialize_unit(),
522 Content::UnitStruct(n) => serializer.serialize_unit_struct(n),
523 Content::UnitVariant(n, i, v) => serializer.serialize_unit_variant(n, i, v),
524 Content::NewtypeStruct(n, ref c) => serializer.serialize_newtype_struct(n, &**c),
525 Content::NewtypeVariant(n, i, v, ref c) => {
526 serializer.serialize_newtype_variant(n, i, v, &**c)
527 }
528 Content::Seq(ref elements) => elements.serialize(serializer),
529 Content::Tuple(ref elements) => {
530 use ser::SerializeTuple;
531 let mut tuple = try!(serializer.serialize_tuple(elements.len()));
532 for e in elements {
533 try!(tuple.serialize_element(e));
534 }
535 tuple.end()
536 }
537 Content::TupleStruct(n, ref fields) => {
538 use ser::SerializeTupleStruct;
539 let mut ts = try!(serializer.serialize_tuple_struct(n, fields.len()));
540 for f in fields {
541 try!(ts.serialize_field(f));
542 }
543 ts.end()
544 }
545 Content::TupleVariant(n, i, v, ref fields) => {
546 use ser::SerializeTupleVariant;
547 let mut tv = try!(serializer.serialize_tuple_variant(n, i, v, fields.len()));
548 for f in fields {
549 try!(tv.serialize_field(f));
550 }
551 tv.end()
552 }
553 Content::Map(ref entries) => {
554 use ser::SerializeMap;
555 let mut map = try!(serializer.serialize_map(Some(entries.len())));
556 for &(ref k, ref v) in entries {
557 try!(map.serialize_entry(k, v));
558 }
559 map.end()
560 }
561 Content::Struct(n, ref fields) => {
562 use ser::SerializeStruct;
563 let mut s = try!(serializer.serialize_struct(n, fields.len()));
564 for &(k, ref v) in fields {
565 try!(s.serialize_field(k, v));
566 }
567 s.end()
568 }
569 Content::StructVariant(n, i, v, ref fields) => {
570 use ser::SerializeStructVariant;
571 let mut sv = try!(serializer.serialize_struct_variant(n, i, v, fields.len()));
572 for &(k, ref v) in fields {
573 try!(sv.serialize_field(k, v));
574 }
575 sv.end()
576 }
577 }
578 }
579 }
580
581 pub struct ContentSerializer<E> {
582 error: PhantomData<E>,
583 }
584
585 impl<E> ContentSerializer<E> {
new() -> Self586 pub fn new() -> Self {
587 ContentSerializer { error: PhantomData }
588 }
589 }
590
591 impl<E> Serializer for ContentSerializer<E>
592 where
593 E: ser::Error,
594 {
595 type Ok = Content;
596 type Error = E;
597
598 type SerializeSeq = SerializeSeq<E>;
599 type SerializeTuple = SerializeTuple<E>;
600 type SerializeTupleStruct = SerializeTupleStruct<E>;
601 type SerializeTupleVariant = SerializeTupleVariant<E>;
602 type SerializeMap = SerializeMap<E>;
603 type SerializeStruct = SerializeStruct<E>;
604 type SerializeStructVariant = SerializeStructVariant<E>;
605
serialize_bool(self, v: bool) -> Result<Content, E>606 fn serialize_bool(self, v: bool) -> Result<Content, E> {
607 Ok(Content::Bool(v))
608 }
609
serialize_i8(self, v: i8) -> Result<Content, E>610 fn serialize_i8(self, v: i8) -> Result<Content, E> {
611 Ok(Content::I8(v))
612 }
613
serialize_i16(self, v: i16) -> Result<Content, E>614 fn serialize_i16(self, v: i16) -> Result<Content, E> {
615 Ok(Content::I16(v))
616 }
617
serialize_i32(self, v: i32) -> Result<Content, E>618 fn serialize_i32(self, v: i32) -> Result<Content, E> {
619 Ok(Content::I32(v))
620 }
621
serialize_i64(self, v: i64) -> Result<Content, E>622 fn serialize_i64(self, v: i64) -> Result<Content, E> {
623 Ok(Content::I64(v))
624 }
625
serialize_u8(self, v: u8) -> Result<Content, E>626 fn serialize_u8(self, v: u8) -> Result<Content, E> {
627 Ok(Content::U8(v))
628 }
629
serialize_u16(self, v: u16) -> Result<Content, E>630 fn serialize_u16(self, v: u16) -> Result<Content, E> {
631 Ok(Content::U16(v))
632 }
633
serialize_u32(self, v: u32) -> Result<Content, E>634 fn serialize_u32(self, v: u32) -> Result<Content, E> {
635 Ok(Content::U32(v))
636 }
637
serialize_u64(self, v: u64) -> Result<Content, E>638 fn serialize_u64(self, v: u64) -> Result<Content, E> {
639 Ok(Content::U64(v))
640 }
641
serialize_f32(self, v: f32) -> Result<Content, E>642 fn serialize_f32(self, v: f32) -> Result<Content, E> {
643 Ok(Content::F32(v))
644 }
645
serialize_f64(self, v: f64) -> Result<Content, E>646 fn serialize_f64(self, v: f64) -> Result<Content, E> {
647 Ok(Content::F64(v))
648 }
649
serialize_char(self, v: char) -> Result<Content, E>650 fn serialize_char(self, v: char) -> Result<Content, E> {
651 Ok(Content::Char(v))
652 }
653
serialize_str(self, value: &str) -> Result<Content, E>654 fn serialize_str(self, value: &str) -> Result<Content, E> {
655 Ok(Content::String(value.to_owned()))
656 }
657
serialize_bytes(self, value: &[u8]) -> Result<Content, E>658 fn serialize_bytes(self, value: &[u8]) -> Result<Content, E> {
659 Ok(Content::Bytes(value.to_owned()))
660 }
661
serialize_none(self) -> Result<Content, E>662 fn serialize_none(self) -> Result<Content, E> {
663 Ok(Content::None)
664 }
665
serialize_some<T: ?Sized>(self, value: &T) -> Result<Content, E> where T: Serialize,666 fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Content, E>
667 where
668 T: Serialize,
669 {
670 Ok(Content::Some(Box::new(try!(value.serialize(self)))))
671 }
672
serialize_unit(self) -> Result<Content, E>673 fn serialize_unit(self) -> Result<Content, E> {
674 Ok(Content::Unit)
675 }
676
serialize_unit_struct(self, name: &'static str) -> Result<Content, E>677 fn serialize_unit_struct(self, name: &'static str) -> Result<Content, E> {
678 Ok(Content::UnitStruct(name))
679 }
680
serialize_unit_variant( self, name: &'static str, variant_index: u32, variant: &'static str, ) -> Result<Content, E>681 fn serialize_unit_variant(
682 self,
683 name: &'static str,
684 variant_index: u32,
685 variant: &'static str,
686 ) -> Result<Content, E> {
687 Ok(Content::UnitVariant(name, variant_index, variant))
688 }
689
serialize_newtype_struct<T: ?Sized>( self, name: &'static str, value: &T, ) -> Result<Content, E> where T: Serialize,690 fn serialize_newtype_struct<T: ?Sized>(
691 self,
692 name: &'static str,
693 value: &T,
694 ) -> Result<Content, E>
695 where
696 T: Serialize,
697 {
698 Ok(Content::NewtypeStruct(
699 name,
700 Box::new(try!(value.serialize(self))),
701 ))
702 }
703
serialize_newtype_variant<T: ?Sized>( self, name: &'static str, variant_index: u32, variant: &'static str, value: &T, ) -> Result<Content, E> where T: Serialize,704 fn serialize_newtype_variant<T: ?Sized>(
705 self,
706 name: &'static str,
707 variant_index: u32,
708 variant: &'static str,
709 value: &T,
710 ) -> Result<Content, E>
711 where
712 T: Serialize,
713 {
714 Ok(Content::NewtypeVariant(
715 name,
716 variant_index,
717 variant,
718 Box::new(try!(value.serialize(self))),
719 ))
720 }
721
serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, E>722 fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, E> {
723 Ok(SerializeSeq {
724 elements: Vec::with_capacity(len.unwrap_or(0)),
725 error: PhantomData,
726 })
727 }
728
serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, E>729 fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, E> {
730 Ok(SerializeTuple {
731 elements: Vec::with_capacity(len),
732 error: PhantomData,
733 })
734 }
735
serialize_tuple_struct( self, name: &'static str, len: usize, ) -> Result<Self::SerializeTupleStruct, E>736 fn serialize_tuple_struct(
737 self,
738 name: &'static str,
739 len: usize,
740 ) -> Result<Self::SerializeTupleStruct, E> {
741 Ok(SerializeTupleStruct {
742 name: name,
743 fields: Vec::with_capacity(len),
744 error: PhantomData,
745 })
746 }
747
serialize_tuple_variant( self, name: &'static str, variant_index: u32, variant: &'static str, len: usize, ) -> Result<Self::SerializeTupleVariant, E>748 fn serialize_tuple_variant(
749 self,
750 name: &'static str,
751 variant_index: u32,
752 variant: &'static str,
753 len: usize,
754 ) -> Result<Self::SerializeTupleVariant, E> {
755 Ok(SerializeTupleVariant {
756 name: name,
757 variant_index: variant_index,
758 variant: variant,
759 fields: Vec::with_capacity(len),
760 error: PhantomData,
761 })
762 }
763
serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, E>764 fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, E> {
765 Ok(SerializeMap {
766 entries: Vec::with_capacity(len.unwrap_or(0)),
767 key: None,
768 error: PhantomData,
769 })
770 }
771
serialize_struct( self, name: &'static str, len: usize, ) -> Result<Self::SerializeStruct, E>772 fn serialize_struct(
773 self,
774 name: &'static str,
775 len: usize,
776 ) -> Result<Self::SerializeStruct, E> {
777 Ok(SerializeStruct {
778 name: name,
779 fields: Vec::with_capacity(len),
780 error: PhantomData,
781 })
782 }
783
serialize_struct_variant( self, name: &'static str, variant_index: u32, variant: &'static str, len: usize, ) -> Result<Self::SerializeStructVariant, E>784 fn serialize_struct_variant(
785 self,
786 name: &'static str,
787 variant_index: u32,
788 variant: &'static str,
789 len: usize,
790 ) -> Result<Self::SerializeStructVariant, E> {
791 Ok(SerializeStructVariant {
792 name: name,
793 variant_index: variant_index,
794 variant: variant,
795 fields: Vec::with_capacity(len),
796 error: PhantomData,
797 })
798 }
799 }
800
801 pub struct SerializeSeq<E> {
802 elements: Vec<Content>,
803 error: PhantomData<E>,
804 }
805
806 impl<E> ser::SerializeSeq for SerializeSeq<E>
807 where
808 E: ser::Error,
809 {
810 type Ok = Content;
811 type Error = E;
812
serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), E> where T: Serialize,813 fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), E>
814 where
815 T: Serialize,
816 {
817 let value = try!(value.serialize(ContentSerializer::<E>::new()));
818 self.elements.push(value);
819 Ok(())
820 }
821
end(self) -> Result<Content, E>822 fn end(self) -> Result<Content, E> {
823 Ok(Content::Seq(self.elements))
824 }
825 }
826
827 pub struct SerializeTuple<E> {
828 elements: Vec<Content>,
829 error: PhantomData<E>,
830 }
831
832 impl<E> ser::SerializeTuple for SerializeTuple<E>
833 where
834 E: ser::Error,
835 {
836 type Ok = Content;
837 type Error = E;
838
serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), E> where T: Serialize,839 fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), E>
840 where
841 T: Serialize,
842 {
843 let value = try!(value.serialize(ContentSerializer::<E>::new()));
844 self.elements.push(value);
845 Ok(())
846 }
847
end(self) -> Result<Content, E>848 fn end(self) -> Result<Content, E> {
849 Ok(Content::Tuple(self.elements))
850 }
851 }
852
853 pub struct SerializeTupleStruct<E> {
854 name: &'static str,
855 fields: Vec<Content>,
856 error: PhantomData<E>,
857 }
858
859 impl<E> ser::SerializeTupleStruct for SerializeTupleStruct<E>
860 where
861 E: ser::Error,
862 {
863 type Ok = Content;
864 type Error = E;
865
serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), E> where T: Serialize,866 fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), E>
867 where
868 T: Serialize,
869 {
870 let value = try!(value.serialize(ContentSerializer::<E>::new()));
871 self.fields.push(value);
872 Ok(())
873 }
874
end(self) -> Result<Content, E>875 fn end(self) -> Result<Content, E> {
876 Ok(Content::TupleStruct(self.name, self.fields))
877 }
878 }
879
880 pub struct SerializeTupleVariant<E> {
881 name: &'static str,
882 variant_index: u32,
883 variant: &'static str,
884 fields: Vec<Content>,
885 error: PhantomData<E>,
886 }
887
888 impl<E> ser::SerializeTupleVariant for SerializeTupleVariant<E>
889 where
890 E: ser::Error,
891 {
892 type Ok = Content;
893 type Error = E;
894
serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), E> where T: Serialize,895 fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), E>
896 where
897 T: Serialize,
898 {
899 let value = try!(value.serialize(ContentSerializer::<E>::new()));
900 self.fields.push(value);
901 Ok(())
902 }
903
end(self) -> Result<Content, E>904 fn end(self) -> Result<Content, E> {
905 Ok(Content::TupleVariant(
906 self.name,
907 self.variant_index,
908 self.variant,
909 self.fields,
910 ))
911 }
912 }
913
914 pub struct SerializeMap<E> {
915 entries: Vec<(Content, Content)>,
916 key: Option<Content>,
917 error: PhantomData<E>,
918 }
919
920 impl<E> ser::SerializeMap for SerializeMap<E>
921 where
922 E: ser::Error,
923 {
924 type Ok = Content;
925 type Error = E;
926
serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), E> where T: Serialize,927 fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), E>
928 where
929 T: Serialize,
930 {
931 let key = try!(key.serialize(ContentSerializer::<E>::new()));
932 self.key = Some(key);
933 Ok(())
934 }
935
serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), E> where T: Serialize,936 fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), E>
937 where
938 T: Serialize,
939 {
940 let key = self
941 .key
942 .take()
943 .expect("serialize_value called before serialize_key");
944 let value = try!(value.serialize(ContentSerializer::<E>::new()));
945 self.entries.push((key, value));
946 Ok(())
947 }
948
end(self) -> Result<Content, E>949 fn end(self) -> Result<Content, E> {
950 Ok(Content::Map(self.entries))
951 }
952
serialize_entry<K: ?Sized, V: ?Sized>(&mut self, key: &K, value: &V) -> Result<(), E> where K: Serialize, V: Serialize,953 fn serialize_entry<K: ?Sized, V: ?Sized>(&mut self, key: &K, value: &V) -> Result<(), E>
954 where
955 K: Serialize,
956 V: Serialize,
957 {
958 let key = try!(key.serialize(ContentSerializer::<E>::new()));
959 let value = try!(value.serialize(ContentSerializer::<E>::new()));
960 self.entries.push((key, value));
961 Ok(())
962 }
963 }
964
965 pub struct SerializeStruct<E> {
966 name: &'static str,
967 fields: Vec<(&'static str, Content)>,
968 error: PhantomData<E>,
969 }
970
971 impl<E> ser::SerializeStruct for SerializeStruct<E>
972 where
973 E: ser::Error,
974 {
975 type Ok = Content;
976 type Error = E;
977
serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), E> where T: Serialize,978 fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), E>
979 where
980 T: Serialize,
981 {
982 let value = try!(value.serialize(ContentSerializer::<E>::new()));
983 self.fields.push((key, value));
984 Ok(())
985 }
986
end(self) -> Result<Content, E>987 fn end(self) -> Result<Content, E> {
988 Ok(Content::Struct(self.name, self.fields))
989 }
990 }
991
992 pub struct SerializeStructVariant<E> {
993 name: &'static str,
994 variant_index: u32,
995 variant: &'static str,
996 fields: Vec<(&'static str, Content)>,
997 error: PhantomData<E>,
998 }
999
1000 impl<E> ser::SerializeStructVariant for SerializeStructVariant<E>
1001 where
1002 E: ser::Error,
1003 {
1004 type Ok = Content;
1005 type Error = E;
1006
serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), E> where T: Serialize,1007 fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<(), E>
1008 where
1009 T: Serialize,
1010 {
1011 let value = try!(value.serialize(ContentSerializer::<E>::new()));
1012 self.fields.push((key, value));
1013 Ok(())
1014 }
1015
end(self) -> Result<Content, E>1016 fn end(self) -> Result<Content, E> {
1017 Ok(Content::StructVariant(
1018 self.name,
1019 self.variant_index,
1020 self.variant,
1021 self.fields,
1022 ))
1023 }
1024 }
1025 }
1026
1027 #[cfg(any(feature = "std", feature = "alloc"))]
1028 pub struct FlatMapSerializer<'a, M: 'a>(pub &'a mut M);
1029
1030 #[cfg(any(feature = "std", feature = "alloc"))]
1031 impl<'a, M> FlatMapSerializer<'a, M>
1032 where
1033 M: SerializeMap + 'a,
1034 {
bad_type(what: Unsupported) -> M::Error1035 fn bad_type(what: Unsupported) -> M::Error {
1036 ser::Error::custom(format_args!(
1037 "can only flatten structs and maps (got {})",
1038 what
1039 ))
1040 }
1041 }
1042
1043 #[cfg(any(feature = "std", feature = "alloc"))]
1044 impl<'a, M> Serializer for FlatMapSerializer<'a, M>
1045 where
1046 M: SerializeMap + 'a,
1047 {
1048 type Ok = ();
1049 type Error = M::Error;
1050
1051 type SerializeSeq = Impossible<Self::Ok, M::Error>;
1052 type SerializeTuple = Impossible<Self::Ok, M::Error>;
1053 type SerializeTupleStruct = Impossible<Self::Ok, M::Error>;
1054 type SerializeMap = FlatMapSerializeMap<'a, M>;
1055 type SerializeStruct = FlatMapSerializeStruct<'a, M>;
1056 type SerializeTupleVariant = Impossible<Self::Ok, M::Error>;
1057 type SerializeStructVariant = FlatMapSerializeStructVariantAsMapValue<'a, M>;
1058
serialize_bool(self, _: bool) -> Result<Self::Ok, Self::Error>1059 fn serialize_bool(self, _: bool) -> Result<Self::Ok, Self::Error> {
1060 Err(Self::bad_type(Unsupported::Boolean))
1061 }
1062
serialize_i8(self, _: i8) -> Result<Self::Ok, Self::Error>1063 fn serialize_i8(self, _: i8) -> Result<Self::Ok, Self::Error> {
1064 Err(Self::bad_type(Unsupported::Integer))
1065 }
1066
serialize_i16(self, _: i16) -> Result<Self::Ok, Self::Error>1067 fn serialize_i16(self, _: i16) -> Result<Self::Ok, Self::Error> {
1068 Err(Self::bad_type(Unsupported::Integer))
1069 }
1070
serialize_i32(self, _: i32) -> Result<Self::Ok, Self::Error>1071 fn serialize_i32(self, _: i32) -> Result<Self::Ok, Self::Error> {
1072 Err(Self::bad_type(Unsupported::Integer))
1073 }
1074
serialize_i64(self, _: i64) -> Result<Self::Ok, Self::Error>1075 fn serialize_i64(self, _: i64) -> Result<Self::Ok, Self::Error> {
1076 Err(Self::bad_type(Unsupported::Integer))
1077 }
1078
serialize_u8(self, _: u8) -> Result<Self::Ok, Self::Error>1079 fn serialize_u8(self, _: u8) -> Result<Self::Ok, Self::Error> {
1080 Err(Self::bad_type(Unsupported::Integer))
1081 }
1082
serialize_u16(self, _: u16) -> Result<Self::Ok, Self::Error>1083 fn serialize_u16(self, _: u16) -> Result<Self::Ok, Self::Error> {
1084 Err(Self::bad_type(Unsupported::Integer))
1085 }
1086
serialize_u32(self, _: u32) -> Result<Self::Ok, Self::Error>1087 fn serialize_u32(self, _: u32) -> Result<Self::Ok, Self::Error> {
1088 Err(Self::bad_type(Unsupported::Integer))
1089 }
1090
serialize_u64(self, _: u64) -> Result<Self::Ok, Self::Error>1091 fn serialize_u64(self, _: u64) -> Result<Self::Ok, Self::Error> {
1092 Err(Self::bad_type(Unsupported::Integer))
1093 }
1094
serialize_f32(self, _: f32) -> Result<Self::Ok, Self::Error>1095 fn serialize_f32(self, _: f32) -> Result<Self::Ok, Self::Error> {
1096 Err(Self::bad_type(Unsupported::Float))
1097 }
1098
serialize_f64(self, _: f64) -> Result<Self::Ok, Self::Error>1099 fn serialize_f64(self, _: f64) -> Result<Self::Ok, Self::Error> {
1100 Err(Self::bad_type(Unsupported::Float))
1101 }
1102
serialize_char(self, _: char) -> Result<Self::Ok, Self::Error>1103 fn serialize_char(self, _: char) -> Result<Self::Ok, Self::Error> {
1104 Err(Self::bad_type(Unsupported::Char))
1105 }
1106
serialize_str(self, _: &str) -> Result<Self::Ok, Self::Error>1107 fn serialize_str(self, _: &str) -> Result<Self::Ok, Self::Error> {
1108 Err(Self::bad_type(Unsupported::String))
1109 }
1110
serialize_bytes(self, _: &[u8]) -> Result<Self::Ok, Self::Error>1111 fn serialize_bytes(self, _: &[u8]) -> Result<Self::Ok, Self::Error> {
1112 Err(Self::bad_type(Unsupported::ByteArray))
1113 }
1114
serialize_none(self) -> Result<Self::Ok, Self::Error>1115 fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
1116 Ok(())
1117 }
1118
serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error> where T: Serialize,1119 fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
1120 where
1121 T: Serialize,
1122 {
1123 value.serialize(self)
1124 }
1125
serialize_unit(self) -> Result<Self::Ok, Self::Error>1126 fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
1127 Ok(())
1128 }
1129
serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error>1130 fn serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error> {
1131 Err(Self::bad_type(Unsupported::UnitStruct))
1132 }
1133
serialize_unit_variant( self, _: &'static str, _: u32, _: &'static str, ) -> Result<Self::Ok, Self::Error>1134 fn serialize_unit_variant(
1135 self,
1136 _: &'static str,
1137 _: u32,
1138 _: &'static str,
1139 ) -> Result<Self::Ok, Self::Error> {
1140 Err(Self::bad_type(Unsupported::Enum))
1141 }
1142
serialize_newtype_struct<T: ?Sized>( self, _: &'static str, value: &T, ) -> Result<Self::Ok, Self::Error> where T: Serialize,1143 fn serialize_newtype_struct<T: ?Sized>(
1144 self,
1145 _: &'static str,
1146 value: &T,
1147 ) -> Result<Self::Ok, Self::Error>
1148 where
1149 T: Serialize,
1150 {
1151 value.serialize(self)
1152 }
1153
serialize_newtype_variant<T: ?Sized>( self, _: &'static str, _: u32, variant: &'static str, value: &T, ) -> Result<Self::Ok, Self::Error> where T: Serialize,1154 fn serialize_newtype_variant<T: ?Sized>(
1155 self,
1156 _: &'static str,
1157 _: u32,
1158 variant: &'static str,
1159 value: &T,
1160 ) -> Result<Self::Ok, Self::Error>
1161 where
1162 T: Serialize,
1163 {
1164 try!(self.0.serialize_key(variant));
1165 self.0.serialize_value(value)
1166 }
1167
serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq, Self::Error>1168 fn serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
1169 Err(Self::bad_type(Unsupported::Sequence))
1170 }
1171
serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, Self::Error>1172 fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, Self::Error> {
1173 Err(Self::bad_type(Unsupported::Tuple))
1174 }
1175
serialize_tuple_struct( self, _: &'static str, _: usize, ) -> Result<Self::SerializeTupleStruct, Self::Error>1176 fn serialize_tuple_struct(
1177 self,
1178 _: &'static str,
1179 _: usize,
1180 ) -> Result<Self::SerializeTupleStruct, Self::Error> {
1181 Err(Self::bad_type(Unsupported::TupleStruct))
1182 }
1183
serialize_tuple_variant( self, _: &'static str, _: u32, _: &'static str, _: usize, ) -> Result<Self::SerializeTupleVariant, Self::Error>1184 fn serialize_tuple_variant(
1185 self,
1186 _: &'static str,
1187 _: u32,
1188 _: &'static str,
1189 _: usize,
1190 ) -> Result<Self::SerializeTupleVariant, Self::Error> {
1191 Err(Self::bad_type(Unsupported::Enum))
1192 }
1193
serialize_map(self, _: Option<usize>) -> Result<Self::SerializeMap, Self::Error>1194 fn serialize_map(self, _: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
1195 Ok(FlatMapSerializeMap(self.0))
1196 }
1197
serialize_struct( self, _: &'static str, _: usize, ) -> Result<Self::SerializeStruct, Self::Error>1198 fn serialize_struct(
1199 self,
1200 _: &'static str,
1201 _: usize,
1202 ) -> Result<Self::SerializeStruct, Self::Error> {
1203 Ok(FlatMapSerializeStruct(self.0))
1204 }
1205
serialize_struct_variant( self, _: &'static str, _: u32, inner_variant: &'static str, _: usize, ) -> Result<Self::SerializeStructVariant, Self::Error>1206 fn serialize_struct_variant(
1207 self,
1208 _: &'static str,
1209 _: u32,
1210 inner_variant: &'static str,
1211 _: usize,
1212 ) -> Result<Self::SerializeStructVariant, Self::Error> {
1213 try!(self.0.serialize_key(inner_variant));
1214 Ok(FlatMapSerializeStructVariantAsMapValue::new(
1215 self.0,
1216 inner_variant,
1217 ))
1218 }
1219 }
1220
1221 #[cfg(any(feature = "std", feature = "alloc"))]
1222 pub struct FlatMapSerializeMap<'a, M: 'a>(&'a mut M);
1223
1224 #[cfg(any(feature = "std", feature = "alloc"))]
1225 impl<'a, M> ser::SerializeMap for FlatMapSerializeMap<'a, M>
1226 where
1227 M: SerializeMap + 'a,
1228 {
1229 type Ok = ();
1230 type Error = M::Error;
1231
serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error> where T: Serialize,1232 fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error>
1233 where
1234 T: Serialize,
1235 {
1236 self.0.serialize_key(key)
1237 }
1238
serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error> where T: Serialize,1239 fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
1240 where
1241 T: Serialize,
1242 {
1243 self.0.serialize_value(value)
1244 }
1245
serialize_entry<K: ?Sized, V: ?Sized>( &mut self, key: &K, value: &V, ) -> Result<(), Self::Error> where K: Serialize, V: Serialize,1246 fn serialize_entry<K: ?Sized, V: ?Sized>(
1247 &mut self,
1248 key: &K,
1249 value: &V,
1250 ) -> Result<(), Self::Error>
1251 where
1252 K: Serialize,
1253 V: Serialize,
1254 {
1255 self.0.serialize_entry(key, value)
1256 }
1257
end(self) -> Result<(), Self::Error>1258 fn end(self) -> Result<(), Self::Error> {
1259 Ok(())
1260 }
1261 }
1262
1263 #[cfg(any(feature = "std", feature = "alloc"))]
1264 pub struct FlatMapSerializeStruct<'a, M: 'a>(&'a mut M);
1265
1266 #[cfg(any(feature = "std", feature = "alloc"))]
1267 impl<'a, M> ser::SerializeStruct for FlatMapSerializeStruct<'a, M>
1268 where
1269 M: SerializeMap + 'a,
1270 {
1271 type Ok = ();
1272 type Error = M::Error;
1273
serialize_field<T: ?Sized>( &mut self, key: &'static str, value: &T, ) -> Result<(), Self::Error> where T: Serialize,1274 fn serialize_field<T: ?Sized>(
1275 &mut self,
1276 key: &'static str,
1277 value: &T,
1278 ) -> Result<(), Self::Error>
1279 where
1280 T: Serialize,
1281 {
1282 self.0.serialize_entry(key, value)
1283 }
1284
end(self) -> Result<(), Self::Error>1285 fn end(self) -> Result<(), Self::Error> {
1286 Ok(())
1287 }
1288 }
1289
1290 #[cfg(any(feature = "std", feature = "alloc"))]
1291 pub struct FlatMapSerializeStructVariantAsMapValue<'a, M: 'a> {
1292 map: &'a mut M,
1293 name: &'static str,
1294 fields: Vec<(&'static str, Content)>,
1295 }
1296
1297 #[cfg(any(feature = "std", feature = "alloc"))]
1298 impl<'a, M> FlatMapSerializeStructVariantAsMapValue<'a, M>
1299 where
1300 M: SerializeMap + 'a,
1301 {
new(map: &'a mut M, name: &'static str) -> FlatMapSerializeStructVariantAsMapValue<'a, M>1302 fn new(map: &'a mut M, name: &'static str) -> FlatMapSerializeStructVariantAsMapValue<'a, M> {
1303 FlatMapSerializeStructVariantAsMapValue {
1304 map: map,
1305 name: name,
1306 fields: Vec::new(),
1307 }
1308 }
1309 }
1310
1311 #[cfg(any(feature = "std", feature = "alloc"))]
1312 impl<'a, M> ser::SerializeStructVariant for FlatMapSerializeStructVariantAsMapValue<'a, M>
1313 where
1314 M: SerializeMap + 'a,
1315 {
1316 type Ok = ();
1317 type Error = M::Error;
1318
serialize_field<T: ?Sized>( &mut self, key: &'static str, value: &T, ) -> Result<(), Self::Error> where T: Serialize,1319 fn serialize_field<T: ?Sized>(
1320 &mut self,
1321 key: &'static str,
1322 value: &T,
1323 ) -> Result<(), Self::Error>
1324 where
1325 T: Serialize,
1326 {
1327 let value = try!(value.serialize(ContentSerializer::<M::Error>::new()));
1328 self.fields.push((key, value));
1329 Ok(())
1330 }
1331
end(self) -> Result<(), Self::Error>1332 fn end(self) -> Result<(), Self::Error> {
1333 try!(self
1334 .map
1335 .serialize_value(&Content::Struct(self.name, self.fields)));
1336 Ok(())
1337 }
1338 }
1339