1use std::error;
4use std::fmt::{self, Display};
5use std::io::Write;
6
7use serde;
8use serde::ser::{
9 SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant, SerializeTuple,
10 SerializeTupleStruct, SerializeTupleVariant,
11};
12use serde::Serialize;
13
14use rmp::encode::ValueWriteError;
15use rmp::{encode, Marker};
16
17use crate::config::{
18 BinaryConfig, DefaultConfig, HumanReadableConfig, SerializerConfig, StructMapConfig,
19 StructTupleConfig
20};
21use crate::MSGPACK_EXT_STRUCT_NAME;
22
23#[derive(Debug)]
26pub enum Error {
27 InvalidValueWrite(ValueWriteError),
29 UnknownLength,
32 InvalidDataModel(&'static str),
34 DepthLimitExceeded,
36 Syntax(String),
38}
39
40impl error::Error for Error {
41 #[cold]
42 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
43 match *self {
44 Error::InvalidValueWrite(ref err) => Some(err),
45 Error::UnknownLength => None,
46 Error::InvalidDataModel(_) => None,
47 Error::DepthLimitExceeded => None,
48 Error::Syntax(..) => None,
49 }
50 }
51}
52
53impl Display for Error {
54 #[cold]
55 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
56 match *self {
57 Error::InvalidValueWrite(ref err) => write!(f, "invalid value write: {}", err),
58 Error::UnknownLength => {
59 f.write_str("attempt to serialize struct, sequence or map with unknown length")
60 }
61 Error::InvalidDataModel(r) => write!(f, "serialize data model is invalid: {}", r),
62 Error::DepthLimitExceeded => f.write_str("depth limit exceeded"),
63 Error::Syntax(ref msg) => f.write_str(msg),
64 }
65 }
66}
67
68impl From<ValueWriteError> for Error {
69 #[cold]
70 fn from(err: ValueWriteError) -> Error {
71 Error::InvalidValueWrite(err)
72 }
73}
74
75impl serde::ser::Error for Error {
76 #[cold]
78 fn custom<T: Display>(msg: T) -> Error {
79 Error::Syntax(msg.to_string())
80 }
81}
82
83pub trait UnderlyingWrite {
85 type Write: Write;
87
88 fn get_ref(&self) -> &Self::Write;
90
91 fn get_mut(&mut self) -> &mut Self::Write;
95
96 fn into_inner(self) -> Self::Write;
98}
99
100#[derive(Debug)]
115pub struct Serializer<W, C = DefaultConfig> {
116 wr: W,
117 config: C,
118 depth: usize,
119}
120
121impl<W: Write, C> Serializer<W, C> {
122 #[inline(always)]
124 pub fn get_ref(&self) -> &W {
125 &self.wr
126 }
127
128 #[inline(always)]
132 pub fn get_mut(&mut self) -> &mut W {
133 &mut self.wr
134 }
135
136 #[inline(always)]
138 pub fn into_inner(self) -> W {
139 self.wr
140 }
141
142 #[doc(hidden)]
146 #[inline]
147 pub fn unstable_set_max_depth(&mut self, depth: usize) {
148 self.depth = depth;
149 }
150}
151
152impl<W: Write> Serializer<W, DefaultConfig> {
153 #[inline]
161 pub fn new(wr: W) -> Self {
162 Serializer {
163 wr,
164 depth: 1024,
165 config: DefaultConfig,
166 }
167 }
168}
169
170impl<'a, W: Write + 'a, C> Serializer<W, C> {
171 #[inline]
172 fn compound(&'a mut self) -> Result<Compound<'a, W, C>, Error> {
173 let c = Compound { se: self };
174 Ok(c)
175 }
176}
177
178impl<'a, W: Write + 'a, C: SerializerConfig> Serializer<W, C> {
179 #[inline]
180 fn maybe_unknown_len_compound<F>(&'a mut self, len: Option<usize>, f: F) -> Result<MaybeUnknownLengthCompound<'a, W, C>, Error>
181 where F: Fn(&mut W, u32) -> Result<Marker, ValueWriteError>
182 {
183 Ok(MaybeUnknownLengthCompound {
184 compound: match len {
185 Some(len) => {
186 f(&mut self.wr, len as u32)?;
187 None
188 }
189 None => Some(UnknownLengthCompound::from(&*self)),
190 },
191 se: self,
192 })
193 }
194}
195
196impl<W: Write, C> Serializer<W, C> {
197 #[inline]
202 pub fn with_struct_map(self) -> Serializer<W, StructMapConfig<C>> {
203 let Serializer { wr, depth, config } = self;
204 Serializer {
205 wr,
206 depth,
207 config: StructMapConfig::new(config),
208 }
209 }
210
211 #[inline]
217 pub fn with_struct_tuple(self) -> Serializer<W, StructTupleConfig<C>> {
218 let Serializer { wr, depth, config } = self;
219 Serializer {
220 wr,
221 depth,
222 config: StructTupleConfig::new(config),
223 }
224 }
225
226 #[inline]
234 pub fn with_human_readable(self) -> Serializer<W, HumanReadableConfig<C>> {
235 let Serializer { wr, depth, config } = self;
236 Serializer {
237 wr,
238 depth,
239 config: HumanReadableConfig::new(config),
240 }
241 }
242
243 #[inline]
249 pub fn with_binary(self) -> Serializer<W, BinaryConfig<C>> {
250 let Serializer { wr, depth, config } = self;
251 Serializer {
252 wr,
253 depth,
254 config: BinaryConfig::new(config),
255 }
256 }
257}
258
259impl<W: Write, C> UnderlyingWrite for Serializer<W, C> {
260 type Write = W;
261
262 #[inline(always)]
263 fn get_ref(&self) -> &Self::Write {
264 &self.wr
265 }
266
267 #[inline(always)]
268 fn get_mut(&mut self) -> &mut Self::Write {
269 &mut self.wr
270 }
271
272 #[inline(always)]
273 fn into_inner(self) -> Self::Write {
274 self.wr
275 }
276}
277
278#[derive(Debug)]
280pub struct Compound<'a, W: 'a, C: 'a> {
281 se: &'a mut Serializer<W, C>,
282}
283
284#[derive(Debug)]
285#[allow(missing_docs)]
286pub struct ExtFieldSerializer<'a, W> {
287 wr: &'a mut W,
288 tag: Option<i8>,
289 finish: bool,
290}
291
292#[derive(Debug)]
294pub struct ExtSerializer<'a, W> {
295 fields_se: ExtFieldSerializer<'a, W>,
296 tuple_received: bool,
297}
298
299impl<'a, W: Write + 'a, C: SerializerConfig> SerializeSeq for Compound<'a, W, C> {
300 type Ok = ();
301 type Error = Error;
302
303 #[inline]
304 fn serialize_element<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), Self::Error> {
305 value.serialize(&mut *self.se)
306 }
307
308 #[inline(always)]
309 fn end(self) -> Result<Self::Ok, Self::Error> {
310 Ok(())
311 }
312}
313
314impl<'a, W: Write + 'a, C: SerializerConfig> SerializeTuple for Compound<'a, W, C> {
315 type Ok = ();
316 type Error = Error;
317
318 #[inline]
319 fn serialize_element<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), Self::Error> {
320 value.serialize(&mut *self.se)
321 }
322
323 #[inline(always)]
324 fn end(self) -> Result<Self::Ok, Self::Error> {
325 Ok(())
326 }
327}
328
329impl<'a, W: Write + 'a, C: SerializerConfig> SerializeTupleStruct for Compound<'a, W, C> {
330 type Ok = ();
331 type Error = Error;
332
333 #[inline]
334 fn serialize_field<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), Self::Error> {
335 value.serialize(&mut *self.se)
336 }
337
338 #[inline(always)]
339 fn end(self) -> Result<Self::Ok, Self::Error> {
340 Ok(())
341 }
342}
343
344impl<'a, W: Write + 'a, C: SerializerConfig> SerializeStruct for Compound<'a, W, C> {
345 type Ok = ();
346 type Error = Error;
347
348 #[inline]
349 fn serialize_field<T: ?Sized + Serialize>(&mut self, key: &'static str, value: &T) ->
350 Result<(), Self::Error>
351 {
352 C::write_struct_field(&mut *self.se, key, value)
353 }
354
355 #[inline(always)]
356 fn end(self) -> Result<Self::Ok, Self::Error> {
357 Ok(())
358 }
359}
360
361impl<'a, W: Write + 'a, C: SerializerConfig> SerializeTupleVariant for Compound<'a, W, C> {
362 type Ok = ();
363 type Error = Error;
364
365 #[inline]
366 fn serialize_field<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), Self::Error> {
367 value.serialize(&mut *self.se)
368 }
369
370 #[inline(always)]
371 fn end(self) -> Result<Self::Ok, Self::Error> {
372 Ok(())
373 }
374}
375
376impl<'a, W: Write + 'a, C: SerializerConfig> SerializeStructVariant for Compound<'a, W, C> {
377 type Ok = ();
378 type Error = Error;
379
380 fn serialize_field<T: ?Sized + Serialize>(&mut self, key: &'static str, value: &T) ->
381 Result<(), Self::Error>
382 {
383 C::write_struct_field(&mut *self.se, key, value)
384 }
385
386 #[inline(always)]
387 fn end(self) -> Result<Self::Ok, Self::Error> {
388 Ok(())
389 }
390}
391
392#[derive(Debug)]
395struct UnknownLengthCompound<C> {
396 se: Serializer<Vec<u8>, C>,
397 elem_count: u32,
398}
399impl<W, C: SerializerConfig> From<&Serializer<W, C>> for UnknownLengthCompound<C> {
400 fn from(se: &Serializer<W, C>) -> Self {
401 Self {
402 se: Serializer { wr: Vec::with_capacity(128), config: se.config, depth: se.depth },
403 elem_count: 0
404 }
405 }
406}
407
408#[derive(Debug)]
424pub struct MaybeUnknownLengthCompound<'a, W: 'a, C: 'a> {
425 se: &'a mut Serializer<W, C>,
426 compound: Option<UnknownLengthCompound<C>>,
427}
428
429impl<'a, W: Write + 'a, C: SerializerConfig> SerializeSeq for MaybeUnknownLengthCompound<'a, W, C> {
430 type Ok = ();
431 type Error = Error;
432
433 fn serialize_element<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), Self::Error> {
434 match self.compound.as_mut() {
435 None => value.serialize(&mut *self.se),
436 Some(buf) => {
437 value.serialize(&mut buf.se)?;
438 buf.elem_count += 1;
439 Ok(())
440 }
441 }
442 }
443
444 fn end(self) -> Result<Self::Ok, Self::Error> {
445 if let Some(compound) = self.compound {
446 encode::write_array_len(&mut self.se.wr, compound.elem_count)?;
447 self.se.wr.write_all(&compound.se.into_inner())
448 .map_err(ValueWriteError::InvalidDataWrite)?;
449 }
450 Ok(())
451 }
452}
453
454impl<'a, W: Write + 'a, C: SerializerConfig> SerializeMap for MaybeUnknownLengthCompound<'a, W, C> {
455 type Ok = ();
456 type Error = Error;
457
458 fn serialize_key<T: ?Sized + Serialize>(&mut self, key: &T) -> Result<(), Self::Error> {
459 <Self as SerializeSeq>::serialize_element(self, key)
460 }
461
462 fn serialize_value<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), Self::Error> {
463 <Self as SerializeSeq>::serialize_element(self, value)
464 }
465
466 fn end(self) -> Result<Self::Ok, Self::Error> {
467 if let Some(compound) = self.compound {
468 encode::write_map_len(&mut self.se.wr, compound.elem_count / 2)?;
469 self.se.wr.write_all(&compound.se.into_inner())
470 .map_err(ValueWriteError::InvalidDataWrite)?;
471 }
472 Ok(())
473 }
474}
475
476impl<'a, W, C> serde::Serializer for &'a mut Serializer<W, C>
477where
478 W: Write,
479 C: SerializerConfig,
480{
481 type Ok = ();
482 type Error = Error;
483
484 type SerializeSeq = MaybeUnknownLengthCompound<'a, W, C>;
485 type SerializeTuple = Compound<'a, W, C>;
486 type SerializeTupleStruct = Compound<'a, W, C>;
487 type SerializeTupleVariant = Compound<'a, W, C>;
488 type SerializeMap = MaybeUnknownLengthCompound<'a, W, C>;
489 type SerializeStruct = Compound<'a, W, C>;
490 type SerializeStructVariant = Compound<'a, W, C>;
491
492 fn is_human_readable(&self) -> bool {
493 C::is_human_readable()
494 }
495
496 fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> {
497 encode::write_bool(&mut self.wr, v)
498 .map_err(|err| Error::InvalidValueWrite(ValueWriteError::InvalidMarkerWrite(err)))
499 }
500
501 fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> {
502 self.serialize_i64(v as i64)
503 }
504
505 fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> {
506 self.serialize_i64(v as i64)
507 }
508
509 fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> {
510 self.serialize_i64(v as i64)
511 }
512
513 fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> {
514 encode::write_sint(&mut self.wr, v)?;
515 Ok(())
516 }
517
518 fn serialize_i128(self, v: i128) -> Result<Self::Ok, Self::Error> {
519 self.serialize_bytes(&v.to_be_bytes())
520 }
521
522 fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> {
523 self.serialize_u64(v as u64)
524 }
525
526 fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> {
527 self.serialize_u64(v as u64)
528 }
529
530 fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> {
531 self.serialize_u64(v as u64)
532 }
533
534 fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> {
535 encode::write_uint(&mut self.wr, v)?;
536 Ok(())
537 }
538
539 fn serialize_u128(self, v: u128) -> Result<Self::Ok, Self::Error> {
540 self.serialize_bytes(&v.to_be_bytes())
541 }
542
543 fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> {
544 encode::write_f32(&mut self.wr, v)?;
545 Ok(())
546 }
547
548 fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error> {
549 encode::write_f64(&mut self.wr, v)?;
550 Ok(())
551 }
552
553 fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> {
554 let mut buf = [0; 4];
556 self.serialize_str(v.encode_utf8(&mut buf))
557 }
558
559 fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> {
560 encode::write_str(&mut self.wr, v)?;
561 Ok(())
562 }
563
564 fn serialize_bytes(self, value: &[u8]) -> Result<Self::Ok, Self::Error> {
565 encode::write_bin_len(&mut self.wr, value.len() as u32)?;
566 self.wr
567 .write_all(value)
568 .map_err(|err| Error::InvalidValueWrite(ValueWriteError::InvalidDataWrite(err)))
569 }
570
571 fn serialize_none(self) -> Result<(), Self::Error> {
572 self.serialize_unit()
573 }
574
575 fn serialize_some<T: ?Sized + serde::Serialize>(self, v: &T) -> Result<(), Self::Error> {
576 v.serialize(self)
577 }
578
579 fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
580 encode::write_nil(&mut self.wr)
581 .map_err(|err| Error::InvalidValueWrite(ValueWriteError::InvalidMarkerWrite(err)))
582 }
583
584 fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> {
585 encode::write_array_len(&mut self.wr, 0)?;
586 Ok(())
587 }
588
589 fn serialize_unit_variant(self, _name: &str, idx: u32, variant: &'static str) ->
590 Result<Self::Ok, Self::Error>
591 {
592 C::write_variant_ident(self, idx, variant)
593 }
594
595 fn serialize_newtype_struct<T: ?Sized + serde::Serialize>(self, name: &'static str, value: &T) -> Result<(), Self::Error> {
596 if name == MSGPACK_EXT_STRUCT_NAME {
597 let mut ext_se = ExtSerializer::new(self);
598 value.serialize(&mut ext_se)?;
599
600 return ext_se.end();
601 }
602
603 value.serialize(self)
605 }
606
607 fn serialize_newtype_variant<T: ?Sized + serde::Serialize>(self, _name: &'static str, idx: u32, variant: &'static str, value: &T) -> Result<Self::Ok, Self::Error> {
608 encode::write_map_len(&mut self.wr, 1)?;
610 C::write_variant_ident(self, idx, variant)?;
611 value.serialize(self)
612 }
613
614 fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Error> {
615 self.maybe_unknown_len_compound(len, |wr, len| encode::write_array_len(wr, len))
616 }
617
618 fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
620 encode::write_array_len(&mut self.wr, len as u32)?;
621
622 self.compound()
623 }
624
625 fn serialize_tuple_struct(self, _name: &'static str, len: usize) ->
626 Result<Self::SerializeTupleStruct, Self::Error>
627 {
628 encode::write_array_len(&mut self.wr, len as u32)?;
629
630 self.compound()
631 }
632
633 fn serialize_tuple_variant(self, _name: &'static str, idx: u32, variant: &'static str, len: usize) ->
634 Result<Self::SerializeTupleVariant, Error>
635 {
636 encode::write_map_len(&mut self.wr, 1)?;
638 C::write_variant_ident(self, idx, variant)?;
639 self.serialize_tuple(len)
640 }
641
642 fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Error> {
643 self.maybe_unknown_len_compound(len, |wr, len| encode::write_map_len(wr, len))
644 }
645
646 fn serialize_struct(self, _name: &'static str, len: usize) ->
647 Result<Self::SerializeStruct, Self::Error>
648 {
649 C::write_struct_len(self, len)?;
650 self.compound()
651 }
652
653 fn serialize_struct_variant(self, name: &'static str, id: u32, variant: &'static str, len: usize) ->
654 Result<Self::SerializeStructVariant, Error>
655 {
656 encode::write_map_len(&mut self.wr, 1)?;
658 C::write_variant_ident(self, id, variant)?;
659 self.serialize_struct(name, len)
660 }
661}
662
663impl<'a, W: Write + 'a> serde::Serializer for &mut ExtFieldSerializer<'a, W> {
664 type Ok = ();
665 type Error = Error;
666
667 type SerializeSeq = serde::ser::Impossible<(), Error>;
668 type SerializeTuple = serde::ser::Impossible<(), Error>;
669 type SerializeTupleStruct = serde::ser::Impossible<(), Error>;
670 type SerializeTupleVariant = serde::ser::Impossible<(), Error>;
671 type SerializeMap = serde::ser::Impossible<(), Error>;
672 type SerializeStruct = serde::ser::Impossible<(), Error>;
673 type SerializeStructVariant = serde::ser::Impossible<(), Error>;
674
675 #[inline]
676 fn serialize_i8(self, value: i8) -> Result<Self::Ok, Self::Error> {
677 if self.tag.is_none() {
678 self.tag.replace(value);
679 Ok(())
680 } else {
681 Err(Error::InvalidDataModel("expected i8 and bytes, unexpected second i8"))
682 }
683 }
684
685 #[inline]
686 fn serialize_bytes(self, val: &[u8]) -> Result<Self::Ok, Self::Error> {
687 if let Some(tag) = self.tag.take() {
688 encode::write_ext_meta(self.wr, val.len() as u32, tag)?;
689 self.wr
690 .write_all(val)
691 .map_err(|err| Error::InvalidValueWrite(ValueWriteError::InvalidDataWrite(err)))?;
692
693 self.finish = true;
694
695 Ok(())
696 } else {
697 Err(Error::InvalidDataModel("expected i8 and bytes, received bytes first"))
698 }
699 }
700
701 #[inline]
702 fn serialize_bool(self, _val: bool) -> Result<Self::Ok, Self::Error> {
703 Err(Error::InvalidDataModel("expected i8 and bytes, bool unexpected"))
704 }
705
706 #[inline]
707 fn serialize_i16(self, _val: i16) -> Result<Self::Ok, Self::Error> {
708 Err(Error::InvalidDataModel("expected i8 and bytes, i16 unexpected"))
709 }
710
711 #[inline]
712 fn serialize_i32(self, _val: i32) -> Result<Self::Ok, Self::Error> {
713 Err(Error::InvalidDataModel("expected i8 and bytes, i32 unexpected"))
714 }
715
716 #[inline]
717 fn serialize_i64(self, _val: i64) -> Result<Self::Ok, Self::Error> {
718 Err(Error::InvalidDataModel("expected i8 and bytes, i64 unexpected"))
719 }
720
721 #[inline]
722 fn serialize_u8(self, _val: u8) -> Result<Self::Ok, Self::Error> {
723 Err(Error::InvalidDataModel("expected i8 and bytes, u8 unexpected"))
724 }
725
726 #[inline]
727 fn serialize_u16(self, _val: u16) -> Result<Self::Ok, Self::Error> {
728 Err(Error::InvalidDataModel("expected i8 and bytes, u16 unexpected"))
729 }
730
731 #[inline]
732 fn serialize_u32(self, _val: u32) -> Result<Self::Ok, Self::Error> {
733 Err(Error::InvalidDataModel("expected i8 and bytes, u32 unexpected"))
734 }
735
736 #[inline]
737 fn serialize_u64(self, _val: u64) -> Result<Self::Ok, Self::Error> {
738 Err(Error::InvalidDataModel("expected i8 and bytes, u64 unexpected"))
739 }
740
741 #[inline]
742 fn serialize_f32(self, _val: f32) -> Result<Self::Ok, Self::Error> {
743 Err(Error::InvalidDataModel("expected i8 and bytes, f32 unexpected"))
744 }
745
746 #[inline]
747 fn serialize_f64(self, _val: f64) -> Result<Self::Ok, Self::Error> {
748 Err(Error::InvalidDataModel("expected i8 and bytes, f64 unexpected"))
749 }
750
751 #[inline]
752 fn serialize_char(self, _val: char) -> Result<Self::Ok, Self::Error> {
753 Err(Error::InvalidDataModel("expected i8 and bytes, char unexpected"))
754 }
755
756 #[inline]
757 fn serialize_str(self, _val: &str) -> Result<Self::Ok, Self::Error> {
758 Err(Error::InvalidDataModel("expected i8 and bytes, str unexpected"))
759 }
760
761 #[inline]
762 fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
763 Err(Error::InvalidDataModel("expected i8 and bytes, unit unexpected"))
764 }
765
766 #[inline]
767 fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> {
768 Err(Error::InvalidDataModel("expected i8 and bytes, unit struct unexpected"))
769 }
770
771 #[inline]
772 fn serialize_unit_variant(self, _name: &'static str, _idx: u32, _variant: &'static str) -> Result<Self::Ok, Self::Error> {
773 Err(Error::InvalidDataModel("expected i8 and bytes, unit variant unexpected"))
774 }
775
776 #[inline]
777 fn serialize_newtype_struct<T: ?Sized>(self, _name: &'static str, _value: &T) -> Result<Self::Ok, Self::Error>
778 where T: Serialize
779 {
780 Err(Error::InvalidDataModel("expected i8 and bytes, newtype struct unexpected"))
781 }
782
783 fn serialize_newtype_variant<T: ?Sized>(self, _name: &'static str, _idx: u32, _variant: &'static str, _value: &T) -> Result<Self::Ok, Self::Error>
784 where T: Serialize
785 {
786 Err(Error::InvalidDataModel("expected i8 and bytes, newtype variant unexpected"))
787 }
788
789 #[inline]
790 fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
791 Err(Error::InvalidDataModel("expected i8 and bytes, none unexpected"))
792 }
793
794 #[inline]
795 fn serialize_some<T: ?Sized>(self, _value: &T) -> Result<Self::Ok, Self::Error>
796 where T: Serialize
797 {
798 Err(Error::InvalidDataModel("expected i8 and bytes, some unexpected"))
799 }
800
801 fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
802 Err(Error::InvalidDataModel("expected i8 and bytes, seq unexpected"))
803 }
804
805 fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Error> {
806 Err(Error::InvalidDataModel("expected i8 and bytes, tuple unexpected"))
807 }
808
809 fn serialize_tuple_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeTupleStruct, Error> {
810 Err(Error::InvalidDataModel("expected i8 and bytes, tuple struct unexpected"))
811 }
812
813 fn serialize_tuple_variant(self, _name: &'static str, _idx: u32, _variant: &'static str, _len: usize) -> Result<Self::SerializeTupleVariant, Error> {
814 Err(Error::InvalidDataModel("expected i8 and bytes, tuple variant unexpected"))
815 }
816
817 fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Error> {
818 Err(Error::InvalidDataModel("expected i8 and bytes, map unexpected"))
819 }
820
821 fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct, Error> {
822 Err(Error::InvalidDataModel("expected i8 and bytes, struct unexpected"))
823 }
824
825 fn serialize_struct_variant(self, _name: &'static str, _idx: u32, _variant: &'static str, _len: usize) -> Result<Self::SerializeStructVariant, Error> {
826 Err(Error::InvalidDataModel("expected i8 and bytes, struct variant unexpected"))
827 }
828}
829
830impl<'a, W: Write + 'a> serde::ser::Serializer for &mut ExtSerializer<'a, W> {
831 type Ok = ();
832 type Error = Error;
833
834 type SerializeSeq = serde::ser::Impossible<(), Error>;
835 type SerializeTuple = Self;
836 type SerializeTupleStruct = serde::ser::Impossible<(), Error>;
837 type SerializeTupleVariant = serde::ser::Impossible<(), Error>;
838 type SerializeMap = serde::ser::Impossible<(), Error>;
839 type SerializeStruct = serde::ser::Impossible<(), Error>;
840 type SerializeStructVariant = serde::ser::Impossible<(), Error>;
841
842 #[cold]
843 fn serialize_bytes(self, _val: &[u8]) -> Result<Self::Ok, Self::Error> {
844 Err(Error::InvalidDataModel("expected tuple, received bytes"))
845 }
846
847 #[cold]
848 fn serialize_bool(self, _val: bool) -> Result<Self::Ok, Self::Error> {
849 Err(Error::InvalidDataModel("expected tuple, received bool"))
850 }
851
852 #[cold]
853 fn serialize_i8(self, _value: i8) -> Result<Self::Ok, Self::Error> {
854 Err(Error::InvalidDataModel("expected tuple, received i8"))
855 }
856
857 #[cold]
858 fn serialize_i16(self, _val: i16) -> Result<Self::Ok, Self::Error> {
859 Err(Error::InvalidDataModel("expected tuple, received i16"))
860 }
861
862 #[cold]
863 fn serialize_i32(self, _val: i32) -> Result<Self::Ok, Self::Error> {
864 Err(Error::InvalidDataModel("expected tuple, received i32"))
865 }
866
867 #[cold]
868 fn serialize_i64(self, _val: i64) -> Result<Self::Ok, Self::Error> {
869 Err(Error::InvalidDataModel("expected tuple, received i64"))
870 }
871
872 #[cold]
873 fn serialize_u8(self, _val: u8) -> Result<Self::Ok, Self::Error> {
874 Err(Error::InvalidDataModel("expected tuple, received u8"))
875 }
876
877 #[cold]
878 fn serialize_u16(self, _val: u16) -> Result<Self::Ok, Self::Error> {
879 Err(Error::InvalidDataModel("expected tuple, received u16"))
880 }
881
882 #[cold]
883 fn serialize_u32(self, _val: u32) -> Result<Self::Ok, Self::Error> {
884 Err(Error::InvalidDataModel("expected tuple, received u32"))
885 }
886
887 #[cold]
888 fn serialize_u64(self, _val: u64) -> Result<Self::Ok, Self::Error> {
889 Err(Error::InvalidDataModel("expected tuple, received u64"))
890 }
891
892 #[cold]
893 fn serialize_f32(self, _val: f32) -> Result<Self::Ok, Self::Error> {
894 Err(Error::InvalidDataModel("expected tuple, received f32"))
895 }
896
897 #[cold]
898 fn serialize_f64(self, _val: f64) -> Result<Self::Ok, Self::Error> {
899 Err(Error::InvalidDataModel("expected tuple, received f64"))
900 }
901
902 #[cold]
903 fn serialize_char(self, _val: char) -> Result<Self::Ok, Self::Error> {
904 Err(Error::InvalidDataModel("expected tuple, received char"))
905 }
906
907 #[cold]
908 fn serialize_str(self, _val: &str) -> Result<Self::Ok, Self::Error> {
909 Err(Error::InvalidDataModel("expected tuple, received str"))
910 }
911
912 #[cold]
913 fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
914 Err(Error::InvalidDataModel("expected tuple, received unit"))
915 }
916
917 #[cold]
918 fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> {
919 Err(Error::InvalidDataModel("expected tuple, received unit_struct"))
920 }
921
922 #[cold]
923 fn serialize_unit_variant(self, _name: &'static str, _idx: u32, _variant: &'static str) -> Result<Self::Ok, Self::Error> {
924 Err(Error::InvalidDataModel("expected tuple, received unit_variant"))
925 }
926
927 #[cold]
928 fn serialize_newtype_struct<T: ?Sized>(self, _name: &'static str, _value: &T) -> Result<Self::Ok, Self::Error>
929 where T: Serialize
930 {
931 Err(Error::InvalidDataModel("expected tuple, received newtype_struct"))
932 }
933
934 #[cold]
935 fn serialize_newtype_variant<T: ?Sized>(self, _name: &'static str, _idx: u32, _variant: &'static str, _value: &T) -> Result<Self::Ok, Self::Error>
936 where T: Serialize
937 {
938 Err(Error::InvalidDataModel("expected tuple, received newtype_variant"))
939 }
940
941 #[cold]
942 fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
943 Err(Error::InvalidDataModel("expected tuple, received none"))
944 }
945
946 #[cold]
947 fn serialize_some<T: ?Sized>(self, _value: &T) -> Result<Self::Ok, Self::Error>
948 where T: Serialize
949 {
950 Err(Error::InvalidDataModel("expected tuple, received some"))
951 }
952
953 #[cold]
954 fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
955 Err(Error::InvalidDataModel("expected tuple, received seq"))
956 }
957
958 fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Error> {
959 self.tuple_received = true;
961
962 Ok(self)
963 }
964
965 #[cold]
966 fn serialize_tuple_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeTupleStruct, Error> {
967 Err(Error::InvalidDataModel("expected tuple, received tuple_struct"))
968 }
969
970 #[cold]
971 fn serialize_tuple_variant(self, _name: &'static str, _idx: u32, _variant: &'static str, _len: usize) -> Result<Self::SerializeTupleVariant, Error> {
972 Err(Error::InvalidDataModel("expected tuple, received tuple_variant"))
973 }
974
975 #[cold]
976 fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Error> {
977 Err(Error::InvalidDataModel("expected tuple, received map"))
978 }
979
980 #[cold]
981 fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct, Error> {
982 Err(Error::InvalidDataModel("expected tuple, received struct"))
983 }
984
985 #[cold]
986 fn serialize_struct_variant(self, _name: &'static str, _idx: u32, _variant: &'static str, _len: usize) -> Result<Self::SerializeStructVariant, Error> {
987 Err(Error::InvalidDataModel("expected tuple, received struct_variant"))
988 }
989}
990
991impl<'a, W: Write + 'a> SerializeTuple for &mut ExtSerializer<'a, W> {
992 type Ok = ();
993 type Error = Error;
994
995 #[inline]
996 fn serialize_element<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), Self::Error> {
997 value.serialize(&mut self.fields_se)
998 }
999
1000 #[inline(always)]
1001 fn end(self) -> Result<Self::Ok, Self::Error> {
1002 Ok(())
1003 }
1004}
1005
1006impl<'a, W: Write + 'a> ExtSerializer<'a, W> {
1007 #[inline]
1008 fn new<C>(ser: &'a mut Serializer<W, C>) -> Self {
1009 Self {
1010 fields_se: ExtFieldSerializer::new(ser),
1011 tuple_received: false,
1012 }
1013 }
1014
1015 #[inline]
1016 fn end(self) -> Result<(), Error> {
1017 if !self.tuple_received {
1018 Err(Error::InvalidDataModel("expected tuple, received nothing"))
1019 } else {
1020 self.fields_se.end()
1021 }
1022 }
1023}
1024
1025impl<'a, W: Write + 'a> ExtFieldSerializer<'a, W> {
1026 #[inline]
1027 fn new<C>(ser: &'a mut Serializer<W, C>) -> Self {
1028 Self {
1029 wr: UnderlyingWrite::get_mut(ser),
1030 tag: None,
1031 finish: false,
1032 }
1033 }
1034
1035 #[inline]
1036 fn end(self) -> Result<(), Error> {
1037 if self.finish {
1038 Ok(())
1039 } else {
1040 Err(Error::InvalidDataModel("expected i8 and bytes"))
1041 }
1042 }
1043}
1044
1045#[inline]
1050pub fn write<W, T>(wr: &mut W, val: &T) -> Result<(), Error>
1051where
1052 W: Write + ?Sized,
1053 T: Serialize + ?Sized
1054{
1055 val.serialize(&mut Serializer::new(wr))
1056}
1057
1058pub fn write_named<W, T>(wr: &mut W, val: &T) -> Result<(), Error>
1063where
1064 W: Write + ?Sized,
1065 T: Serialize + ?Sized
1066{
1067 let mut se = Serializer::new(wr).with_struct_map();
1068 val.serialize(&mut se)
1069}
1070
1071#[inline]
1076pub fn to_vec<T>(val: &T) -> Result<Vec<u8>, Error>
1077where
1078 T: Serialize + ?Sized
1079{
1080 let mut wr = Vec::with_capacity(128);
1081 write(&mut wr, val)?;
1082 Ok(wr)
1083}
1084
1085#[inline]
1092pub fn to_vec_named<T>(val: &T) -> Result<Vec<u8>, Error>
1093where
1094 T: Serialize + ?Sized
1095{
1096 let mut wr = Vec::with_capacity(128);
1097 write_named(&mut wr, val)?;
1098 Ok(wr)
1099}