Coverage Report

Created: 2025-07-11 06:21

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