1use core::fmt;
18use core::{mem, ops::Deref, marker::PhantomData, iter::FromIterator, convert::TryFrom, time::Duration};
19use core::num::{
20 NonZeroI8,
21 NonZeroI16,
22 NonZeroI32,
23 NonZeroI64,
24 NonZeroI128,
25 NonZeroU8,
26 NonZeroU16,
27 NonZeroU32,
28 NonZeroU64,
29 NonZeroU128,
30};
31use arrayvec::ArrayVec;
32
33use byte_slice_cast::{AsByteSlice, AsMutByteSlice, ToMutByteSlice};
34
35#[cfg(any(feature = "std", feature = "full"))]
36use crate::alloc::{
37 string::String,
38 sync::Arc,
39 rc::Rc,
40};
41use crate::alloc::{
42 vec::Vec,
43 boxed::Box,
44 borrow::{Cow, ToOwned},
45 collections::{
46 BTreeMap, BTreeSet, VecDeque, LinkedList, BinaryHeap
47 }
48};
49use crate::compact::Compact;
50use crate::encode_like::EncodeLike;
51use crate::Error;
52
53pub(crate) const MAX_PREALLOCATION: usize = 4 * 1024;
54const A_BILLION: u32 = 1_000_000_000;
55
56pub trait Input {
58 fn remaining_len(&mut self) -> Result<Option<usize>, Error>;
65
66 fn read(&mut self, into: &mut [u8]) -> Result<(), Error>;
71
72 fn read_byte(&mut self) -> Result<u8, Error> {
74 let mut buf = [0u8];
75 self.read(&mut buf[..])?;
76 Ok(buf[0])
77 }
78
79 fn descend_ref(&mut self) -> Result<(), Error> {
84 Ok(())
85 }
86
87 fn ascend_ref(&mut self) {}
90}
91
92impl<'a> Input for &'a [u8] {
93 fn remaining_len(&mut self) -> Result<Option<usize>, Error> {
94 Ok(Some(self.len()))
95 }
96
97 fn read(&mut self, into: &mut [u8]) -> Result<(), Error> {
98 if into.len() > self.len() {
99 return Err("Not enough data to fill buffer".into());
100 }
101 let len = into.len();
102 into.copy_from_slice(&self[..len]);
103 *self = &self[len..];
104 Ok(())
105 }
106}
107
108#[cfg(feature = "std")]
109impl From<std::io::Error> for Error {
110 fn from(err: std::io::Error) -> Self {
111 use std::io::ErrorKind::*;
112 match err.kind() {
113 NotFound => "io error: NotFound".into(),
114 PermissionDenied => "io error: PermissionDenied".into(),
115 ConnectionRefused => "io error: ConnectionRefused".into(),
116 ConnectionReset => "io error: ConnectionReset".into(),
117 ConnectionAborted => "io error: ConnectionAborted".into(),
118 NotConnected => "io error: NotConnected".into(),
119 AddrInUse => "io error: AddrInUse".into(),
120 AddrNotAvailable => "io error: AddrNotAvailable".into(),
121 BrokenPipe => "io error: BrokenPipe".into(),
122 AlreadyExists => "io error: AlreadyExists".into(),
123 WouldBlock => "io error: WouldBlock".into(),
124 InvalidInput => "io error: InvalidInput".into(),
125 InvalidData => "io error: InvalidData".into(),
126 TimedOut => "io error: TimedOut".into(),
127 WriteZero => "io error: WriteZero".into(),
128 Interrupted => "io error: Interrupted".into(),
129 Other => "io error: Other".into(),
130 UnexpectedEof => "io error: UnexpectedEof".into(),
131 _ => "io error: Unknown".into(),
132 }
133 }
134}
135
136#[cfg(feature = "std")]
138pub struct IoReader<R: std::io::Read>(pub R);
139
140#[cfg(feature = "std")]
141impl<R: std::io::Read> Input for IoReader<R> {
142 fn remaining_len(&mut self) -> Result<Option<usize>, Error> {
143 Ok(None)
144 }
145
146 fn read(&mut self, into: &mut [u8]) -> Result<(), Error> {
147 self.0.read_exact(into).map_err(Into::into)
148 }
149}
150
151pub trait Output {
153 fn write(&mut self, bytes: &[u8]);
155
156 fn push_byte(&mut self, byte: u8) {
158 self.write(&[byte]);
159 }
160}
161
162#[cfg(not(feature = "std"))]
163impl Output for Vec<u8> {
164 fn write(&mut self, bytes: &[u8]) {
165 self.extend_from_slice(bytes)
166 }
167}
168
169#[cfg(feature = "std")]
170impl<W: std::io::Write> Output for W {
171 fn write(&mut self, bytes: &[u8]) {
172 (self as &mut dyn std::io::Write).write_all(bytes).expect("Codec outputs are infallible");
173 }
174}
175
176
177#[doc(hidden)]
181#[non_exhaustive]
182pub enum TypeInfo {
183 Unknown,
185 U8,
186 I8,
187 U16,
188 I16,
189 U32,
190 I32,
191 U64,
192 I64,
193 U128,
194 I128,
195}
196
197pub trait Encode {
202 #[doc(hidden)]
205 const TYPE_INFO: TypeInfo = TypeInfo::Unknown;
206
207 fn size_hint(&self) -> usize {
212 0
213 }
214
215 fn encode_to<T: Output + ?Sized>(&self, dest: &mut T) {
217 self.using_encoded(|buf| dest.write(buf));
218 }
219
220 fn encode(&self) -> Vec<u8> {
222 let mut r = Vec::with_capacity(self.size_hint());
223 self.encode_to(&mut r);
224 r
225 }
226
227 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
229 f(&self.encode())
230 }
231
232 fn encoded_size(&self) -> usize {
241 let mut size_tracker = SizeTracker { written: 0 };
242 self.encode_to(&mut size_tracker);
243 size_tracker.written
244 }
245}
246
247struct SizeTracker {
249 written: usize,
250}
251
252impl Output for SizeTracker {
253 fn write(&mut self, bytes: &[u8]) {
254 self.written += bytes.len();
255 }
256
257 fn push_byte(&mut self, _byte: u8) {
258 self.written += 1;
259 }
260}
261
262pub trait DecodeLength {
265 fn len(self_encoded: &[u8]) -> Result<usize, Error>;
267}
268
269pub trait Decode: Sized {
271 #[doc(hidden)]
274 const TYPE_INFO: TypeInfo = TypeInfo::Unknown;
275
276 fn decode<I: Input>(input: &mut I) -> Result<Self, Error>;
278
279 fn skip<I: Input>(input: &mut I) -> Result<(), Error> {
284 Self::decode(input).map(|_| ())
285 }
286
287 fn encoded_fixed_size() -> Option<usize> {
294 None
295 }
296}
297
298pub trait Codec: Decode + Encode {}
300impl<S: Decode + Encode> Codec for S {}
301
302pub trait FullEncode: Encode + EncodeLike {}
305impl<S: Encode + EncodeLike> FullEncode for S {}
306
307pub trait FullCodec: Decode + FullEncode {}
310impl<S: Decode + FullEncode> FullCodec for S {}
311
312pub trait WrapperTypeEncode: Deref {}
320
321impl<T: ?Sized> WrapperTypeEncode for Box<T> {}
322impl<T: ?Sized + Encode> EncodeLike for Box<T> {}
323impl<T: Encode> EncodeLike<T> for Box<T> {}
324impl<T: Encode> EncodeLike<Box<T>> for T {}
325
326impl<T: ?Sized> WrapperTypeEncode for &T {}
327impl<T: ?Sized + Encode> EncodeLike for &T {}
328impl<T: Encode> EncodeLike<T> for &T {}
329impl<T: Encode> EncodeLike<&T> for T {}
330impl<T: Encode> EncodeLike<T> for &&T {}
331impl<T: Encode> EncodeLike<&&T> for T {}
332
333impl<T: ?Sized> WrapperTypeEncode for &mut T {}
334impl<T: ?Sized + Encode> EncodeLike for &mut T {}
335impl<T: Encode> EncodeLike<T> for &mut T {}
336impl<T: Encode> EncodeLike<&mut T> for T {}
337
338impl<'a, T: ToOwned + ?Sized> WrapperTypeEncode for Cow<'a, T> {}
339impl<'a, T: ToOwned + Encode + ?Sized> EncodeLike for Cow<'a, T> {}
340impl<'a, T: ToOwned + Encode> EncodeLike<T> for Cow<'a, T> {}
341impl<'a, T: ToOwned + Encode> EncodeLike<Cow<'a, T>> for T {}
342
343#[cfg(any(feature = "std", feature = "full"))]
344mod feature_full_wrapper_type_encode {
345 use super::*;
346
347 impl<T: ?Sized> WrapperTypeEncode for Arc<T> {}
348 impl<T: ?Sized + Encode> EncodeLike for Arc<T> {}
349 impl<T: Encode> EncodeLike<T> for Arc<T> {}
350 impl<T: Encode> EncodeLike<Arc<T>> for T {}
351
352 impl<T: ?Sized> WrapperTypeEncode for Rc<T> {}
353 impl<T: ?Sized + Encode> EncodeLike for Rc<T> {}
354 impl<T: Encode> EncodeLike<T> for Rc<T> {}
355 impl<T: Encode> EncodeLike<Rc<T>> for T {}
356
357 impl WrapperTypeEncode for String {}
358 impl EncodeLike for String {}
359 impl EncodeLike<&str> for String {}
360 impl EncodeLike<String> for &str {}
361}
362
363impl<T, X> Encode for X where
364 T: Encode + ?Sized,
365 X: WrapperTypeEncode<Target = T>,
366{
367 fn size_hint(&self) -> usize {
368 (&**self).size_hint()
369 }
370
371 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
372 (&**self).using_encoded(f)
373 }
374
375 fn encode(&self) -> Vec<u8> {
376 (&**self).encode()
377 }
378
379 fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
380 (&**self).encode_to(dest)
381 }
382}
383
384pub trait WrapperTypeDecode: Sized {
388 type Wrapped: Into<Self>;
390}
391impl<T> WrapperTypeDecode for Box<T> {
392 type Wrapped = T;
393}
394#[cfg(any(feature = "std", feature = "full"))]
395impl<T> WrapperTypeDecode for Arc<T> {
396 type Wrapped = T;
397}
398#[cfg(any(feature = "std", feature = "full"))]
399impl<T> WrapperTypeDecode for Rc<T> {
400 type Wrapped = T;
401}
402
403impl<T, X> Decode for X where
404 T: Decode + Into<X>,
405 X: WrapperTypeDecode<Wrapped=T>,
406{
407 fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
408 input.descend_ref()?;
409 let result = Ok(T::decode(input)?.into());
410 input.ascend_ref();
411 result
412 }
413
414}
415
416macro_rules! with_type_info {
423 ( $type_info:expr, $macro:ident $( ( $( $params:ident ),* ) )?, { $( $unknown_variant:tt )* }, ) => {
424 match $type_info {
425 TypeInfo::U8 => { $macro!(u8 $( $( , $params )* )? ) },
426 TypeInfo::I8 => { $macro!(i8 $( $( , $params )* )? ) },
427 TypeInfo::U16 => { $macro!(u16 $( $( , $params )* )? ) },
428 TypeInfo::I16 => { $macro!(i16 $( $( , $params )* )? ) },
429 TypeInfo::U32 => { $macro!(u32 $( $( , $params )* )? ) },
430 TypeInfo::I32 => { $macro!(i32 $( $( , $params )* )? ) },
431 TypeInfo::U64 => { $macro!(u64 $( $( , $params )* )? ) },
432 TypeInfo::I64 => { $macro!(i64 $( $( , $params )* )? ) },
433 TypeInfo::U128 => { $macro!(u128 $( $( , $params )* )? ) },
434 TypeInfo::I128 => { $macro!(i128 $( $( , $params )* )? ) },
435 TypeInfo::Unknown => { $( $unknown_variant )* },
436 }
437 };
438}
439
440pub trait EncodeAsRef<'a, T: 'a> {
442 type RefType: Encode + From<&'a T>;
444}
445
446impl<T: Encode, E: Encode> Encode for Result<T, E> {
447 fn size_hint(&self) -> usize {
448 1 + match *self {
449 Ok(ref t) => t.size_hint(),
450 Err(ref t) => t.size_hint(),
451 }
452 }
453
454 fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
455 match *self {
456 Ok(ref t) => {
457 dest.push_byte(0);
458 t.encode_to(dest);
459 }
460 Err(ref e) => {
461 dest.push_byte(1);
462 e.encode_to(dest);
463 }
464 }
465 }
466}
467
468impl<T, LikeT, E, LikeE> EncodeLike<Result<LikeT, LikeE>> for Result<T, E>
469where
470 T: EncodeLike<LikeT>,
471 LikeT: Encode,
472 E: EncodeLike<LikeE>,
473 LikeE: Encode,
474{}
475
476impl<T: Decode, E: Decode> Decode for Result<T, E> {
477 fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
478 match input.read_byte()
479 .map_err(|e| e.chain("Could not result variant byte for `Result`"))?
480 {
481 0 => Ok(
482 Ok(T::decode(input).map_err(|e| e.chain("Could not Decode `Result::Ok(T)`"))?)
483 ),
484 1 => Ok(
485 Err(E::decode(input).map_err(|e| e.chain("Could not decode `Result::Error(E)`"))?)
486 ),
487 _ => Err("unexpected first byte decoding Result".into()),
488 }
489 }
490}
491
492#[derive(Eq, PartialEq, Clone, Copy)]
494pub struct OptionBool(pub Option<bool>);
495
496impl fmt::Debug for OptionBool {
497 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
498 self.0.fmt(f)
499 }
500}
501
502impl Encode for OptionBool {
503 fn size_hint(&self) -> usize {
504 1
505 }
506
507 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
508 f(&[match *self {
509 OptionBool(None) => 0u8,
510 OptionBool(Some(true)) => 1u8,
511 OptionBool(Some(false)) => 2u8,
512 }])
513 }
514}
515
516impl EncodeLike for OptionBool {}
517
518impl Decode for OptionBool {
519 fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
520 match input.read_byte()? {
521 0 => Ok(OptionBool(None)),
522 1 => Ok(OptionBool(Some(true))),
523 2 => Ok(OptionBool(Some(false))),
524 _ => Err("unexpected first byte decoding OptionBool".into()),
525 }
526 }
527}
528
529impl<T: EncodeLike<U>, U: Encode> EncodeLike<Option<U>> for Option<T> {}
530
531impl<T: Encode> Encode for Option<T> {
532 fn size_hint(&self) -> usize {
533 1 + match *self {
534 Some(ref t) => t.size_hint(),
535 None => 0,
536 }
537 }
538
539 fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
540 match *self {
541 Some(ref t) => {
542 dest.push_byte(1);
543 t.encode_to(dest);
544 }
545 None => dest.push_byte(0),
546 }
547 }
548}
549
550impl<T: Decode> Decode for Option<T> {
551 fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
552 match input.read_byte()
553 .map_err(|e| e.chain("Could not decode variant byte for `Option`"))?
554 {
555 0 => Ok(None),
556 1 => Ok(
557 Some(T::decode(input).map_err(|e| e.chain("Could not decode `Option::Some(T)`"))?)
558 ),
559 _ => Err("unexpected first byte decoding Option".into()),
560 }
561 }
562}
563
564macro_rules! impl_for_non_zero {
565 ( $( $name:ty ),* $(,)? ) => {
566 $(
567 impl Encode for $name {
568 fn size_hint(&self) -> usize {
569 self.get().size_hint()
570 }
571
572 fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
573 self.get().encode_to(dest)
574 }
575
576 fn encode(&self) -> Vec<u8> {
577 self.get().encode()
578 }
579
580 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
581 self.get().using_encoded(f)
582 }
583 }
584
585 impl Decode for $name {
586 fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
587 Self::new(Decode::decode(input)?)
588 .ok_or_else(|| Error::from("cannot create non-zero number from 0"))
589 }
590 }
591 )*
592 }
593}
594
595pub(crate) fn encode_slice_no_len<T: Encode, W: Output + ?Sized>(slice: &[T], dest: &mut W) {
599 macro_rules! encode_to {
600 ( u8, $slice:ident, $dest:ident ) => {{
601 let typed = unsafe { mem::transmute::<&[T], &[u8]>(&$slice[..]) };
602 $dest.write(&typed)
603 }};
604 ( i8, $slice:ident, $dest:ident ) => {{
605 let typed = unsafe { mem::transmute::<&[T], &[u8]>(&$slice[..]) };
608 $dest.write(&typed)
609 }};
610 ( $ty:ty, $slice:ident, $dest:ident ) => {{
611 if cfg!(target_endian = "little") {
612 let typed = unsafe { mem::transmute::<&[T], &[$ty]>(&$slice[..]) };
613 $dest.write(<[$ty] as AsByteSlice<$ty>>::as_byte_slice(typed))
614 } else {
615 for item in $slice.iter() {
616 item.encode_to(dest);
617 }
618 }
619 }};
620 }
621
622 with_type_info! {
623 <T as Encode>::TYPE_INFO,
624 encode_to(slice, dest),
625 {
626 for item in slice.iter() {
627 item.encode_to(dest);
628 }
629 },
630 }
631}
632
633pub(crate) fn decode_vec_with_len<T: Decode, I: Input>(
638 input: &mut I,
639 len: usize,
640) -> Result<Vec<T>, Error> {
641 fn decode_unoptimized<I: Input, T: Decode>(
642 input: &mut I,
643 items_len: usize,
644 ) -> Result<Vec<T>, Error> {
645 let input_capacity = input.remaining_len()?
646 .unwrap_or(MAX_PREALLOCATION)
647 .checked_div(mem::size_of::<T>())
648 .unwrap_or(0);
649 let mut r = Vec::with_capacity(input_capacity.min(items_len));
650 input.descend_ref()?;
651 for _ in 0..items_len {
652 r.push(T::decode(input)?);
653 }
654 input.ascend_ref();
655 Ok(r)
656 }
657
658 macro_rules! decode {
659 ( $ty:ty, $input:ident, $len:ident ) => {{
660 if cfg!(target_endian = "little") || mem::size_of::<T>() == 1 {
661 let vec = read_vec_from_u8s::<_, $ty>($input, $len)?;
662 Ok(unsafe { mem::transmute::<Vec<$ty>, Vec<T>>(vec) })
663 } else {
664 decode_unoptimized($input, $len)
665 }
666 }};
667 }
668
669 with_type_info! {
670 <T as Decode>::TYPE_INFO,
671 decode(input, len),
672 {
673 decode_unoptimized(input, len)
674 },
675 }
676}
677
678impl_for_non_zero! {
679 NonZeroI8,
680 NonZeroI16,
681 NonZeroI32,
682 NonZeroI64,
683 NonZeroI128,
684 NonZeroU8,
685 NonZeroU16,
686 NonZeroU32,
687 NonZeroU64,
688 NonZeroU128,
689}
690
691macro_rules! impl_array {
692 ( $( $n:expr, )* ) => {
693 $(
694 impl<T: Encode> Encode for [T; $n] {
695 fn size_hint(&self) -> usize {
696 mem::size_of::<T>() * $n
697 }
698
699 fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
700 encode_slice_no_len(&self[..], dest)
701 }
702 }
703
704 impl<T: Decode> Decode for [T; $n] {
705 fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
706 let mut r = ArrayVec::new();
707 for _ in 0..$n {
708 r.push(T::decode(input)?);
709 }
710 let i = r.into_inner();
711
712 match i {
713 Ok(a) => Ok(a),
714 Err(_) => Err("failed to get inner array from ArrayVec".into()),
715 }
716 }
717 }
718
719 impl<T: EncodeLike<U>, U: Encode> EncodeLike<[U; $n]> for [T; $n] {}
720 )*
721 }
722}
723
724impl_array!(
725 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
726 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
727 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
728 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
729 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
730 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108,
731 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124,
732 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140,
733 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156,
734 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172,
735 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188,
736 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204,
737 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220,
738 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236,
739 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252,
740 253, 254, 255, 256, 384, 512, 768, 1024, 2048, 4096, 8192, 16384, 32768,
741);
742
743impl Encode for str {
744 fn size_hint(&self) -> usize {
745 self.as_bytes().size_hint()
746 }
747
748 fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
749 self.as_bytes().encode_to(dest)
750 }
751
752 fn encode(&self) -> Vec<u8> {
753 self.as_bytes().encode()
754 }
755
756 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
757 self.as_bytes().using_encoded(f)
758 }
759}
760
761impl<'a, T: ToOwned + ?Sized> Decode for Cow<'a, T>
762 where <T as ToOwned>::Owned: Decode,
763{
764 fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
765 Ok(Cow::Owned(Decode::decode(input)?))
766 }
767}
768
769impl<T> EncodeLike for PhantomData<T> {}
770
771impl<T> Encode for PhantomData<T> {
772 fn encode_to<W: Output + ?Sized>(&self, _dest: &mut W) {}
773}
774
775impl<T> Decode for PhantomData<T> {
776 fn decode<I: Input>(_input: &mut I) -> Result<Self, Error> {
777 Ok(PhantomData)
778 }
779}
780
781#[cfg(any(feature = "std", feature = "full"))]
782impl Decode for String {
783 fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
784 Self::from_utf8(Vec::decode(input)?).map_err(|_| "Invalid utf8 sequence".into())
785 }
786}
787
788pub(crate) fn compact_encode_len_to<W: Output + ?Sized>(dest: &mut W, len: usize) -> Result<(), Error> {
790 if len > u32::max_value() as usize {
791 return Err("Attempted to serialize a collection with too many elements.".into());
792 }
793
794 Compact(len as u32).encode_to(dest);
795 Ok(())
796}
797
798impl<T: Encode> Encode for [T] {
799 fn size_hint(&self) -> usize {
800 mem::size_of::<u32>() + mem::size_of::<T>() * self.len()
801 }
802
803 fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
804 compact_encode_len_to(dest, self.len()).expect("Compact encodes length");
805
806 encode_slice_no_len(self, dest)
807 }
808}
809
810pub(crate) fn read_vec_from_u8s<I, T>(input: &mut I, items_len: usize) -> Result<Vec<T>, Error>
815where
816 I: Input,
817 T: ToMutByteSlice + Default + Clone,
818{
819 debug_assert!(MAX_PREALLOCATION >= mem::size_of::<T>(), "Invalid precondition");
820
821 let byte_len = items_len.checked_mul(mem::size_of::<T>())
822 .ok_or_else(|| "Item is too big and cannot be allocated")?;
823
824 let input_len = input.remaining_len()?;
825
826 if input_len.map(|l| l < byte_len).unwrap_or(false) {
828 return Err("Not enough data to decode vector".into())
829 }
830
831 let r = if input_len.is_some() || byte_len < MAX_PREALLOCATION {
836 let mut items: Vec<T> = vec![Default::default(); items_len];
838 let mut bytes_slice = items.as_mut_byte_slice();
839 input.read(&mut bytes_slice)?;
840
841 items
842 } else {
843 let max_preallocated_items = MAX_PREALLOCATION / mem::size_of::<T>();
846
847 let mut items: Vec<T> = vec![];
849
850 let mut items_remains = items_len;
851
852 while items_remains > 0 {
853 let items_len_read = max_preallocated_items.min(items_remains);
854
855 let items_len_filled = items.len();
856 let items_new_size = items_len_filled + items_len_read;
857
858 items.reserve_exact(items_len_read);
859 unsafe {
860 items.set_len(items_new_size);
861 }
862
863 let bytes_slice = items.as_mut_byte_slice();
864 let bytes_len_filled = items_len_filled * mem::size_of::<T>();
865 input.read(&mut bytes_slice[bytes_len_filled..])?;
866
867 items_remains = items_remains.saturating_sub(items_len_read);
868 }
869
870 items
871 };
872
873 Ok(r)
874}
875
876impl<T> WrapperTypeEncode for Vec<T> {}
877impl<T: EncodeLike<U>, U: Encode> EncodeLike<Vec<U>> for Vec<T> {}
878impl<T: EncodeLike<U>, U: Encode> EncodeLike<&[U]> for Vec<T> {}
879impl<T: EncodeLike<U>, U: Encode> EncodeLike<Vec<U>> for &[T] {}
880
881impl<T: Decode> Decode for Vec<T> {
882 fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
883 <Compact<u32>>::decode(input).and_then(move |Compact(len)| {
884 decode_vec_with_len(input, len as usize)
885 })
886 }
887}
888
889macro_rules! impl_codec_through_iterator {
890 ($(
891 $type:ident
892 { $( $generics:ident $( : $decode_additional:ident )? ),* }
893 { $( $type_like_generics:ident ),* }
894 { $( $impl_like_generics:tt )* }
895 )*) => {$(
896 impl<$( $generics: Encode ),*> Encode for $type<$( $generics, )*> {
897 fn size_hint(&self) -> usize {
898 mem::size_of::<u32>() $( + mem::size_of::<$generics>() * self.len() )*
899 }
900
901 fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
902 compact_encode_len_to(dest, self.len()).expect("Compact encodes length");
903
904 for i in self.iter() {
905 i.encode_to(dest);
906 }
907 }
908 }
909
910 impl<$( $generics: Decode $( + $decode_additional )? ),*> Decode
911 for $type<$( $generics, )*>
912 {
913 fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
914 <Compact<u32>>::decode(input).and_then(move |Compact(len)| {
915 input.descend_ref()?;
916 let result = Result::from_iter((0..len).map(|_| Decode::decode(input)));
917 input.ascend_ref();
918 result
919 })
920 }
921 }
922
923 impl<$( $impl_like_generics )*> EncodeLike<$type<$( $type_like_generics ),*>>
924 for $type<$( $generics ),*> {}
925 impl<$( $impl_like_generics )*> EncodeLike<&[( $( $type_like_generics, )* )]>
926 for $type<$( $generics ),*> {}
927 impl<$( $impl_like_generics )*> EncodeLike<$type<$( $type_like_generics ),*>>
928 for &[( $( $generics, )* )] {}
929 )*}
930}
931
932impl_codec_through_iterator! {
933 BTreeMap { K: Ord, V } { LikeK, LikeV}
934 { K: EncodeLike<LikeK>, LikeK: Encode, V: EncodeLike<LikeV>, LikeV: Encode }
935 BTreeSet { T: Ord } { LikeT }
936 { T: EncodeLike<LikeT>, LikeT: Encode }
937 LinkedList { T } { LikeT }
938 { T: EncodeLike<LikeT>, LikeT: Encode }
939 BinaryHeap { T: Ord } { LikeT }
940 { T: EncodeLike<LikeT>, LikeT: Encode }
941}
942
943impl<T: Encode> EncodeLike for VecDeque<T> {}
944impl<T: EncodeLike<U>, U: Encode> EncodeLike<&[U]> for VecDeque<T> {}
945impl<T: EncodeLike<U>, U: Encode> EncodeLike<VecDeque<U>> for &[T] {}
946impl<T: EncodeLike<U>, U: Encode> EncodeLike<Vec<U>> for VecDeque<T> {}
947impl<T: EncodeLike<U>, U: Encode> EncodeLike<VecDeque<U>> for Vec<T> {}
948
949impl<T: Encode> Encode for VecDeque<T> {
950 fn size_hint(&self) -> usize {
951 mem::size_of::<u32>() + mem::size_of::<T>() * self.len()
952 }
953
954 fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
955 compact_encode_len_to(dest, self.len()).expect("Compact encodes length");
956
957 macro_rules! encode_to {
958 ( $ty:ty, $self:ident, $dest:ident ) => {{
959 if cfg!(target_endian = "little") || mem::size_of::<T>() == 1 {
960 let slices = $self.as_slices();
961 let typed = unsafe {
962 core::mem::transmute::<(&[T], &[T]), (&[$ty], &[$ty])>(slices)
963 };
964
965 $dest.write(<[$ty] as AsByteSlice<$ty>>::as_byte_slice(typed.0));
966 $dest.write(<[$ty] as AsByteSlice<$ty>>::as_byte_slice(typed.1));
967 } else {
968 for item in $self {
969 item.encode_to($dest);
970 }
971 }
972 }};
973 }
974
975 with_type_info! {
976 <T as Encode>::TYPE_INFO,
977 encode_to(self, dest),
978 {
979 for item in self {
980 item.encode_to(dest);
981 }
982 },
983 }
984 }
985}
986
987impl<T: Decode> Decode for VecDeque<T> {
988 fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
989 Ok(<Vec<T>>::decode(input)?.into())
990 }
991}
992
993impl EncodeLike for () {}
994
995impl Encode for () {
996 fn encode_to<W: Output + ?Sized>(&self, _dest: &mut W) {
997 }
998
999 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
1000 f(&[])
1001 }
1002
1003 fn encode(&self) -> Vec<u8> {
1004 Vec::new()
1005 }
1006}
1007
1008impl Decode for () {
1009 fn decode<I: Input>(_: &mut I) -> Result<(), Error> {
1010 Ok(())
1011 }
1012}
1013
1014macro_rules! impl_len {
1015 ( $( $type:ident< $($g:ident),* > ),* ) => { $(
1016 impl<$($g),*> DecodeLength for $type<$($g),*> {
1017 fn len(mut self_encoded: &[u8]) -> Result<usize, Error> {
1018 usize::try_from(u32::from(Compact::<u32>::decode(&mut self_encoded)?))
1019 .map_err(|_| "Failed convert decoded size into usize.".into())
1020 }
1021 }
1022 )*}
1023}
1024
1025impl_len!(Vec<T>, BTreeSet<T>, BTreeMap<K, V>, VecDeque<T>, BinaryHeap<T>, LinkedList<T>);
1027
1028macro_rules! tuple_impl {
1029 (
1030 ($one:ident, $extra:ident),
1031 ) => {
1032 impl<$one: Encode> Encode for ($one,) {
1033 fn size_hint(&self) -> usize {
1034 self.0.size_hint()
1035 }
1036
1037 fn encode_to<T: Output + ?Sized>(&self, dest: &mut T) {
1038 self.0.encode_to(dest);
1039 }
1040
1041 fn encode(&self) -> Vec<u8> {
1042 self.0.encode()
1043 }
1044
1045 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
1046 self.0.using_encoded(f)
1047 }
1048 }
1049
1050 impl<$one: Decode> Decode for ($one,) {
1051 fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1052 match $one::decode(input) {
1053 Err(e) => Err(e),
1054 Ok($one) => Ok(($one,)),
1055 }
1056 }
1057 }
1058
1059 impl<$one: DecodeLength> DecodeLength for ($one,) {
1060 fn len(self_encoded: &[u8]) -> Result<usize, Error> {
1061 $one::len(self_encoded)
1062 }
1063 }
1064
1065 impl<$one: EncodeLike<$extra>, $extra: Encode> crate::EncodeLike<($extra,)> for ($one,) {}
1066 };
1067 (($first:ident, $fextra:ident), $( ( $rest:ident, $rextra:ident ), )+) => {
1068 impl<$first: Encode, $($rest: Encode),+> Encode for ($first, $($rest),+) {
1069 fn size_hint(&self) -> usize {
1070 let (
1071 ref $first,
1072 $(ref $rest),+
1073 ) = *self;
1074 $first.size_hint()
1075 $( + $rest.size_hint() )+
1076 }
1077
1078 fn encode_to<T: Output + ?Sized>(&self, dest: &mut T) {
1079 let (
1080 ref $first,
1081 $(ref $rest),+
1082 ) = *self;
1083
1084 $first.encode_to(dest);
1085 $($rest.encode_to(dest);)+
1086 }
1087 }
1088
1089 impl<$first: Decode, $($rest: Decode),+> Decode for ($first, $($rest),+) {
1090 fn decode<INPUT: Input>(input: &mut INPUT) -> Result<Self, super::Error> {
1091 Ok((
1092 match $first::decode(input) {
1093 Ok(x) => x,
1094 Err(e) => return Err(e),
1095 },
1096 $(match $rest::decode(input) {
1097 Ok(x) => x,
1098 Err(e) => return Err(e),
1099 },)+
1100 ))
1101 }
1102 }
1103
1104 impl<$first: EncodeLike<$fextra>, $fextra: Encode,
1105 $($rest: EncodeLike<$rextra>, $rextra: Encode),+> crate::EncodeLike<($fextra, $( $rextra ),+)>
1106 for ($first, $($rest),+) {}
1107
1108 impl<$first: DecodeLength, $($rest),+> DecodeLength for ($first, $($rest),+) {
1109 fn len(self_encoded: &[u8]) -> Result<usize, Error> {
1110 $first::len(self_encoded)
1111 }
1112 }
1113
1114 tuple_impl!( $( ($rest, $rextra), )+ );
1115 }
1116}
1117
1118#[allow(non_snake_case)]
1119mod inner_tuple_impl {
1120 use super::*;
1121
1122 tuple_impl!(
1123 (A0, A1), (B0, B1), (C0, C1), (D0, D1), (E0, E1), (F0, F1), (G0, G1), (H0, H1), (I0, I1),
1124 (J0, J1), (K0, K1), (L0, L1), (M0, M1), (N0, N1), (O0, O1), (P0, P1), (Q0, Q1), (R0, R1),
1125 );
1126}
1127
1128macro_rules! impl_endians {
1129 ( $( $t:ty; $ty_info:ident ),* ) => { $(
1130 impl EncodeLike for $t {}
1131
1132 impl Encode for $t {
1133 const TYPE_INFO: TypeInfo = TypeInfo::$ty_info;
1134
1135 fn size_hint(&self) -> usize {
1136 mem::size_of::<$t>()
1137 }
1138
1139 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
1140 let buf = self.to_le_bytes();
1141 f(&buf[..])
1142 }
1143 }
1144
1145 impl Decode for $t {
1146 const TYPE_INFO: TypeInfo = TypeInfo::$ty_info;
1147
1148 fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1149 let mut buf = [0u8; mem::size_of::<$t>()];
1150 input.read(&mut buf)?;
1151 Ok(<$t>::from_le_bytes(buf))
1152 }
1153 }
1154 )* }
1155}
1156macro_rules! impl_one_byte {
1157 ( $( $t:ty; $ty_info:ident ),* ) => { $(
1158 impl EncodeLike for $t {}
1159
1160 impl Encode for $t {
1161 const TYPE_INFO: TypeInfo = TypeInfo::$ty_info;
1162
1163 fn size_hint(&self) -> usize {
1164 mem::size_of::<$t>()
1165 }
1166
1167 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
1168 f(&[*self as u8][..])
1169 }
1170 }
1171
1172 impl Decode for $t {
1173 const TYPE_INFO: TypeInfo = TypeInfo::$ty_info;
1174
1175 fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1176 Ok(input.read_byte()? as $t)
1177 }
1178 }
1179 )* }
1180}
1181
1182impl_endians!(u16; U16, u32; U32, u64; U64, u128; U128, i16; I16, i32; I32, i64; I64, i128; I128);
1183impl_one_byte!(u8; U8, i8; I8);
1184
1185impl EncodeLike for bool {}
1186
1187impl Encode for bool {
1188 fn size_hint(&self) -> usize {
1189 mem::size_of::<bool>()
1190 }
1191
1192 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
1193 f(&[*self as u8][..])
1194 }
1195}
1196
1197impl Decode for bool {
1198 fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1199 let byte = input.read_byte()?;
1200 match byte {
1201 0 => Ok(false),
1202 1 => Ok(true),
1203 _ => Err("Invalid boolean representation".into())
1204 }
1205 }
1206}
1207
1208impl Encode for Duration {
1209 fn size_hint(&self) -> usize {
1210 mem::size_of::<u64>() + mem::size_of::<u32>()
1211 }
1212
1213 fn encode(&self) -> Vec<u8> {
1214 let secs = self.as_secs();
1215 let nanos = self.subsec_nanos();
1216 (secs, nanos).encode()
1217 }
1218}
1219
1220impl Decode for Duration {
1221 fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1222 let (secs, nanos) = <(u64, u32)>::decode(input)
1223 .map_err(|e| e.chain("Could not decode `Duration(u64, u32)`"))?;
1224 if nanos >= A_BILLION {
1225 Err("Could not decode `Duration`: Number of nanoseconds should not be higher than 10^9.".into())
1226 } else {
1227 Ok(Duration::new(secs, nanos))
1228 }
1229 }
1230}
1231
1232impl EncodeLike for Duration {}
1233
1234#[cfg(test)]
1235mod tests {
1236 use super::*;
1237 use std::borrow::Cow;
1238
1239 #[test]
1240 fn vec_is_sliceable() {
1241 let v = b"Hello world".to_vec();
1242 v.using_encoded(|ref slice|
1243 assert_eq!(slice, &b"\x2cHello world")
1244 );
1245 }
1246
1247 #[test]
1248 fn encode_borrowed_tuple() {
1249 let x = vec![1u8, 2, 3, 4];
1250 let y = 128i64;
1251
1252 let encoded = (&x, &y).encode();
1253
1254 assert_eq!((x, y), Decode::decode(&mut &encoded[..]).unwrap());
1255 }
1256
1257 #[test]
1258 fn cow_works() {
1259 let x = &[1u32, 2, 3, 4, 5, 6][..];
1260 let y = Cow::Borrowed(&x);
1261 assert_eq!(x.encode(), y.encode());
1262
1263 let z: Cow<'_, [u32]> = Cow::decode(&mut &x.encode()[..]).unwrap();
1264 assert_eq!(*z, *x);
1265 }
1266
1267 #[test]
1268 fn cow_string_works() {
1269 let x = "Hello world!";
1270 let y = Cow::Borrowed(&x);
1271 assert_eq!(x.encode(), y.encode());
1272
1273 let z: Cow<'_, str> = Cow::decode(&mut &x.encode()[..]).unwrap();
1274 assert_eq!(*z, *x);
1275 }
1276
1277 fn hexify(bytes: &[u8]) -> String {
1278 bytes.iter().map(|ref b| format!("{:02x}", b)).collect::<Vec<String>>().join(" ")
1279 }
1280
1281 #[test]
1282 fn string_encoded_as_expected() {
1283 let value = String::from("Hello, World!");
1284 let encoded = value.encode();
1285 assert_eq!(hexify(&encoded), "34 48 65 6c 6c 6f 2c 20 57 6f 72 6c 64 21");
1286 assert_eq!(<String>::decode(&mut &encoded[..]).unwrap(), value);
1287 }
1288
1289 #[test]
1290 fn vec_of_u8_encoded_as_expected() {
1291 let value = vec![0u8, 1, 1, 2, 3, 5, 8, 13, 21, 34];
1292 let encoded = value.encode();
1293 assert_eq!(hexify(&encoded), "28 00 01 01 02 03 05 08 0d 15 22");
1294 assert_eq!(<Vec<u8>>::decode(&mut &encoded[..]).unwrap(), value);
1295 }
1296
1297 #[test]
1298 fn vec_of_i16_encoded_as_expected() {
1299 let value = vec![0i16, 1, -1, 2, -2, 3, -3];
1300 let encoded = value.encode();
1301 assert_eq!(hexify(&encoded), "1c 00 00 01 00 ff ff 02 00 fe ff 03 00 fd ff");
1302 assert_eq!(<Vec<i16>>::decode(&mut &encoded[..]).unwrap(), value);
1303 }
1304
1305 #[test]
1306 fn vec_of_option_int_encoded_as_expected() {
1307 let value = vec![Some(1i8), Some(-1), None];
1308 let encoded = value.encode();
1309 assert_eq!(hexify(&encoded), "0c 01 01 01 ff 00");
1310 assert_eq!(<Vec<Option<i8>>>::decode(&mut &encoded[..]).unwrap(), value);
1311 }
1312
1313 #[test]
1314 fn vec_of_option_bool_encoded_as_expected() {
1315 let value = vec![OptionBool(Some(true)), OptionBool(Some(false)), OptionBool(None)];
1316 let encoded = value.encode();
1317 assert_eq!(hexify(&encoded), "0c 01 02 00");
1318 assert_eq!(<Vec<OptionBool>>::decode(&mut &encoded[..]).unwrap(), value);
1319 }
1320
1321 fn test_encode_length<T: Encode + Decode + DecodeLength>(thing: &T, len: usize) {
1322 assert_eq!(<T as DecodeLength>::len(&mut &thing.encode()[..]).unwrap(), len);
1323 }
1324
1325 #[test]
1326 fn len_works_for_decode_collection_types() {
1327 let vector = vec![10; 10];
1328 let mut btree_map: BTreeMap<u32, u32> = BTreeMap::new();
1329 btree_map.insert(1, 1);
1330 btree_map.insert(2, 2);
1331 let mut btree_set: BTreeSet<u32> = BTreeSet::new();
1332 btree_set.insert(1);
1333 btree_set.insert(2);
1334 let mut vd = VecDeque::new();
1335 vd.push_front(1);
1336 vd.push_front(2);
1337 let mut bh = BinaryHeap::new();
1338 bh.push(1);
1339 bh.push(2);
1340 let mut ll = LinkedList::new();
1341 ll.push_back(1);
1342 ll.push_back(2);
1343 let t1: (Vec<_>,) = (vector.clone(),);
1344 let t2: (Vec<_>, u32) = (vector.clone(), 3u32);
1345
1346 test_encode_length(&vector, 10);
1347 test_encode_length(&btree_map, 2);
1348 test_encode_length(&btree_set, 2);
1349 test_encode_length(&vd, 2);
1350 test_encode_length(&bh, 2);
1351 test_encode_length(&ll, 2);
1352 test_encode_length(&t1, 10);
1353 test_encode_length(&t2, 10);
1354 }
1355
1356 #[test]
1357 fn vec_of_string_encoded_as_expected() {
1358 let value = vec![
1359 "Hamlet".to_owned(),
1360 "Война и мир".to_owned(),
1361 "三国演义".to_owned(),
1362 "أَلْف لَيْلَة وَلَيْلَة".to_owned()
1363 ];
1364 let encoded = value.encode();
1365 assert_eq!(hexify(&encoded), "10 18 48 61 6d 6c 65 74 50 d0 92 d0 be d0 b9 d0 bd d0 b0 20 d0 \
1366 b8 20 d0 bc d0 b8 d1 80 30 e4 b8 89 e5 9b bd e6 bc 94 e4 b9 89 bc d8 a3 d9 8e d9 84 d9 92 \
1367 d9 81 20 d9 84 d9 8e d9 8a d9 92 d9 84 d9 8e d8 a9 20 d9 88 d9 8e d9 84 d9 8e d9 8a d9 92 \
1368 d9 84 d9 8e d8 a9 e2 80 8e");
1369 assert_eq!(<Vec<String>>::decode(&mut &encoded[..]).unwrap(), value);
1370 }
1371
1372 #[derive(Debug, PartialEq)]
1373 struct MyWrapper(Compact<u32>);
1374 impl Deref for MyWrapper {
1375 type Target = Compact<u32>;
1376 fn deref(&self) -> &Self::Target { &self.0 }
1377 }
1378 impl WrapperTypeEncode for MyWrapper {}
1379
1380 impl From<Compact<u32>> for MyWrapper {
1381 fn from(c: Compact<u32>) -> Self { MyWrapper(c) }
1382 }
1383 impl WrapperTypeDecode for MyWrapper {
1384 type Wrapped = Compact<u32>;
1385 }
1386
1387 #[test]
1388 fn should_work_for_wrapper_types() {
1389 let result = vec![0b1100];
1390
1391 assert_eq!(MyWrapper(3u32.into()).encode(), result);
1392 assert_eq!(MyWrapper::decode(&mut &*result).unwrap(), MyWrapper(3_u32.into()));
1393 }
1394
1395 #[test]
1396 fn codec_vec_deque_u8_and_u16() {
1397 let mut v_u8 = VecDeque::new();
1398 let mut v_u16 = VecDeque::new();
1399
1400 for i in 0..50 {
1401 v_u8.push_front(i as u8);
1402 v_u16.push_front(i as u16);
1403 }
1404 for i in 50..100 {
1405 v_u8.push_back(i as u8);
1406 v_u16.push_back(i as u16);
1407 }
1408
1409 assert_eq!(Decode::decode(&mut &v_u8.encode()[..]), Ok(v_u8));
1410 assert_eq!(Decode::decode(&mut &v_u16.encode()[..]), Ok(v_u16));
1411 }
1412
1413 #[test]
1414 fn codec_iterator() {
1415 let t1: BTreeSet<u32> = FromIterator::from_iter((0..10).flat_map(|i| 0..i));
1416 let t2: LinkedList<u32> = FromIterator::from_iter((0..10).flat_map(|i| 0..i));
1417 let t3: BinaryHeap<u32> = FromIterator::from_iter((0..10).flat_map(|i| 0..i));
1418 let t4: BTreeMap<u16, u32> = FromIterator::from_iter(
1419 (0..10)
1420 .flat_map(|i| 0..i)
1421 .map(|i| (i as u16, i + 10))
1422 );
1423 let t5: BTreeSet<Vec<u8>> = FromIterator::from_iter((0..10).map(|i| Vec::from_iter(0..i)));
1424 let t6: LinkedList<Vec<u8>> = FromIterator::from_iter((0..10).map(|i| Vec::from_iter(0..i)));
1425 let t7: BinaryHeap<Vec<u8>> = FromIterator::from_iter((0..10).map(|i| Vec::from_iter(0..i)));
1426 let t8: BTreeMap<Vec<u8>, u32> = FromIterator::from_iter(
1427 (0..10)
1428 .map(|i| Vec::from_iter(0..i))
1429 .map(|i| (i.clone(), i.len() as u32))
1430 );
1431
1432 assert_eq!(Decode::decode(&mut &t1.encode()[..]), Ok(t1));
1433 assert_eq!(Decode::decode(&mut &t2.encode()[..]), Ok(t2));
1434 assert_eq!(
1435 Decode::decode(&mut &t3.encode()[..]).map(BinaryHeap::into_sorted_vec),
1436 Ok(t3.into_sorted_vec()),
1437 );
1438 assert_eq!(Decode::decode(&mut &t4.encode()[..]), Ok(t4));
1439 assert_eq!(Decode::decode(&mut &t5.encode()[..]), Ok(t5));
1440 assert_eq!(Decode::decode(&mut &t6.encode()[..]), Ok(t6));
1441 assert_eq!(
1442 Decode::decode(&mut &t7.encode()[..]).map(BinaryHeap::into_sorted_vec),
1443 Ok(t7.into_sorted_vec()),
1444 );
1445 assert_eq!(Decode::decode(&mut &t8.encode()[..]), Ok(t8));
1446 }
1447
1448 #[test]
1449 fn io_reader() {
1450 let mut io_reader = IoReader(std::io::Cursor::new(&[1u8, 2, 3][..]));
1451
1452 let mut v = [0; 2];
1453 io_reader.read(&mut v[..]).unwrap();
1454 assert_eq!(v, [1, 2]);
1455
1456 assert_eq!(io_reader.read_byte().unwrap(), 3);
1457
1458 assert_eq!(io_reader.read_byte(), Err("io error: UnexpectedEof".into()));
1459 }
1460
1461 #[test]
1462 fn shared_references_implement_encode() {
1463 std::sync::Arc::new(10u32).encode();
1464 std::rc::Rc::new(10u32).encode();
1465 }
1466
1467 #[test]
1468 fn not_limit_input_test() {
1469 use crate::Input;
1470
1471 struct NoLimit<'a>(&'a [u8]);
1472
1473 impl<'a> Input for NoLimit<'a> {
1474 fn remaining_len(&mut self) -> Result<Option<usize>, Error> {
1475 Ok(None)
1476 }
1477
1478 fn read(&mut self, into: &mut [u8]) -> Result<(), Error> {
1479 self.0.read(into)
1480 }
1481 }
1482
1483 let len = MAX_PREALLOCATION * 2 + 1;
1484 let mut i = Compact(len as u32).encode();
1485 i.resize(i.len() + len, 0);
1486 assert_eq!(<Vec<u8>>::decode(&mut NoLimit(&i[..])).unwrap(), vec![0u8; len]);
1487
1488 let i = Compact(len as u32).encode();
1489 assert_eq!(
1490 <Vec<u8>>::decode(&mut NoLimit(&i[..])).err().unwrap().to_string(),
1491 "Not enough data to fill buffer",
1492 );
1493
1494 let i = Compact(1000u32).encode();
1495 assert_eq!(
1496 <Vec<u8>>::decode(&mut NoLimit(&i[..])).err().unwrap().to_string(),
1497 "Not enough data to fill buffer",
1498 );
1499 }
1500
1501 #[test]
1502 fn boolean() {
1503 assert_eq!(true.encode(), vec![1]);
1504 assert_eq!(false.encode(), vec![0]);
1505 assert_eq!(bool::decode(&mut &[1][..]).unwrap(), true);
1506 assert_eq!(bool::decode(&mut &[0][..]).unwrap(), false);
1507 }
1508
1509 #[test]
1510 fn some_encode_like() {
1511 fn t<B: EncodeLike>() {}
1512 t::<&[u8]>();
1513 t::<&str>();
1514 }
1515
1516 #[test]
1517 fn vec_deque_encode_like_vec() {
1518 let data: VecDeque<u32> = vec![1, 2, 3, 4, 5, 6].into();
1519 let encoded = data.encode();
1520
1521 let decoded = Vec::<u32>::decode(&mut &encoded[..]).unwrap();
1522 assert!(decoded.iter().all(|v| data.contains(&v)));
1523 assert_eq!(data.len(), decoded.len());
1524
1525 let encoded = decoded.encode();
1526 let decoded = VecDeque::<u32>::decode(&mut &encoded[..]).unwrap();
1527 assert_eq!(data, decoded);
1528 }
1529
1530 #[test]
1531 fn vec_decode_right_capacity() {
1532 let data: Vec<u32> = vec![1, 2, 3];
1533 let mut encoded = data.encode();
1534 encoded.resize(encoded.len() * 2, 0);
1535 let decoded = Vec::<u32>::decode(&mut &encoded[..]).unwrap();
1536 assert_eq!(data, decoded);
1537 assert_eq!(decoded.capacity(), decoded.len());
1538 let data: Vec<String> = vec!["1".into(), "2".into(), "3".into()];
1540 let mut encoded = data.encode();
1541 encoded.resize(65536, 0);
1542 let decoded = Vec::<String>::decode(&mut &encoded[..]).unwrap();
1543 assert_eq!(data, decoded);
1544 assert_eq!(decoded.capacity(), decoded.len());
1545 }
1546
1547 #[test]
1548 fn duration() {
1549 let num_secs = 13;
1550 let num_nanos = 37;
1551
1552 let duration = Duration::new(num_secs, num_nanos);
1553 let expected = (num_secs, num_nanos as u32).encode();
1554
1555 assert_eq!(duration.encode(), expected);
1556 assert_eq!(Duration::decode(&mut &expected[..]).unwrap(), duration);
1557 }
1558
1559 #[test]
1560 fn malformed_duration_encoding_fails() {
1561 let invalid_nanos = A_BILLION;
1563 let encoded = (0u64, invalid_nanos).encode();
1564 assert!(Duration::decode(&mut &encoded[..]).is_err());
1565
1566 let num_secs = 1u64;
1567 let num_nanos = 37u32;
1568 let invalid_nanos = num_secs as u32 * A_BILLION + num_nanos;
1569 let encoded = (num_secs, invalid_nanos).encode();
1570 assert!(Duration::decode(&mut &encoded[..]).is_err());
1572
1573 let duration = Duration::from_nanos(invalid_nanos as u64);
1575 let expected = (num_secs, num_nanos).encode();
1576
1577 assert_eq!(duration.encode(), expected);
1578 assert_eq!(Duration::decode(&mut &expected[..]).unwrap(), duration);
1579 }
1580
1581 #[test]
1582 fn u64_max() {
1583 let num_secs = u64::max_value();
1584 let num_nanos = 0;
1585 let duration = Duration::new(num_secs, num_nanos);
1586 let expected = (num_secs, num_nanos).encode();
1587
1588 assert_eq!(duration.encode(), expected);
1589 assert_eq!(Duration::decode(&mut &expected[..]).unwrap(), duration);
1590 }
1591
1592 #[test]
1593 fn decoding_does_not_overflow() {
1594 let num_secs = u64::max_value();
1595 let num_nanos = A_BILLION;
1596
1597 let encoded = (num_secs, num_nanos).encode();
1600 assert!(Duration::decode(&mut &encoded[..]).is_err());
1601 }
1602
1603 #[test]
1604 fn string_invalid_utf8() {
1605 let mut bytes: &[u8] = &[20, 114, 167, 10, 20, 114];
1607
1608 let obj = <String>::decode(&mut bytes);
1609 assert!(obj.is_err());
1610 }
1611
1612 #[test]
1613 fn empty_array_encode_and_decode() {
1614 let data: [u32; 0] = [];
1615 let encoded = data.encode();
1616 assert!(encoded.is_empty());
1617 <[u32; 0]>::decode(&mut &encoded[..]).unwrap();
1618 }
1619
1620 fn test_encoded_size(val: impl Encode) {
1621 let length = val.using_encoded(|v| v.len());
1622
1623 assert_eq!(length, val.encoded_size());
1624 }
1625
1626 struct TestStruct {
1627 data: Vec<u32>,
1628 other: u8,
1629 compact: Compact<u128>,
1630 }
1631
1632 impl Encode for TestStruct {
1633 fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
1634 self.data.encode_to(dest);
1635 self.other.encode_to(dest);
1636 self.compact.encode_to(dest);
1637 }
1638 }
1639
1640 #[test]
1641 fn encoded_size_works() {
1642 test_encoded_size(120u8);
1643 test_encoded_size(30u16);
1644 test_encoded_size(1u32);
1645 test_encoded_size(2343545u64);
1646 test_encoded_size(34358394245459854u128);
1647 test_encoded_size(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10u32]);
1648 test_encoded_size(Compact(32445u32));
1649 test_encoded_size(Compact(34353454453545u128));
1650 test_encoded_size(TestStruct {
1651 data: vec![1, 2, 4, 5, 6],
1652 other: 45,
1653 compact: Compact(123234545),
1654 });
1655 }
1656}