1use std::io::Write;
2use std::u32;
3
4use serde;
5
6use byteorder::WriteBytesExt;
7
8use super::config::{IntEncoding, SizeLimit};
9use super::{Error, ErrorKind, Result};
10use config::{BincodeByteOrder, Options};
11use std::mem::size_of;
12
13/// An Serializer that encodes values directly into a Writer.
14///
15/// The specified byte-order will impact the endianness that is
16/// used during the encoding.
17///
18/// This struct should not be used often.
19/// For most cases, prefer the `encode_into` function.
20pub struct Serializer<W, O: Options> {
21 writer: W,
22 _options: O,
23}
24
25macro_rules! impl_serialize_literal {
26 ($ser_method:ident($ty:ty) = $write:ident()) => {
27 pub(crate) fn $ser_method(&mut self, v: $ty) -> Result<()> {
28 self.writer
29 .$write::<<O::Endian as BincodeByteOrder>::Endian>(v)
30 .map_err(Into::into)
31 }
32 };
33}
34
35impl<W: Write, O: Options> Serializer<W, O> {
36 /// Creates a new Serializer with the given `Write`r.
37 pub fn new(w: W, options: O) -> Serializer<W, O> {
38 Serializer {
39 writer: w,
40 _options: options,
41 }
42 }
43
44 pub(crate) fn serialize_byte(&mut self, v: u8) -> Result<()> {
45 self.writer.write_u8(v).map_err(op:Into::into)
46 }
47
48 impl_serialize_literal! {serialize_literal_u16(u16) = write_u16()}
49 impl_serialize_literal! {serialize_literal_u32(u32) = write_u32()}
50 impl_serialize_literal! {serialize_literal_u64(u64) = write_u64()}
51
52 serde_if_integer128! {
53 impl_serialize_literal!{serialize_literal_u128(u128) = write_u128()}
54 }
55}
56
57macro_rules! impl_serialize_int {
58 ($ser_method:ident($ty:ty) = $ser_int:ident()) => {
59 fn $ser_method(self, v: $ty) -> Result<()> {
60 O::IntEncoding::$ser_int(self, v)
61 }
62 };
63}
64
65impl<'a, W: Write, O: Options> serde::Serializer for &'a mut Serializer<W, O> {
66 type Ok = ();
67 type Error = Error;
68 type SerializeSeq = Compound<'a, W, O>;
69 type SerializeTuple = Compound<'a, W, O>;
70 type SerializeTupleStruct = Compound<'a, W, O>;
71 type SerializeTupleVariant = Compound<'a, W, O>;
72 type SerializeMap = Compound<'a, W, O>;
73 type SerializeStruct = Compound<'a, W, O>;
74 type SerializeStructVariant = Compound<'a, W, O>;
75
76 fn serialize_unit(self) -> Result<()> {
77 Ok(())
78 }
79
80 fn serialize_unit_struct(self, _: &'static str) -> Result<()> {
81 Ok(())
82 }
83
84 fn serialize_bool(self, v: bool) -> Result<()> {
85 self.serialize_byte(v as u8)
86 }
87
88 fn serialize_u8(self, v: u8) -> Result<()> {
89 self.serialize_byte(v)
90 }
91
92 impl_serialize_int! {serialize_u16(u16) = serialize_u16()}
93 impl_serialize_int! {serialize_u32(u32) = serialize_u32()}
94 impl_serialize_int! {serialize_u64(u64) = serialize_u64()}
95
96 fn serialize_i8(self, v: i8) -> Result<()> {
97 self.serialize_byte(v as u8)
98 }
99
100 impl_serialize_int! {serialize_i16(i16) = serialize_i16()}
101 impl_serialize_int! {serialize_i32(i32) = serialize_i32()}
102 impl_serialize_int! {serialize_i64(i64) = serialize_i64()}
103
104 serde_if_integer128! {
105 impl_serialize_int!{serialize_u128(u128) = serialize_u128()}
106 impl_serialize_int!{serialize_i128(i128) = serialize_i128()}
107 }
108
109 fn serialize_f32(self, v: f32) -> Result<()> {
110 self.writer
111 .write_f32::<<O::Endian as BincodeByteOrder>::Endian>(v)
112 .map_err(Into::into)
113 }
114
115 fn serialize_f64(self, v: f64) -> Result<()> {
116 self.writer
117 .write_f64::<<O::Endian as BincodeByteOrder>::Endian>(v)
118 .map_err(Into::into)
119 }
120
121 fn serialize_str(self, v: &str) -> Result<()> {
122 O::IntEncoding::serialize_len(self, v.len())?;
123 self.writer.write_all(v.as_bytes()).map_err(Into::into)
124 }
125
126 fn serialize_char(self, c: char) -> Result<()> {
127 self.writer
128 .write_all(encode_utf8(c).as_slice())
129 .map_err(Into::into)
130 }
131
132 fn serialize_bytes(self, v: &[u8]) -> Result<()> {
133 O::IntEncoding::serialize_len(self, v.len())?;
134 self.writer.write_all(v).map_err(Into::into)
135 }
136
137 fn serialize_none(self) -> Result<()> {
138 self.writer.write_u8(0).map_err(Into::into)
139 }
140
141 fn serialize_some<T: ?Sized>(self, v: &T) -> Result<()>
142 where
143 T: serde::Serialize,
144 {
145 self.writer.write_u8(1)?;
146 v.serialize(self)
147 }
148
149 fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
150 let len = len.ok_or(ErrorKind::SequenceMustHaveLength)?;
151 O::IntEncoding::serialize_len(self, len)?;
152 Ok(Compound { ser: self })
153 }
154
155 fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
156 Ok(Compound { ser: self })
157 }
158
159 fn serialize_tuple_struct(
160 self,
161 _name: &'static str,
162 _len: usize,
163 ) -> Result<Self::SerializeTupleStruct> {
164 Ok(Compound { ser: self })
165 }
166
167 fn serialize_tuple_variant(
168 self,
169 _name: &'static str,
170 variant_index: u32,
171 _variant: &'static str,
172 _len: usize,
173 ) -> Result<Self::SerializeTupleVariant> {
174 O::IntEncoding::serialize_u32(self, variant_index)?;
175 Ok(Compound { ser: self })
176 }
177
178 fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap> {
179 let len = len.ok_or(ErrorKind::SequenceMustHaveLength)?;
180 O::IntEncoding::serialize_len(self, len)?;
181 Ok(Compound { ser: self })
182 }
183
184 fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
185 Ok(Compound { ser: self })
186 }
187
188 fn serialize_struct_variant(
189 self,
190 _name: &'static str,
191 variant_index: u32,
192 _variant: &'static str,
193 _len: usize,
194 ) -> Result<Self::SerializeStructVariant> {
195 O::IntEncoding::serialize_u32(self, variant_index)?;
196 Ok(Compound { ser: self })
197 }
198
199 fn serialize_newtype_struct<T: ?Sized>(self, _name: &'static str, value: &T) -> Result<()>
200 where
201 T: serde::ser::Serialize,
202 {
203 value.serialize(self)
204 }
205
206 fn serialize_newtype_variant<T: ?Sized>(
207 self,
208 _name: &'static str,
209 variant_index: u32,
210 _variant: &'static str,
211 value: &T,
212 ) -> Result<()>
213 where
214 T: serde::ser::Serialize,
215 {
216 O::IntEncoding::serialize_u32(self, variant_index)?;
217 value.serialize(self)
218 }
219
220 fn serialize_unit_variant(
221 self,
222 _name: &'static str,
223 variant_index: u32,
224 _variant: &'static str,
225 ) -> Result<()> {
226 O::IntEncoding::serialize_u32(self, variant_index)
227 }
228
229 fn is_human_readable(&self) -> bool {
230 false
231 }
232}
233
234pub(crate) struct SizeChecker<O: Options> {
235 pub options: O,
236 pub total: u64,
237}
238
239impl<O: Options> SizeChecker<O> {
240 fn add_raw(&mut self, size: u64) -> Result<()> {
241 self.options.limit().add(size)?;
242 self.total += size;
243
244 Ok(())
245 }
246
247 fn add_discriminant(&mut self, idx: u32) -> Result<()> {
248 let bytes: u64 = O::IntEncoding::u32_size(idx);
249 self.add_raw(size:bytes)
250 }
251
252 fn add_len(&mut self, len: usize) -> Result<()> {
253 let bytes: u64 = O::IntEncoding::len_size(len);
254 self.add_raw(size:bytes)
255 }
256}
257
258macro_rules! impl_size_int {
259 ($ser_method:ident($ty:ty) = $size_method:ident()) => {
260 fn $ser_method(self, v: $ty) -> Result<()> {
261 self.add_raw(O::IntEncoding::$size_method(v))
262 }
263 };
264}
265
266impl<'a, O: Options> serde::Serializer for &'a mut SizeChecker<O> {
267 type Ok = ();
268 type Error = Error;
269 type SerializeSeq = SizeCompound<'a, O>;
270 type SerializeTuple = SizeCompound<'a, O>;
271 type SerializeTupleStruct = SizeCompound<'a, O>;
272 type SerializeTupleVariant = SizeCompound<'a, O>;
273 type SerializeMap = SizeCompound<'a, O>;
274 type SerializeStruct = SizeCompound<'a, O>;
275 type SerializeStructVariant = SizeCompound<'a, O>;
276
277 fn serialize_unit(self) -> Result<()> {
278 Ok(())
279 }
280
281 fn serialize_unit_struct(self, _: &'static str) -> Result<()> {
282 Ok(())
283 }
284
285 fn serialize_bool(self, _: bool) -> Result<()> {
286 self.add_raw(1)
287 }
288
289 fn serialize_u8(self, _: u8) -> Result<()> {
290 self.add_raw(1)
291 }
292 fn serialize_i8(self, _: i8) -> Result<()> {
293 self.add_raw(1)
294 }
295
296 impl_size_int! {serialize_u16(u16) = u16_size()}
297 impl_size_int! {serialize_u32(u32) = u32_size()}
298 impl_size_int! {serialize_u64(u64) = u64_size()}
299 impl_size_int! {serialize_i16(i16) = i16_size()}
300 impl_size_int! {serialize_i32(i32) = i32_size()}
301 impl_size_int! {serialize_i64(i64) = i64_size()}
302
303 serde_if_integer128! {
304 impl_size_int!{serialize_u128(u128) = u128_size()}
305 impl_size_int!{serialize_i128(i128) = i128_size()}
306 }
307
308 fn serialize_f32(self, _: f32) -> Result<()> {
309 self.add_raw(size_of::<f32>() as u64)
310 }
311
312 fn serialize_f64(self, _: f64) -> Result<()> {
313 self.add_raw(size_of::<f64>() as u64)
314 }
315
316 fn serialize_str(self, v: &str) -> Result<()> {
317 self.add_len(v.len())?;
318 self.add_raw(v.len() as u64)
319 }
320
321 fn serialize_char(self, c: char) -> Result<()> {
322 self.add_raw(encode_utf8(c).as_slice().len() as u64)
323 }
324
325 fn serialize_bytes(self, v: &[u8]) -> Result<()> {
326 self.add_len(v.len())?;
327 self.add_raw(v.len() as u64)
328 }
329
330 fn serialize_none(self) -> Result<()> {
331 self.add_raw(1)
332 }
333
334 fn serialize_some<T: ?Sized>(self, v: &T) -> Result<()>
335 where
336 T: serde::Serialize,
337 {
338 self.add_raw(1)?;
339 v.serialize(self)
340 }
341
342 fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
343 let len = len.ok_or(ErrorKind::SequenceMustHaveLength)?;
344
345 self.add_len(len)?;
346 Ok(SizeCompound { ser: self })
347 }
348
349 fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
350 Ok(SizeCompound { ser: self })
351 }
352
353 fn serialize_tuple_struct(
354 self,
355 _name: &'static str,
356 _len: usize,
357 ) -> Result<Self::SerializeTupleStruct> {
358 Ok(SizeCompound { ser: self })
359 }
360
361 fn serialize_tuple_variant(
362 self,
363 _name: &'static str,
364 variant_index: u32,
365 _variant: &'static str,
366 _len: usize,
367 ) -> Result<Self::SerializeTupleVariant> {
368 self.add_raw(O::IntEncoding::u32_size(variant_index))?;
369 Ok(SizeCompound { ser: self })
370 }
371
372 fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap> {
373 let len = len.ok_or(ErrorKind::SequenceMustHaveLength)?;
374
375 self.add_len(len)?;
376 Ok(SizeCompound { ser: self })
377 }
378
379 fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
380 Ok(SizeCompound { ser: self })
381 }
382
383 fn serialize_struct_variant(
384 self,
385 _name: &'static str,
386 variant_index: u32,
387 _variant: &'static str,
388 _len: usize,
389 ) -> Result<Self::SerializeStructVariant> {
390 self.add_discriminant(variant_index)?;
391 Ok(SizeCompound { ser: self })
392 }
393
394 fn serialize_newtype_struct<V: serde::Serialize + ?Sized>(
395 self,
396 _name: &'static str,
397 v: &V,
398 ) -> Result<()> {
399 v.serialize(self)
400 }
401
402 fn serialize_unit_variant(
403 self,
404 _name: &'static str,
405 variant_index: u32,
406 _variant: &'static str,
407 ) -> Result<()> {
408 self.add_discriminant(variant_index)
409 }
410
411 fn serialize_newtype_variant<V: serde::Serialize + ?Sized>(
412 self,
413 _name: &'static str,
414 variant_index: u32,
415 _variant: &'static str,
416 value: &V,
417 ) -> Result<()> {
418 self.add_discriminant(variant_index)?;
419 value.serialize(self)
420 }
421
422 fn is_human_readable(&self) -> bool {
423 false
424 }
425}
426
427pub struct Compound<'a, W: 'a, O: Options + 'a> {
428 ser: &'a mut Serializer<W, O>,
429}
430
431impl<'a, W, O> serde::ser::SerializeSeq for Compound<'a, W, O>
432where
433 W: Write,
434 O: Options,
435{
436 type Ok = ();
437 type Error = Error;
438
439 #[inline]
440 fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<()>
441 where
442 T: serde::ser::Serialize,
443 {
444 value.serialize(&mut *self.ser)
445 }
446
447 #[inline]
448 fn end(self) -> Result<()> {
449 Ok(())
450 }
451}
452
453impl<'a, W, O> serde::ser::SerializeTuple for Compound<'a, W, O>
454where
455 W: Write,
456 O: Options,
457{
458 type Ok = ();
459 type Error = Error;
460
461 #[inline]
462 fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<()>
463 where
464 T: serde::ser::Serialize,
465 {
466 value.serialize(&mut *self.ser)
467 }
468
469 #[inline]
470 fn end(self) -> Result<()> {
471 Ok(())
472 }
473}
474
475impl<'a, W, O> serde::ser::SerializeTupleStruct for Compound<'a, W, O>
476where
477 W: Write,
478 O: Options,
479{
480 type Ok = ();
481 type Error = Error;
482
483 #[inline]
484 fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<()>
485 where
486 T: serde::ser::Serialize,
487 {
488 value.serialize(&mut *self.ser)
489 }
490
491 #[inline]
492 fn end(self) -> Result<()> {
493 Ok(())
494 }
495}
496
497impl<'a, W, O> serde::ser::SerializeTupleVariant for Compound<'a, W, O>
498where
499 W: Write,
500 O: Options,
501{
502 type Ok = ();
503 type Error = Error;
504
505 #[inline]
506 fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<()>
507 where
508 T: serde::ser::Serialize,
509 {
510 value.serialize(&mut *self.ser)
511 }
512
513 #[inline]
514 fn end(self) -> Result<()> {
515 Ok(())
516 }
517}
518
519impl<'a, W, O> serde::ser::SerializeMap for Compound<'a, W, O>
520where
521 W: Write,
522 O: Options,
523{
524 type Ok = ();
525 type Error = Error;
526
527 #[inline]
528 fn serialize_key<K: ?Sized>(&mut self, value: &K) -> Result<()>
529 where
530 K: serde::ser::Serialize,
531 {
532 value.serialize(&mut *self.ser)
533 }
534
535 #[inline]
536 fn serialize_value<V: ?Sized>(&mut self, value: &V) -> Result<()>
537 where
538 V: serde::ser::Serialize,
539 {
540 value.serialize(&mut *self.ser)
541 }
542
543 #[inline]
544 fn end(self) -> Result<()> {
545 Ok(())
546 }
547}
548
549impl<'a, W, O> serde::ser::SerializeStruct for Compound<'a, W, O>
550where
551 W: Write,
552 O: Options,
553{
554 type Ok = ();
555 type Error = Error;
556
557 #[inline]
558 fn serialize_field<T: ?Sized>(&mut self, _key: &'static str, value: &T) -> Result<()>
559 where
560 T: serde::ser::Serialize,
561 {
562 value.serialize(&mut *self.ser)
563 }
564
565 #[inline]
566 fn end(self) -> Result<()> {
567 Ok(())
568 }
569}
570
571impl<'a, W, O> serde::ser::SerializeStructVariant for Compound<'a, W, O>
572where
573 W: Write,
574 O: Options,
575{
576 type Ok = ();
577 type Error = Error;
578
579 #[inline]
580 fn serialize_field<T: ?Sized>(&mut self, _key: &'static str, value: &T) -> Result<()>
581 where
582 T: serde::ser::Serialize,
583 {
584 value.serialize(&mut *self.ser)
585 }
586
587 #[inline]
588 fn end(self) -> Result<()> {
589 Ok(())
590 }
591}
592
593pub(crate) struct SizeCompound<'a, S: Options + 'a> {
594 ser: &'a mut SizeChecker<S>,
595}
596
597impl<'a, O: Options> serde::ser::SerializeSeq for SizeCompound<'a, O> {
598 type Ok = ();
599 type Error = Error;
600
601 #[inline]
602 fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<()>
603 where
604 T: serde::ser::Serialize,
605 {
606 value.serialize(&mut *self.ser)
607 }
608
609 #[inline]
610 fn end(self) -> Result<()> {
611 Ok(())
612 }
613}
614
615impl<'a, O: Options> serde::ser::SerializeTuple for SizeCompound<'a, O> {
616 type Ok = ();
617 type Error = Error;
618
619 #[inline]
620 fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<()>
621 where
622 T: serde::ser::Serialize,
623 {
624 value.serialize(&mut *self.ser)
625 }
626
627 #[inline]
628 fn end(self) -> Result<()> {
629 Ok(())
630 }
631}
632
633impl<'a, O: Options> serde::ser::SerializeTupleStruct for SizeCompound<'a, O> {
634 type Ok = ();
635 type Error = Error;
636
637 #[inline]
638 fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<()>
639 where
640 T: serde::ser::Serialize,
641 {
642 value.serialize(&mut *self.ser)
643 }
644
645 #[inline]
646 fn end(self) -> Result<()> {
647 Ok(())
648 }
649}
650
651impl<'a, O: Options> serde::ser::SerializeTupleVariant for SizeCompound<'a, O> {
652 type Ok = ();
653 type Error = Error;
654
655 #[inline]
656 fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<()>
657 where
658 T: serde::ser::Serialize,
659 {
660 value.serialize(&mut *self.ser)
661 }
662
663 #[inline]
664 fn end(self) -> Result<()> {
665 Ok(())
666 }
667}
668
669impl<'a, O: Options + 'a> serde::ser::SerializeMap for SizeCompound<'a, O> {
670 type Ok = ();
671 type Error = Error;
672
673 #[inline]
674 fn serialize_key<K: ?Sized>(&mut self, value: &K) -> Result<()>
675 where
676 K: serde::ser::Serialize,
677 {
678 value.serialize(&mut *self.ser)
679 }
680
681 #[inline]
682 fn serialize_value<V: ?Sized>(&mut self, value: &V) -> Result<()>
683 where
684 V: serde::ser::Serialize,
685 {
686 value.serialize(&mut *self.ser)
687 }
688
689 #[inline]
690 fn end(self) -> Result<()> {
691 Ok(())
692 }
693}
694
695impl<'a, O: Options> serde::ser::SerializeStruct for SizeCompound<'a, O> {
696 type Ok = ();
697 type Error = Error;
698
699 #[inline]
700 fn serialize_field<T: ?Sized>(&mut self, _key: &'static str, value: &T) -> Result<()>
701 where
702 T: serde::ser::Serialize,
703 {
704 value.serialize(&mut *self.ser)
705 }
706
707 #[inline]
708 fn end(self) -> Result<()> {
709 Ok(())
710 }
711}
712
713impl<'a, O: Options> serde::ser::SerializeStructVariant for SizeCompound<'a, O> {
714 type Ok = ();
715 type Error = Error;
716
717 #[inline]
718 fn serialize_field<T: ?Sized>(&mut self, _key: &'static str, value: &T) -> Result<()>
719 where
720 T: serde::ser::Serialize,
721 {
722 value.serialize(&mut *self.ser)
723 }
724
725 #[inline]
726 fn end(self) -> Result<()> {
727 Ok(())
728 }
729}
730const TAG_CONT: u8 = 0b1000_0000;
731const TAG_TWO_B: u8 = 0b1100_0000;
732const TAG_THREE_B: u8 = 0b1110_0000;
733const TAG_FOUR_B: u8 = 0b1111_0000;
734const MAX_ONE_B: u32 = 0x80;
735const MAX_TWO_B: u32 = 0x800;
736const MAX_THREE_B: u32 = 0x10000;
737
738fn encode_utf8(c: char) -> EncodeUtf8 {
739 let code: u32 = c as u32;
740 let mut buf: [u8; 4] = [0; 4];
741 let pos: usize = if code < MAX_ONE_B {
742 buf[3] = code as u8;
743 3
744 } else if code < MAX_TWO_B {
745 buf[2] = (code >> 6 & 0x1F) as u8 | TAG_TWO_B;
746 buf[3] = (code & 0x3F) as u8 | TAG_CONT;
747 2
748 } else if code < MAX_THREE_B {
749 buf[1] = (code >> 12 & 0x0F) as u8 | TAG_THREE_B;
750 buf[2] = (code >> 6 & 0x3F) as u8 | TAG_CONT;
751 buf[3] = (code & 0x3F) as u8 | TAG_CONT;
752 1
753 } else {
754 buf[0] = (code >> 18 & 0x07) as u8 | TAG_FOUR_B;
755 buf[1] = (code >> 12 & 0x3F) as u8 | TAG_CONT;
756 buf[2] = (code >> 6 & 0x3F) as u8 | TAG_CONT;
757 buf[3] = (code & 0x3F) as u8 | TAG_CONT;
758 0
759 };
760 EncodeUtf8 { buf, pos }
761}
762
763struct EncodeUtf8 {
764 buf: [u8; 4],
765 pos: usize,
766}
767
768impl EncodeUtf8 {
769 fn as_slice(&self) -> &[u8] {
770 &self.buf[self.pos..]
771 }
772}
773