parity_scale_codec/
codec.rs

1// Copyright 2017-2018 Parity Technologies
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Serialization.
16
17use core::fmt;
18use core::{
19	convert::TryFrom,
20	iter::FromIterator,
21	marker::PhantomData,
22	mem,
23	mem::{
24		MaybeUninit,
25	},
26	ops::{Deref, Range, RangeInclusive},
27	time::Duration,
28};
29use core::num::{
30	NonZeroI8,
31	NonZeroI16,
32	NonZeroI32,
33	NonZeroI64,
34	NonZeroI128,
35	NonZeroU8,
36	NonZeroU16,
37	NonZeroU32,
38	NonZeroU64,
39	NonZeroU128,
40};
41
42use byte_slice_cast::{AsByteSlice, AsMutByteSlice, ToMutByteSlice};
43
44#[cfg(target_has_atomic = "ptr")]
45use crate::alloc::sync::Arc;
46use crate::alloc::{
47	boxed::Box,
48	borrow::{Cow, ToOwned},
49	collections::{
50		BTreeMap, BTreeSet, VecDeque, LinkedList, BinaryHeap
51	},
52	rc::Rc,
53	string::String,
54	vec::Vec,
55};
56use crate::compact::Compact;
57use crate::DecodeFinished;
58use crate::encode_like::EncodeLike;
59use crate::Error;
60
61pub(crate) const MAX_PREALLOCATION: usize = 4 * 1024;
62const A_BILLION: u32 = 1_000_000_000;
63
64/// Trait that allows reading of data into a slice.
65pub trait Input {
66	/// Should return the remaining length of the input data. If no information about the input
67	/// length is available, `None` should be returned.
68	///
69	/// The length is used to constrain the preallocation while decoding. Returning a garbage
70	/// length can open the doors for a denial of service attack to your application.
71	/// Otherwise, returning `None` can decrease the performance of your application.
72	fn remaining_len(&mut self) -> Result<Option<usize>, Error>;
73
74	/// Read the exact number of bytes required to fill the given buffer.
75	///
76	/// Note that this function is similar to `std::io::Read::read_exact` and not
77	/// `std::io::Read::read`.
78	fn read(&mut self, into: &mut [u8]) -> Result<(), Error>;
79
80	/// Read a single byte from the input.
81	fn read_byte(&mut self) -> Result<u8, Error> {
82		let mut buf = [0u8];
83		self.read(&mut buf[..])?;
84		Ok(buf[0])
85	}
86
87	/// Descend into nested reference when decoding.
88	/// This is called when decoding a new refence-based instance,
89	/// such as `Vec` or `Box`. Currently all such types are
90	/// allocated on the heap.
91	fn descend_ref(&mut self) -> Result<(), Error> {
92		Ok(())
93	}
94
95	/// Ascend to previous structure level when decoding.
96	/// This is called when decoding reference-based type is finished.
97	fn ascend_ref(&mut self) {}
98
99	/// !INTERNAL USE ONLY!
100	///
101	/// Decodes a `bytes::Bytes`.
102	#[cfg(feature = "bytes")]
103	#[doc(hidden)]
104	fn scale_internal_decode_bytes(&mut self) -> Result<bytes::Bytes, Error> where Self: Sized {
105		Vec::<u8>::decode(self).map(bytes::Bytes::from)
106	}
107}
108
109impl<'a> Input for &'a [u8] {
110	fn remaining_len(&mut self) -> Result<Option<usize>, Error> {
111		Ok(Some(self.len()))
112	}
113
114	fn read(&mut self, into: &mut [u8]) -> Result<(), Error> {
115		if into.len() > self.len() {
116			return Err("Not enough data to fill buffer".into());
117		}
118		let len = into.len();
119		into.copy_from_slice(&self[..len]);
120		*self = &self[len..];
121		Ok(())
122	}
123}
124
125#[cfg(feature = "std")]
126impl From<std::io::Error> for Error {
127	fn from(err: std::io::Error) -> Self {
128		use std::io::ErrorKind::*;
129		match err.kind() {
130			NotFound => "io error: NotFound".into(),
131			PermissionDenied => "io error: PermissionDenied".into(),
132			ConnectionRefused => "io error: ConnectionRefused".into(),
133			ConnectionReset => "io error: ConnectionReset".into(),
134			ConnectionAborted => "io error: ConnectionAborted".into(),
135			NotConnected => "io error: NotConnected".into(),
136			AddrInUse => "io error: AddrInUse".into(),
137			AddrNotAvailable => "io error: AddrNotAvailable".into(),
138			BrokenPipe => "io error: BrokenPipe".into(),
139			AlreadyExists => "io error: AlreadyExists".into(),
140			WouldBlock => "io error: WouldBlock".into(),
141			InvalidInput => "io error: InvalidInput".into(),
142			InvalidData => "io error: InvalidData".into(),
143			TimedOut => "io error: TimedOut".into(),
144			WriteZero => "io error: WriteZero".into(),
145			Interrupted => "io error: Interrupted".into(),
146			Other => "io error: Other".into(),
147			UnexpectedEof => "io error: UnexpectedEof".into(),
148			_ => "io error: Unknown".into(),
149		}
150	}
151}
152
153/// Wrapper that implements Input for any `Read` type.
154#[cfg(feature = "std")]
155pub struct IoReader<R: std::io::Read>(pub R);
156
157#[cfg(feature = "std")]
158impl<R: std::io::Read> Input for IoReader<R> {
159	fn remaining_len(&mut self) -> Result<Option<usize>, Error> {
160		Ok(None)
161	}
162
163	fn read(&mut self, into: &mut [u8]) -> Result<(), Error> {
164		self.0.read_exact(into).map_err(Into::into)
165	}
166}
167
168/// Trait that allows writing of data.
169pub trait Output {
170	/// Write to the output.
171	fn write(&mut self, bytes: &[u8]);
172
173	/// Write a single byte to the output.
174	fn push_byte(&mut self, byte: u8) {
175		self.write(&[byte]);
176	}
177}
178
179#[cfg(not(feature = "std"))]
180impl Output for Vec<u8> {
181	fn write(&mut self, bytes: &[u8]) {
182		self.extend_from_slice(bytes)
183	}
184}
185
186#[cfg(feature = "std")]
187impl<W: std::io::Write> Output for W {
188	fn write(&mut self, bytes: &[u8]) {
189		(self as &mut dyn std::io::Write).write_all(bytes).expect("Codec outputs are infallible");
190	}
191}
192
193
194/// !INTERNAL USE ONLY!
195///
196/// This enum provides type information to optimize encoding/decoding by doing fake specialization.
197#[doc(hidden)]
198#[non_exhaustive]
199pub enum TypeInfo {
200	/// Default value of [`Encode::TYPE_INFO`] to not require implementors to set this value in the trait.
201	Unknown,
202	U8,
203	I8,
204	U16,
205	I16,
206	U32,
207	I32,
208	U64,
209	I64,
210	U128,
211	I128,
212	F32,
213	F64,
214}
215
216/// Trait that allows zero-copy write of value-references to slices in LE format.
217///
218/// Implementations should override `using_encoded` for value types and `encode_to` and `size_hint` for allocating types.
219/// Wrapper types should override all methods.
220pub trait Encode {
221	// !INTERNAL USE ONLY!
222	// This const helps SCALE to optimize the encoding/decoding by doing fake specialization.
223	#[doc(hidden)]
224	const TYPE_INFO: TypeInfo = TypeInfo::Unknown;
225
226	/// If possible give a hint of expected size of the encoding.
227	///
228	/// This method is used inside default implementation of `encode`
229	/// to avoid re-allocations.
230	fn size_hint(&self) -> usize {
231		0
232	}
233
234	/// Convert self to a slice and append it to the destination.
235	fn encode_to<T: Output + ?Sized>(&self, dest: &mut T) {
236		self.using_encoded(|buf| dest.write(buf));
237	}
238
239	/// Convert self to an owned vector.
240	fn encode(&self) -> Vec<u8> {
241		let mut r = Vec::with_capacity(self.size_hint());
242		self.encode_to(&mut r);
243		r
244	}
245
246	/// Convert self to a slice and then invoke the given closure with it.
247	fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
248		f(&self.encode())
249	}
250
251	/// Calculates the encoded size.
252	///
253	/// Should be used when the encoded data isn't required.
254	///
255	/// # Note
256	///
257	/// This works by using a special [`Output`] that only tracks the size. So, there are no allocations inside the
258	/// output. However, this can not prevent allocations that some types are doing inside their own encoding.
259	fn encoded_size(&self) -> usize {
260		let mut size_tracker = SizeTracker { written: 0 };
261		self.encode_to(&mut size_tracker);
262		size_tracker.written
263	}
264}
265
266// Implements `Output` and only keeps track of the number of written bytes
267struct SizeTracker {
268	written: usize,
269}
270
271impl Output for SizeTracker {
272	fn write(&mut self, bytes: &[u8]) {
273		self.written += bytes.len();
274	}
275
276	fn push_byte(&mut self, _byte: u8) {
277		self.written += 1;
278	}
279}
280
281/// Trait that allows the length of a collection to be read, without having
282/// to read and decode the entire elements.
283pub trait DecodeLength {
284	/// Return the number of elements in `self_encoded`.
285	fn len(self_encoded: &[u8]) -> Result<usize, Error>;
286}
287
288/// Trait that allows zero-copy read of value-references from slices in LE format.
289pub trait Decode: Sized {
290	// !INTERNAL USE ONLY!
291	// This const helps SCALE to optimize the encoding/decoding by doing fake specialization.
292	#[doc(hidden)]
293	const TYPE_INFO: TypeInfo = TypeInfo::Unknown;
294
295	/// Attempt to deserialise the value from input.
296	fn decode<I: Input>(input: &mut I) -> Result<Self, Error>;
297
298	/// Attempt to deserialize the value from input into a pre-allocated piece of memory.
299	///
300	/// The default implementation will just call [`Decode::decode`].
301	///
302	/// # Safety
303	///
304	/// If this function returns `Ok` then `dst` **must** be properly initialized.
305	///
306	/// This is enforced by requiring the implementation to return a [`DecodeFinished`]
307	/// which can only be created by calling [`DecodeFinished::assert_decoding_finished`] which is `unsafe`.
308	fn decode_into<I: Input>(input: &mut I, dst: &mut MaybeUninit<Self>) -> Result<DecodeFinished, Error> {
309		let value = Self::decode(input)?;
310		dst.write(value);
311
312		// SAFETY: We've written the decoded value to `dst` so calling this is safe.
313		unsafe { Ok(DecodeFinished::assert_decoding_finished()) }
314	}
315
316	/// Attempt to skip the encoded value from input.
317	///
318	/// The default implementation of this function is just calling [`Decode::decode`].
319	/// When possible, an implementation should provide a specialized implementation.
320	fn skip<I: Input>(input: &mut I) -> Result<(), Error> {
321		Self::decode(input).map(|_| ())
322	}
323
324	/// Returns the fixed encoded size of the type.
325	///
326	/// If it returns `Some(size)` then all possible values of this
327	/// type have the given size (in bytes) when encoded.
328	///
329	/// NOTE: A type with a fixed encoded size may return `None`.
330	fn encoded_fixed_size() -> Option<usize> {
331		None
332	}
333}
334
335/// Trait that allows zero-copy read/write of value-references to/from slices in LE format.
336pub trait Codec: Decode + Encode {}
337impl<S: Decode + Encode> Codec for S {}
338
339/// Trait that bound `EncodeLike` along with `Encode`. Usefull for generic being used in function
340/// with `EncodeLike` parameters.
341pub trait FullEncode: Encode + EncodeLike {}
342impl<S: Encode + EncodeLike> FullEncode for S {}
343
344/// Trait that bound `EncodeLike` along with `Codec`. Usefull for generic being used in function
345/// with `EncodeLike` parameters.
346pub trait FullCodec: Decode + FullEncode {}
347impl<S: Decode + FullEncode> FullCodec for S {}
348
349/// A marker trait for types that wrap other encodable type.
350///
351/// Such types should not carry any additional information
352/// that would require to be encoded, because the encoding
353/// is assumed to be the same as the wrapped type.
354///
355/// The wrapped type that is referred to is the [`Deref::Target`].
356pub trait WrapperTypeEncode: Deref {}
357
358impl<T: ?Sized> WrapperTypeEncode for Box<T> {}
359impl<T: ?Sized + Encode> EncodeLike for Box<T> {}
360impl<T: Encode> EncodeLike<T> for Box<T> {}
361impl<T: Encode> EncodeLike<Box<T>> for T {}
362
363impl<T: ?Sized> WrapperTypeEncode for &T {}
364impl<T: ?Sized + Encode> EncodeLike for &T {}
365impl<T: Encode> EncodeLike<T> for &T {}
366impl<T: Encode> EncodeLike<&T> for T {}
367impl<T: Encode> EncodeLike<T> for &&T {}
368impl<T: Encode> EncodeLike<&&T> for T {}
369
370impl<T: ?Sized> WrapperTypeEncode for &mut T {}
371impl<T: ?Sized + Encode> EncodeLike for &mut T {}
372impl<T: Encode> EncodeLike<T> for &mut T {}
373impl<T: Encode> EncodeLike<&mut T> for T {}
374
375impl<'a, T: ToOwned + ?Sized> WrapperTypeEncode for Cow<'a, T> {}
376impl<'a, T: ToOwned + Encode + ?Sized> EncodeLike for Cow<'a, T> {}
377impl<'a, T: ToOwned + Encode> EncodeLike<T> for Cow<'a, T> {}
378impl<'a, T: ToOwned + Encode> EncodeLike<Cow<'a, T>> for T {}
379
380impl<T: ?Sized> WrapperTypeEncode for Rc<T> {}
381impl<T: ?Sized + Encode> EncodeLike for Rc<T> {}
382impl<T: Encode> EncodeLike<T> for Rc<T> {}
383impl<T: Encode> EncodeLike<Rc<T>> for T {}
384
385impl WrapperTypeEncode for String {}
386impl EncodeLike for String {}
387impl EncodeLike<&str> for String {}
388impl EncodeLike<String> for &str {}
389
390#[cfg(target_has_atomic = "ptr")]
391mod atomic_ptr_targets  {
392	use super::*;
393	impl<T: ?Sized> WrapperTypeEncode for Arc<T> {}
394	impl<T: ?Sized + Encode> EncodeLike for Arc<T> {}
395	impl<T: Encode> EncodeLike<T> for Arc<T> {}
396	impl<T: Encode> EncodeLike<Arc<T>> for T {}
397}
398
399#[cfg(feature = "bytes")]
400mod feature_wrapper_bytes {
401	use super::*;
402	use bytes::Bytes;
403
404	impl WrapperTypeEncode for Bytes {}
405	impl EncodeLike for Bytes {}
406	impl EncodeLike<&[u8]> for Bytes {}
407	impl EncodeLike<Vec<u8>> for Bytes {}
408	impl EncodeLike<Bytes> for &[u8] {}
409	impl EncodeLike<Bytes> for Vec<u8> {}
410}
411
412#[cfg(feature = "bytes")]
413struct BytesCursor {
414	bytes: bytes::Bytes,
415	position: usize
416}
417
418#[cfg(feature = "bytes")]
419impl Input for BytesCursor {
420	fn remaining_len(&mut self) -> Result<Option<usize>, Error> {
421		Ok(Some(self.bytes.len() - self.position))
422	}
423
424	fn read(&mut self, into: &mut [u8]) -> Result<(), Error> {
425		if into.len() > self.bytes.len() - self.position {
426			return Err("Not enough data to fill buffer".into())
427		}
428
429		into.copy_from_slice(&self.bytes[self.position..self.position + into.len()]);
430		self.position += into.len();
431		Ok(())
432	}
433
434	fn scale_internal_decode_bytes(&mut self) -> Result<bytes::Bytes, Error> {
435		let length = <Compact<u32>>::decode(self)?.0 as usize;
436
437		bytes::Buf::advance(&mut self.bytes, self.position);
438		self.position = 0;
439
440		if length > self.bytes.len() {
441			return Err("Not enough data to fill buffer".into());
442		}
443
444		Ok(self.bytes.split_to(length))
445	}
446}
447
448/// Decodes a given `T` from `Bytes`.
449#[cfg(feature = "bytes")]
450pub fn decode_from_bytes<T>(bytes: bytes::Bytes) -> Result<T, Error> where T: Decode {
451	// We could just use implement `Input` for `Bytes` and use `Bytes::split_to`
452	// to move the cursor, however doing it this way allows us to prevent an
453	// unnecessary allocation when the `T` which is being deserialized doesn't
454	// take advantage of the fact that it's being deserialized from `Bytes`.
455	//
456	// `Bytes` can be cheaply created from a `Vec<u8>`. It is both zero-copy
457	// *and* zero-allocation. However once you `.clone()` it or call `split_to()`
458	// an extra one-time allocation is triggered where the `Bytes` changes it's internal
459	// representation from essentially being a `Box<[u8]>` into being an `Arc<Box<[u8]>>`.
460	//
461	// If the `T` is `Bytes` or is a structure which contains `Bytes` in it then
462	// we don't really care, because this allocation will have to be made anyway.
463	//
464	// However, if `T` doesn't contain any `Bytes` then this extra allocation is
465	// technically unnecessary, and we can avoid it by tracking the position ourselves
466	// and treating the underlying `Bytes` as a fancy `&[u8]`.
467	let mut input = BytesCursor {
468		bytes,
469		position: 0
470	};
471	T::decode(&mut input)
472}
473
474#[cfg(feature = "bytes")]
475impl Decode for bytes::Bytes {
476	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
477		input.scale_internal_decode_bytes()
478	}
479}
480
481impl<T, X> Encode for X where
482	T: Encode + ?Sized,
483	X: WrapperTypeEncode<Target = T>,
484{
485	fn size_hint(&self) -> usize {
486		(**self).size_hint()
487	}
488
489	fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
490		(**self).using_encoded(f)
491	}
492
493	fn encode(&self) -> Vec<u8> {
494		(**self).encode()
495	}
496
497	fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
498		(**self).encode_to(dest)
499	}
500}
501
502/// A marker trait for types that can be created solely from other decodable types.
503///
504/// The decoding of such type is assumed to be the same as the wrapped type.
505pub trait WrapperTypeDecode: Sized {
506	/// A wrapped type.
507	type Wrapped: Into<Self>;
508
509	// !INTERNAL USE ONLY!
510	// This is a used to specialize `decode` for the wrapped type.
511	#[doc(hidden)]
512	#[inline]
513	fn decode_wrapped<I: Input>(input: &mut I) -> Result<Self, Error> where Self::Wrapped: Decode {
514		input.descend_ref()?;
515		let result = Ok(Self::Wrapped::decode(input)?.into());
516		input.ascend_ref();
517		result
518	}
519}
520
521impl<T> WrapperTypeDecode for Box<T> {
522	type Wrapped = T;
523
524	fn decode_wrapped<I: Input>(input: &mut I) -> Result<Self, Error> where Self::Wrapped: Decode {
525		input.descend_ref()?;
526
527		// Placement new is not yet stable, but we can just manually allocate a chunk of memory
528		// and convert it to a `Box` ourselves.
529		//
530		// The explicit types here are written out for clarity.
531		//
532		// TODO: Use `Box::new_uninit` once that's stable.
533		let layout = core::alloc::Layout::new::<MaybeUninit<T>>();
534
535		let ptr: *mut MaybeUninit<T> = if layout.size() == 0 {
536			core::ptr::NonNull::dangling().as_ptr()
537		} else {
538
539			// SAFETY: Layout has a non-zero size so calling this is safe.
540			let ptr: *mut u8 = unsafe {
541				crate::alloc::alloc::alloc(layout)
542			};
543
544			if ptr.is_null() {
545				crate::alloc::alloc::handle_alloc_error(layout);
546			}
547
548			ptr.cast()
549		};
550
551		// SAFETY: Constructing a `Box` from a piece of memory allocated with `std::alloc::alloc`
552		//         is explicitly allowed as long as it was allocated with the global allocator
553		//         and the memory layout matches.
554		//
555		//         Constructing a `Box` from `NonNull::dangling` is also always safe as long
556		//         as the underlying type is zero-sized.
557		let mut boxed: Box<MaybeUninit<T>> = unsafe { Box::from_raw(ptr) };
558
559		T::decode_into(input, &mut boxed)?;
560
561		// Decoding succeeded, so let's get rid of `MaybeUninit`.
562		//
563		// TODO: Use `Box::assume_init` once that's stable.
564		let ptr: *mut MaybeUninit<T> = Box::into_raw(boxed);
565		let ptr: *mut T = ptr.cast();
566
567		// SAFETY: `MaybeUninit` doesn't affect the memory layout, so casting the pointer back
568		//         into a `Box` is safe.
569		let boxed: Box<T> = unsafe { Box::from_raw(ptr) };
570
571		input.ascend_ref();
572		Ok(boxed)
573	}
574}
575
576impl<T> WrapperTypeDecode for Rc<T> {
577	type Wrapped = T;
578
579	fn decode_wrapped<I: Input>(input: &mut I) -> Result<Self, Error> where Self::Wrapped: Decode {
580		// TODO: This is inefficient; use `Rc::new_uninit` once that's stable.
581		Box::<T>::decode(input).map(|output| output.into())
582	}
583}
584
585#[cfg(target_has_atomic = "ptr")]
586impl<T> WrapperTypeDecode for Arc<T> {
587	type Wrapped = T;
588
589	fn decode_wrapped<I: Input>(input: &mut I) -> Result<Self, Error> where Self::Wrapped: Decode {
590		// TODO: This is inefficient; use `Arc::new_uninit` once that's stable.
591		Box::<T>::decode(input).map(|output| output.into())
592	}
593}
594
595impl<T, X> Decode for X where
596	T: Decode + Into<X>,
597	X: WrapperTypeDecode<Wrapped=T>,
598{
599	#[inline]
600	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
601		Self::decode_wrapped(input)
602	}
603}
604
605/// A macro that matches on a [`TypeInfo`] and expands a given macro per variant.
606///
607/// The first parameter to the given macro will be the type of variant (e.g. `u8`, `u32`, etc.) and other parameters
608/// given to this macro.
609///
610/// The last parameter is the code that should be executed for the `Unknown` type info.
611macro_rules! with_type_info {
612	( $type_info:expr, $macro:ident $( ( $( $params:ident ),* ) )?, { $( $unknown_variant:tt )* }, ) => {
613		match $type_info {
614			TypeInfo::U8 => { $macro!(u8 $( $( , $params )* )? ) },
615			TypeInfo::I8 => { $macro!(i8 $( $( , $params )* )? ) },
616			TypeInfo::U16 => { $macro!(u16 $( $( , $params )* )? ) },
617			TypeInfo::I16 => { $macro!(i16 $( $( , $params )* )? ) },
618			TypeInfo::U32 => { $macro!(u32 $( $( , $params )* )? ) },
619			TypeInfo::I32 => { $macro!(i32 $( $( , $params )* )? ) },
620			TypeInfo::U64 => { $macro!(u64 $( $( , $params )* )? ) },
621			TypeInfo::I64 => { $macro!(i64 $( $( , $params )* )? ) },
622			TypeInfo::U128 => { $macro!(u128 $( $( , $params )* )? ) },
623			TypeInfo::I128 => { $macro!(i128 $( $( , $params )* )? ) },
624			TypeInfo::Unknown => { $( $unknown_variant )* },
625			TypeInfo::F32 => { $macro!(f32 $( $( , $params )* )? ) },
626			TypeInfo::F64 => { $macro!(f64 $( $( , $params )* )? ) },
627		}
628	};
629}
630
631/// Something that can be encoded as a reference.
632pub trait EncodeAsRef<'a, T: 'a> {
633	/// The reference type that is used for encoding.
634	type RefType: Encode + From<&'a T>;
635}
636
637impl<T: Encode, E: Encode> Encode for Result<T, E> {
638	fn size_hint(&self) -> usize {
639		1 + match *self {
640			Ok(ref t) => t.size_hint(),
641			Err(ref t) => t.size_hint(),
642		}
643	}
644
645	fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
646		match *self {
647			Ok(ref t) => {
648				dest.push_byte(0);
649				t.encode_to(dest);
650			}
651			Err(ref e) => {
652				dest.push_byte(1);
653				e.encode_to(dest);
654			}
655		}
656	}
657}
658
659impl<T, LikeT, E, LikeE> EncodeLike<Result<LikeT, LikeE>> for Result<T, E>
660where
661	T: EncodeLike<LikeT>,
662	LikeT: Encode,
663	E: EncodeLike<LikeE>,
664	LikeE: Encode,
665{}
666
667impl<T: Decode, E: Decode> Decode for Result<T, E> {
668	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
669		match input.read_byte()
670			.map_err(|e| e.chain("Could not result variant byte for `Result`"))?
671		{
672			0 => Ok(
673				Ok(T::decode(input).map_err(|e| e.chain("Could not Decode `Result::Ok(T)`"))?)
674			),
675			1 => Ok(
676				Err(E::decode(input).map_err(|e| e.chain("Could not decode `Result::Error(E)`"))?)
677			),
678			_ => Err("unexpected first byte decoding Result".into()),
679		}
680	}
681}
682
683/// Shim type because we can't do a specialised implementation for `Option<bool>` directly.
684#[derive(Eq, PartialEq, Clone, Copy)]
685pub struct OptionBool(pub Option<bool>);
686
687impl fmt::Debug for OptionBool {
688	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
689		self.0.fmt(f)
690	}
691}
692
693impl Encode for OptionBool {
694	fn size_hint(&self) -> usize {
695		1
696	}
697
698	fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
699		f(&[match *self {
700			OptionBool(None) => 0u8,
701			OptionBool(Some(true)) => 1u8,
702			OptionBool(Some(false)) => 2u8,
703		}])
704	}
705}
706
707impl EncodeLike for OptionBool {}
708
709impl Decode for OptionBool {
710	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
711		match input.read_byte()? {
712			0 => Ok(OptionBool(None)),
713			1 => Ok(OptionBool(Some(true))),
714			2 => Ok(OptionBool(Some(false))),
715			_ => Err("unexpected first byte decoding OptionBool".into()),
716		}
717	}
718}
719
720impl<T: EncodeLike<U>, U: Encode> EncodeLike<Option<U>> for Option<T> {}
721
722impl<T: Encode> Encode for Option<T> {
723	fn size_hint(&self) -> usize {
724		1 + match *self {
725			Some(ref t) => t.size_hint(),
726			None => 0,
727		}
728	}
729
730	fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
731		match *self {
732			Some(ref t) => {
733				dest.push_byte(1);
734				t.encode_to(dest);
735			}
736			None => dest.push_byte(0),
737		}
738	}
739}
740
741impl<T: Decode> Decode for Option<T> {
742	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
743		match input.read_byte()
744			.map_err(|e| e.chain("Could not decode variant byte for `Option`"))?
745		{
746			0 => Ok(None),
747			1 => Ok(
748				Some(T::decode(input).map_err(|e| e.chain("Could not decode `Option::Some(T)`"))?)
749			),
750			_ => Err("unexpected first byte decoding Option".into()),
751		}
752	}
753}
754
755macro_rules! impl_for_non_zero {
756	( $( $name:ty ),* $(,)? ) => {
757		$(
758			impl Encode for $name {
759				fn size_hint(&self) -> usize {
760					self.get().size_hint()
761				}
762
763				fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
764					self.get().encode_to(dest)
765				}
766
767				fn encode(&self) -> Vec<u8> {
768					self.get().encode()
769				}
770
771				fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
772					self.get().using_encoded(f)
773				}
774			}
775
776			impl EncodeLike for $name {}
777
778			impl Decode for $name {
779				fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
780					Self::new(Decode::decode(input)?)
781						.ok_or_else(|| Error::from("cannot create non-zero number from 0"))
782				}
783			}
784		)*
785	}
786}
787
788/// Encode the slice without prepending the len.
789///
790/// This is equivalent to encoding all the element one by one, but it is optimized for some types.
791pub(crate) fn encode_slice_no_len<T: Encode, W: Output + ?Sized>(slice: &[T], dest: &mut W) {
792	macro_rules! encode_to {
793		( u8, $slice:ident, $dest:ident ) => {{
794			let typed = unsafe { mem::transmute::<&[T], &[u8]>(&$slice[..]) };
795			$dest.write(&typed)
796		}};
797		( i8, $slice:ident, $dest:ident ) => {{
798			// `i8` has the same size as `u8`. We can just convert it here and write to the
799			// dest buffer directly.
800			let typed = unsafe { mem::transmute::<&[T], &[u8]>(&$slice[..]) };
801			$dest.write(&typed)
802		}};
803		( $ty:ty, $slice:ident, $dest:ident ) => {{
804			if cfg!(target_endian = "little") {
805				let typed = unsafe { mem::transmute::<&[T], &[$ty]>(&$slice[..]) };
806				$dest.write(<[$ty] as AsByteSlice<$ty>>::as_byte_slice(typed))
807			} else {
808				for item in $slice.iter() {
809					item.encode_to(dest);
810				}
811			}
812		}};
813	}
814
815	with_type_info! {
816		<T as Encode>::TYPE_INFO,
817		encode_to(slice, dest),
818		{
819			for item in slice.iter() {
820				item.encode_to(dest);
821			}
822		},
823	}
824}
825
826/// Decode the vec (without a prepended len).
827///
828/// This is equivalent to decode all elements one by one, but it is optimized in some
829/// situation.
830pub fn decode_vec_with_len<T: Decode, I: Input>(
831	input: &mut I,
832	len: usize,
833) -> Result<Vec<T>, Error> {
834	fn decode_unoptimized<I: Input, T: Decode>(
835		input: &mut I,
836		items_len: usize,
837	) -> Result<Vec<T>, Error> {
838		let input_capacity = input.remaining_len()?
839			.unwrap_or(MAX_PREALLOCATION)
840			.checked_div(mem::size_of::<T>())
841			.unwrap_or(0);
842		let mut r = Vec::with_capacity(input_capacity.min(items_len));
843		input.descend_ref()?;
844		for _ in 0..items_len {
845			r.push(T::decode(input)?);
846		}
847		input.ascend_ref();
848		Ok(r)
849	}
850
851	macro_rules! decode {
852		( $ty:ty, $input:ident, $len:ident ) => {{
853			if cfg!(target_endian = "little") || mem::size_of::<T>() == 1 {
854				let vec = read_vec_from_u8s::<_, $ty>($input, $len)?;
855				Ok(unsafe { mem::transmute::<Vec<$ty>, Vec<T>>(vec) })
856			} else {
857				decode_unoptimized($input, $len)
858			}
859		}};
860	}
861
862	with_type_info! {
863		<T as Decode>::TYPE_INFO,
864		decode(input, len),
865		{
866			decode_unoptimized(input, len)
867		},
868	}
869}
870
871impl_for_non_zero! {
872	NonZeroI8,
873	NonZeroI16,
874	NonZeroI32,
875	NonZeroI64,
876	NonZeroI128,
877	NonZeroU8,
878	NonZeroU16,
879	NonZeroU32,
880	NonZeroU64,
881	NonZeroU128,
882}
883
884impl<T: Encode, const N: usize> Encode for [T; N] {
885	fn size_hint(&self) -> usize {
886		mem::size_of::<T>() * N
887	}
888
889	fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
890		encode_slice_no_len(&self[..], dest)
891	}
892}
893
894const fn calculate_array_bytesize<T, const N: usize>() -> usize {
895	struct AssertNotOverflow<T, const N: usize>(PhantomData<T>);
896
897	impl<T, const N: usize> AssertNotOverflow<T, N> {
898		const OK: () = assert!(mem::size_of::<T>().checked_mul(N).is_some(), "array size overflow");
899	}
900
901	#[allow(clippy::let_unit_value)]
902	let () = AssertNotOverflow::<T, N>::OK;
903	mem::size_of::<T>() * N
904}
905
906impl<T: Decode, const N: usize> Decode for [T; N] {
907	#[inline(always)]
908	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
909		let mut array = MaybeUninit::uninit();
910		Self::decode_into(input, &mut array)?;
911
912		// SAFETY: `decode_into` succeeded, so the array is initialized.
913		unsafe {
914			Ok(array.assume_init())
915		}
916	}
917
918    fn decode_into<I: Input>(input: &mut I, dst: &mut MaybeUninit<Self>) -> Result<DecodeFinished, Error> {
919		let is_primitive = match <T as Decode>::TYPE_INFO {
920			| TypeInfo::U8
921			| TypeInfo::I8
922				=> true,
923			| TypeInfo::U16
924			| TypeInfo::I16
925			| TypeInfo::U32
926			| TypeInfo::I32
927			| TypeInfo::U64
928			| TypeInfo::I64
929			| TypeInfo::U128
930			| TypeInfo::I128
931			| TypeInfo::F32
932			| TypeInfo::F64
933				=> cfg!(target_endian = "little"),
934			TypeInfo::Unknown => false
935		};
936
937		if is_primitive {
938			// Let's read the array in bulk as that's going to be a lot
939			// faster than just reading each element one-by-one.
940
941			let ptr: *mut [T; N] = dst.as_mut_ptr();
942			let ptr: *mut u8 = ptr.cast();
943
944			let bytesize = calculate_array_bytesize::<T, N>();
945
946			// TODO: This is potentially slow; it'd be better if `Input` supported
947			//       reading directly into uninitialized memory.
948			//
949			// SAFETY: The pointer is valid and points to a memory `bytesize` bytes big.
950			unsafe {
951				ptr.write_bytes(0, bytesize);
952			}
953
954			// SAFETY: We've zero-initialized everything so creating a slice here is safe.
955			let slice: &mut [u8] = unsafe {
956				core::slice::from_raw_parts_mut(ptr, bytesize)
957			};
958
959			input.read(slice)?;
960
961			// SAFETY: We've initialized the whole slice so calling this is safe.
962			unsafe {
963				return Ok(DecodeFinished::assert_decoding_finished());
964			}
965		}
966
967		let slice: &mut [MaybeUninit<T>; N] = {
968			let ptr: *mut [T; N] = dst.as_mut_ptr();
969			let ptr: *mut [MaybeUninit<T>; N] = ptr.cast();
970			// SAFETY: Casting `&mut MaybeUninit<[T; N]>` into `&mut [MaybeUninit<T>; N]` is safe.
971			unsafe { &mut *ptr }
972		};
973
974		/// A wrapper type to make sure the partially read elements are always
975		/// dropped in case an error occurs or the underlying `decode` implementation panics.
976		struct State<'a, T, const N: usize> {
977			count: usize,
978			slice: &'a mut [MaybeUninit<T>; N]
979		}
980
981		impl<'a, T, const N: usize> Drop for State<'a, T, N> {
982			fn drop(&mut self) {
983				if !mem::needs_drop::<T>() {
984					// If the types don't actually need to be dropped then don't even
985					// try to run the loop below.
986					//
987					// Most likely won't make a difference in release mode, but will
988					// make a difference in debug mode.
989					return;
990				}
991
992				// TODO: Use `MaybeUninit::slice_assume_init_mut` + `core::ptr::drop_in_place`
993				//       once `slice_assume_init_mut` is stable.
994				for item in &mut self.slice[..self.count] {
995					// SAFETY: Each time we've read a new element we incremented `count`,
996					//         and we only drop at most `count` elements here,
997					//         so all of the elements we drop here are valid.
998					unsafe {
999						item.assume_init_drop();
1000					}
1001				}
1002			}
1003		}
1004
1005		let mut state = State {
1006			count: 0,
1007			slice
1008		};
1009
1010		while state.count < state.slice.len() {
1011			T::decode_into(input, &mut state.slice[state.count])?;
1012			state.count += 1;
1013		}
1014
1015		// We've successfully read everything, so disarm the `Drop` impl.
1016		mem::forget(state);
1017
1018		// SAFETY: We've initialized the whole slice so calling this is safe.
1019		unsafe {
1020			Ok(DecodeFinished::assert_decoding_finished())
1021		}
1022	}
1023
1024	fn skip<I: Input>(input: &mut I) -> Result<(), Error> {
1025		if Self::encoded_fixed_size().is_some() {
1026			// Should skip the bytes, but Input does not support skip.
1027			for _ in 0..N {
1028				T::skip(input)?;
1029			}
1030		} else {
1031		    Self::decode(input)?;
1032		}
1033		Ok(())
1034	}
1035
1036	fn encoded_fixed_size() -> Option<usize> {
1037		Some(<T as Decode>::encoded_fixed_size()? * N)
1038	}
1039}
1040
1041impl<T: EncodeLike<U>, U: Encode, const N: usize> EncodeLike<[U; N]> for [T; N] {}
1042
1043impl Encode for str {
1044	fn size_hint(&self) -> usize {
1045		self.as_bytes().size_hint()
1046	}
1047
1048	fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
1049		self.as_bytes().encode_to(dest)
1050	}
1051
1052	fn encode(&self) -> Vec<u8> {
1053		self.as_bytes().encode()
1054	}
1055
1056	fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
1057		self.as_bytes().using_encoded(f)
1058	}
1059}
1060
1061impl<'a, T: ToOwned + ?Sized> Decode for Cow<'a, T>
1062	where <T as ToOwned>::Owned: Decode,
1063{
1064	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1065		Ok(Cow::Owned(Decode::decode(input)?))
1066	}
1067}
1068
1069impl<T> EncodeLike for PhantomData<T> {}
1070
1071impl<T> Encode for PhantomData<T> {
1072	fn encode_to<W: Output + ?Sized>(&self, _dest: &mut W) {}
1073}
1074
1075impl<T> Decode for PhantomData<T> {
1076	fn decode<I: Input>(_input: &mut I) -> Result<Self, Error> {
1077		Ok(PhantomData)
1078	}
1079}
1080
1081impl Decode for String {
1082	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1083		Self::from_utf8(Vec::decode(input)?).map_err(|_| "Invalid utf8 sequence".into())
1084	}
1085}
1086
1087/// Writes the compact encoding of `len` do `dest`.
1088pub(crate) fn compact_encode_len_to<W: Output + ?Sized>(dest: &mut W, len: usize) -> Result<(), Error> {
1089	if len > u32::MAX as usize {
1090		return Err("Attempted to serialize a collection with too many elements.".into());
1091	}
1092
1093	Compact(len as u32).encode_to(dest);
1094	Ok(())
1095}
1096
1097impl<T: Encode> Encode for [T] {
1098	fn size_hint(&self) -> usize {
1099		mem::size_of::<u32>() + mem::size_of_val(self)
1100	}
1101
1102	fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
1103		compact_encode_len_to(dest, self.len()).expect("Compact encodes length");
1104
1105		encode_slice_no_len(self, dest)
1106	}
1107}
1108
1109/// Create a `Vec<T>` by casting directly from a buffer of read `u8`s
1110///
1111/// The encoding of `T` must be equal to its binary representation, and size of `T` must be less or
1112/// equal to [`MAX_PREALLOCATION`].
1113pub(crate) fn read_vec_from_u8s<I, T>(input: &mut I, items_len: usize) -> Result<Vec<T>, Error>
1114where
1115	I: Input,
1116	T: ToMutByteSlice + Default + Clone,
1117{
1118	debug_assert!(MAX_PREALLOCATION >= mem::size_of::<T>(), "Invalid precondition");
1119
1120	let byte_len = items_len.checked_mul(mem::size_of::<T>())
1121		.ok_or("Item is too big and cannot be allocated")?;
1122
1123	let input_len = input.remaining_len()?;
1124
1125	// If there is input len and it cannot be pre-allocated then return directly.
1126	if input_len.map(|l| l < byte_len).unwrap_or(false) {
1127		return Err("Not enough data to decode vector".into())
1128	}
1129
1130	// In both these branches we're going to be creating and resizing a Vec<T>,
1131	// but casting it to a &mut [u8] for reading.
1132
1133	// Note: we checked that if input_len is some then it can preallocated.
1134	let r = if input_len.is_some() || byte_len < MAX_PREALLOCATION {
1135		// Here we pre-allocate the whole buffer.
1136		let mut items: Vec<T> = vec![Default::default(); items_len];
1137		let bytes_slice = items.as_mut_byte_slice();
1138		input.read(bytes_slice)?;
1139
1140		items
1141	} else {
1142		// An allowed number of preallocated item.
1143		// Note: `MAX_PREALLOCATION` is expected to be more or equal to size of `T`, precondition.
1144		let max_preallocated_items = MAX_PREALLOCATION / mem::size_of::<T>();
1145
1146		// Here we pre-allocate only the maximum pre-allocation
1147		let mut items: Vec<T> = vec![];
1148
1149		let mut items_remains = items_len;
1150
1151		while items_remains > 0 {
1152			let items_len_read = max_preallocated_items.min(items_remains);
1153
1154			let items_len_filled = items.len();
1155			let items_new_size = items_len_filled + items_len_read;
1156
1157			items.reserve_exact(items_len_read);
1158			unsafe {
1159				items.set_len(items_new_size);
1160			}
1161
1162			let bytes_slice = items.as_mut_byte_slice();
1163			let bytes_len_filled = items_len_filled * mem::size_of::<T>();
1164			input.read(&mut bytes_slice[bytes_len_filled..])?;
1165
1166			items_remains = items_remains.saturating_sub(items_len_read);
1167		}
1168
1169		items
1170	};
1171
1172	Ok(r)
1173}
1174
1175impl<T> WrapperTypeEncode for Vec<T> {}
1176impl<T: EncodeLike<U>, U: Encode> EncodeLike<Vec<U>> for Vec<T> {}
1177impl<T: EncodeLike<U>, U: Encode> EncodeLike<&[U]> for Vec<T> {}
1178impl<T: EncodeLike<U>, U: Encode> EncodeLike<Vec<U>> for &[T] {}
1179
1180impl<T: Decode> Decode for Vec<T> {
1181	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1182		<Compact<u32>>::decode(input).and_then(move |Compact(len)| {
1183			decode_vec_with_len(input, len as usize)
1184		})
1185	}
1186}
1187
1188macro_rules! impl_codec_through_iterator {
1189	($(
1190		$type:ident
1191		{ $( $generics:ident $( : $decode_additional:ident )? ),* }
1192		{ $( $type_like_generics:ident ),* }
1193		{ $( $impl_like_generics:tt )* }
1194	)*) => {$(
1195		impl<$( $generics: Encode ),*> Encode for $type<$( $generics, )*> {
1196			fn size_hint(&self) -> usize {
1197				mem::size_of::<u32>() $( + mem::size_of::<$generics>() * self.len() )*
1198			}
1199
1200			fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
1201				compact_encode_len_to(dest, self.len()).expect("Compact encodes length");
1202
1203				for i in self.iter() {
1204					i.encode_to(dest);
1205				}
1206			}
1207		}
1208
1209		impl<$( $generics: Decode $( + $decode_additional )? ),*> Decode
1210			for $type<$( $generics, )*>
1211		{
1212			fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1213				<Compact<u32>>::decode(input).and_then(move |Compact(len)| {
1214					input.descend_ref()?;
1215					let result = Result::from_iter((0..len).map(|_| Decode::decode(input)));
1216					input.ascend_ref();
1217					result
1218				})
1219			}
1220		}
1221
1222		impl<$( $impl_like_generics )*> EncodeLike<$type<$( $type_like_generics ),*>>
1223			for $type<$( $generics ),*> {}
1224		impl<$( $impl_like_generics )*> EncodeLike<&[( $( $type_like_generics, )* )]>
1225			for $type<$( $generics ),*> {}
1226		impl<$( $impl_like_generics )*> EncodeLike<$type<$( $type_like_generics ),*>>
1227			for &[( $( $generics, )* )] {}
1228	)*}
1229}
1230
1231impl_codec_through_iterator! {
1232	BTreeMap { K: Ord, V } { LikeK, LikeV}
1233		{ K: EncodeLike<LikeK>, LikeK: Encode, V: EncodeLike<LikeV>, LikeV: Encode }
1234	BTreeSet { T: Ord } { LikeT }
1235		{ T: EncodeLike<LikeT>, LikeT: Encode }
1236	LinkedList { T } { LikeT }
1237		{ T: EncodeLike<LikeT>, LikeT: Encode }
1238	BinaryHeap { T: Ord } { LikeT }
1239		{ T: EncodeLike<LikeT>, LikeT: Encode }
1240}
1241
1242impl<T: Encode> EncodeLike for VecDeque<T> {}
1243impl<T: EncodeLike<U>, U: Encode> EncodeLike<&[U]> for VecDeque<T> {}
1244impl<T: EncodeLike<U>, U: Encode> EncodeLike<VecDeque<U>> for &[T] {}
1245impl<T: EncodeLike<U>, U: Encode> EncodeLike<Vec<U>> for VecDeque<T> {}
1246impl<T: EncodeLike<U>, U: Encode> EncodeLike<VecDeque<U>> for Vec<T> {}
1247
1248impl<T: Encode> Encode for VecDeque<T> {
1249	fn size_hint(&self) -> usize {
1250		mem::size_of::<u32>() + mem::size_of::<T>() * self.len()
1251	}
1252
1253	fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
1254		compact_encode_len_to(dest, self.len()).expect("Compact encodes length");
1255
1256		macro_rules! encode_to {
1257			( $ty:ty, $self:ident, $dest:ident ) => {{
1258				if cfg!(target_endian = "little") || mem::size_of::<T>() == 1 {
1259					let slices = $self.as_slices();
1260					let typed = unsafe {
1261						core::mem::transmute::<(&[T], &[T]), (&[$ty], &[$ty])>(slices)
1262					};
1263
1264					$dest.write(<[$ty] as AsByteSlice<$ty>>::as_byte_slice(typed.0));
1265					$dest.write(<[$ty] as AsByteSlice<$ty>>::as_byte_slice(typed.1));
1266				} else {
1267					for item in $self {
1268						item.encode_to($dest);
1269					}
1270				}
1271			}};
1272		}
1273
1274		with_type_info! {
1275			<T as Encode>::TYPE_INFO,
1276			encode_to(self, dest),
1277			{
1278				for item in self {
1279					item.encode_to(dest);
1280				}
1281			},
1282		}
1283	}
1284}
1285
1286impl<T: Decode> Decode for VecDeque<T> {
1287	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1288		Ok(<Vec<T>>::decode(input)?.into())
1289	}
1290}
1291
1292impl EncodeLike for () {}
1293
1294impl Encode for () {
1295	fn encode_to<W: Output + ?Sized>(&self, _dest: &mut W) {
1296	}
1297
1298	fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
1299		f(&[])
1300	}
1301
1302	fn encode(&self) -> Vec<u8> {
1303		Vec::new()
1304	}
1305}
1306
1307impl Decode for () {
1308	fn decode<I: Input>(_: &mut I) -> Result<(), Error> {
1309		Ok(())
1310	}
1311}
1312
1313macro_rules! impl_len {
1314	( $( $type:ident< $($g:ident),* > ),* ) => { $(
1315		impl<$($g),*> DecodeLength for $type<$($g),*> {
1316			fn len(mut self_encoded: &[u8]) -> Result<usize, Error> {
1317				usize::try_from(u32::from(Compact::<u32>::decode(&mut self_encoded)?))
1318					.map_err(|_| "Failed convert decoded size into usize.".into())
1319			}
1320		}
1321	)*}
1322}
1323
1324// Collection types that support compact decode length.
1325impl_len!(Vec<T>, BTreeSet<T>, BTreeMap<K, V>, VecDeque<T>, BinaryHeap<T>, LinkedList<T>);
1326
1327macro_rules! tuple_impl {
1328	(
1329		($one:ident, $extra:ident),
1330	) => {
1331		impl<$one: Encode> Encode for ($one,) {
1332			fn size_hint(&self) -> usize {
1333				self.0.size_hint()
1334			}
1335
1336			fn encode_to<T: Output + ?Sized>(&self, dest: &mut T) {
1337				self.0.encode_to(dest);
1338			}
1339
1340			fn encode(&self) -> Vec<u8> {
1341				self.0.encode()
1342			}
1343
1344			fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
1345				self.0.using_encoded(f)
1346			}
1347		}
1348
1349		impl<$one: Decode> Decode for ($one,) {
1350			fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1351				match $one::decode(input) {
1352					Err(e) => Err(e),
1353					Ok($one) => Ok(($one,)),
1354				}
1355			}
1356		}
1357
1358		impl<$one: DecodeLength> DecodeLength for ($one,) {
1359			fn len(self_encoded: &[u8]) -> Result<usize, Error> {
1360				$one::len(self_encoded)
1361			}
1362		}
1363
1364		impl<$one: EncodeLike<$extra>, $extra: Encode> crate::EncodeLike<($extra,)> for ($one,) {}
1365	};
1366	(($first:ident, $fextra:ident), $( ( $rest:ident, $rextra:ident ), )+) => {
1367		impl<$first: Encode, $($rest: Encode),+> Encode for ($first, $($rest),+) {
1368			fn size_hint(&self) -> usize {
1369				let (
1370					ref $first,
1371					$(ref $rest),+
1372				) = *self;
1373				$first.size_hint()
1374				$( + $rest.size_hint() )+
1375			}
1376
1377			fn encode_to<T: Output + ?Sized>(&self, dest: &mut T) {
1378				let (
1379					ref $first,
1380					$(ref $rest),+
1381				) = *self;
1382
1383				$first.encode_to(dest);
1384				$($rest.encode_to(dest);)+
1385			}
1386		}
1387
1388		impl<$first: Decode, $($rest: Decode),+> Decode for ($first, $($rest),+) {
1389			fn decode<INPUT: Input>(input: &mut INPUT) -> Result<Self, super::Error> {
1390				Ok((
1391					match $first::decode(input) {
1392						Ok(x) => x,
1393						Err(e) => return Err(e),
1394					},
1395					$(match $rest::decode(input) {
1396						Ok(x) => x,
1397						Err(e) => return Err(e),
1398					},)+
1399				))
1400			}
1401		}
1402
1403		impl<$first: EncodeLike<$fextra>, $fextra: Encode,
1404			$($rest: EncodeLike<$rextra>, $rextra: Encode),+> crate::EncodeLike<($fextra, $( $rextra ),+)>
1405			for ($first, $($rest),+) {}
1406
1407		impl<$first: DecodeLength, $($rest),+> DecodeLength for ($first, $($rest),+) {
1408			fn len(self_encoded: &[u8]) -> Result<usize, Error> {
1409				$first::len(self_encoded)
1410			}
1411		}
1412
1413		tuple_impl!( $( ($rest, $rextra), )+ );
1414	}
1415}
1416
1417#[allow(non_snake_case)]
1418mod inner_tuple_impl {
1419	use super::*;
1420
1421	tuple_impl!(
1422		(A0, A1), (B0, B1), (C0, C1), (D0, D1), (E0, E1), (F0, F1), (G0, G1), (H0, H1), (I0, I1),
1423		(J0, J1), (K0, K1), (L0, L1), (M0, M1), (N0, N1), (O0, O1), (P0, P1), (Q0, Q1), (R0, R1),
1424	);
1425}
1426
1427macro_rules! impl_endians {
1428	( $( $t:ty; $ty_info:ident ),* ) => { $(
1429		impl EncodeLike for $t {}
1430
1431		impl Encode for $t {
1432			const TYPE_INFO: TypeInfo = TypeInfo::$ty_info;
1433
1434			fn size_hint(&self) -> usize {
1435				mem::size_of::<$t>()
1436			}
1437
1438			fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
1439				let buf = self.to_le_bytes();
1440				f(&buf[..])
1441			}
1442		}
1443
1444		impl Decode for $t {
1445			const TYPE_INFO: TypeInfo = TypeInfo::$ty_info;
1446
1447			fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1448				let mut buf = [0u8; mem::size_of::<$t>()];
1449				input.read(&mut buf)?;
1450				Ok(<$t>::from_le_bytes(buf))
1451			}
1452
1453			fn encoded_fixed_size() -> Option<usize> {
1454				Some(mem::size_of::<$t>())
1455			}
1456		}
1457	)* }
1458}
1459macro_rules! impl_one_byte {
1460	( $( $t:ty; $ty_info:ident ),* ) => { $(
1461		impl EncodeLike for $t {}
1462
1463		impl Encode for $t {
1464			const TYPE_INFO: TypeInfo = TypeInfo::$ty_info;
1465
1466			fn size_hint(&self) -> usize {
1467				mem::size_of::<$t>()
1468			}
1469
1470			fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
1471				f(&[*self as u8][..])
1472			}
1473		}
1474
1475		impl Decode for $t {
1476			const TYPE_INFO: TypeInfo = TypeInfo::$ty_info;
1477
1478			fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1479				Ok(input.read_byte()? as $t)
1480			}
1481		}
1482	)* }
1483}
1484
1485impl_endians!(u16; U16, u32; U32, u64; U64, u128; U128, i16; I16, i32; I32, i64; I64, i128; I128);
1486impl_one_byte!(u8; U8, i8; I8);
1487
1488impl_endians!(f32; F32, f64; F64);
1489
1490impl EncodeLike for bool {}
1491
1492impl Encode for bool {
1493	fn size_hint(&self) -> usize {
1494		mem::size_of::<bool>()
1495	}
1496
1497	fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
1498		f(&[*self as u8][..])
1499	}
1500}
1501
1502impl Decode for bool {
1503	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1504		let byte = input.read_byte()?;
1505		match byte {
1506			0 => Ok(false),
1507			1 => Ok(true),
1508			_ => Err("Invalid boolean representation".into())
1509		}
1510	}
1511
1512	fn encoded_fixed_size() -> Option<usize> {
1513		Some(1)
1514	}
1515}
1516
1517impl Encode for Duration {
1518	fn size_hint(&self) -> usize {
1519		mem::size_of::<u64>() + mem::size_of::<u32>()
1520	}
1521
1522	fn encode(&self) -> Vec<u8> {
1523		let secs = self.as_secs();
1524		let nanos = self.subsec_nanos();
1525		(secs, nanos).encode()
1526	}
1527}
1528
1529impl Decode for Duration {
1530	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1531		let (secs, nanos) = <(u64, u32)>::decode(input)
1532			.map_err(|e| e.chain("Could not decode `Duration(u64, u32)`"))?;
1533		if nanos >= A_BILLION {
1534			Err("Could not decode `Duration`: Number of nanoseconds should not be higher than 10^9.".into())
1535		} else {
1536			Ok(Duration::new(secs, nanos))
1537		}
1538	}
1539}
1540
1541impl EncodeLike for Duration {}
1542
1543impl<T> Encode for Range<T>
1544where
1545	T: Encode
1546{
1547	fn size_hint(&self) -> usize {
1548		2 * mem::size_of::<T>()
1549	}
1550
1551	fn encode(&self) -> Vec<u8> {
1552		(&self.start, &self.end).encode()
1553	}
1554}
1555
1556impl<T> Decode for Range<T>
1557where
1558	T: Decode
1559{
1560	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1561		let (start, end) = <(T, T)>::decode(input)
1562			.map_err(|e| e.chain("Could not decode `Range<T>`"))?;
1563		Ok(Range { start, end })
1564	}
1565}
1566
1567impl<T> Encode for RangeInclusive<T>
1568where
1569	T: Encode
1570{
1571	fn size_hint(&self) -> usize {
1572		2 * mem::size_of::<T>()
1573	}
1574
1575	fn encode(&self) -> Vec<u8> {
1576		(self.start(), self.end()).encode()
1577	}
1578}
1579
1580impl<T> Decode for RangeInclusive<T>
1581where
1582	T: Decode
1583{
1584	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1585		let (start, end) = <(T, T)>::decode(input)
1586			.map_err(|e| e.chain("Could not decode `RangeInclusive<T>`"))?;
1587		Ok(RangeInclusive::new(start, end))
1588	}
1589}
1590
1591
1592#[cfg(test)]
1593mod tests {
1594	use super::*;
1595	use std::borrow::Cow;
1596
1597	#[test]
1598	fn vec_is_sliceable() {
1599		let v = b"Hello world".to_vec();
1600		v.using_encoded(|ref slice|
1601			assert_eq!(slice, &b"\x2cHello world")
1602		);
1603	}
1604
1605	#[test]
1606	fn encode_borrowed_tuple() {
1607		let x = vec![1u8, 2, 3, 4];
1608		let y = 128i64;
1609
1610		let encoded = (&x, &y).encode();
1611
1612		assert_eq!((x, y), Decode::decode(&mut &encoded[..]).unwrap());
1613	}
1614
1615	#[test]
1616	fn cow_works() {
1617		let x = &[1u32, 2, 3, 4, 5, 6][..];
1618		let y = Cow::Borrowed(&x);
1619		assert_eq!(x.encode(), y.encode());
1620
1621		let z: Cow<'_, [u32]> = Cow::decode(&mut &x.encode()[..]).unwrap();
1622		assert_eq!(*z, *x);
1623	}
1624
1625	#[test]
1626	fn cow_string_works() {
1627		let x = "Hello world!";
1628		let y = Cow::Borrowed(&x);
1629		assert_eq!(x.encode(), y.encode());
1630
1631		let z: Cow<'_, str> = Cow::decode(&mut &x.encode()[..]).unwrap();
1632		assert_eq!(*z, *x);
1633	}
1634
1635	fn hexify(bytes: &[u8]) -> String {
1636		bytes.iter().map(|ref b| format!("{:02x}", b)).collect::<Vec<String>>().join(" ")
1637	}
1638
1639	#[test]
1640	fn string_encoded_as_expected() {
1641		let value = String::from("Hello, World!");
1642		let encoded = value.encode();
1643		assert_eq!(hexify(&encoded), "34 48 65 6c 6c 6f 2c 20 57 6f 72 6c 64 21");
1644		assert_eq!(<String>::decode(&mut &encoded[..]).unwrap(), value);
1645	}
1646
1647	#[test]
1648	fn vec_of_u8_encoded_as_expected() {
1649		let value = vec![0u8, 1, 1, 2, 3, 5, 8, 13, 21, 34];
1650		let encoded = value.encode();
1651		assert_eq!(hexify(&encoded), "28 00 01 01 02 03 05 08 0d 15 22");
1652		assert_eq!(<Vec<u8>>::decode(&mut &encoded[..]).unwrap(), value);
1653	}
1654
1655	#[test]
1656	fn vec_of_i16_encoded_as_expected() {
1657		let value = vec![0i16, 1, -1, 2, -2, 3, -3];
1658		let encoded = value.encode();
1659		assert_eq!(hexify(&encoded), "1c 00 00 01 00 ff ff 02 00 fe ff 03 00 fd ff");
1660		assert_eq!(<Vec<i16>>::decode(&mut &encoded[..]).unwrap(), value);
1661	}
1662
1663	#[test]
1664	fn vec_of_option_int_encoded_as_expected() {
1665		let value = vec![Some(1i8), Some(-1), None];
1666		let encoded = value.encode();
1667		assert_eq!(hexify(&encoded), "0c 01 01 01 ff 00");
1668		assert_eq!(<Vec<Option<i8>>>::decode(&mut &encoded[..]).unwrap(), value);
1669	}
1670
1671	#[test]
1672	fn vec_of_option_bool_encoded_as_expected() {
1673		let value = vec![OptionBool(Some(true)), OptionBool(Some(false)), OptionBool(None)];
1674		let encoded = value.encode();
1675		assert_eq!(hexify(&encoded), "0c 01 02 00");
1676		assert_eq!(<Vec<OptionBool>>::decode(&mut &encoded[..]).unwrap(), value);
1677	}
1678
1679	#[cfg(feature = "bytes")]
1680	#[test]
1681	fn bytes_works_as_expected() {
1682		let input = bytes::Bytes::from_static(b"hello");
1683		let encoded = Encode::encode(&input);
1684		let encoded_vec = input.to_vec().encode();
1685		assert_eq!(encoded, encoded_vec);
1686
1687		assert_eq!(
1688			&b"hello"[..],
1689			bytes::Bytes::decode(&mut &encoded[..]).unwrap(),
1690		);
1691	}
1692
1693	#[cfg(feature = "bytes")]
1694	#[test]
1695	fn bytes_deserialized_from_bytes_is_zero_copy() {
1696		let encoded = bytes::Bytes::from(Encode::encode(&b"hello".to_vec()));
1697		let decoded = decode_from_bytes::<bytes::Bytes>(encoded.clone()).unwrap();
1698		assert_eq!(decoded, &b"hello"[..]);
1699
1700		// The `slice_ref` will panic if the `decoded` is not a subslice of `encoded`.
1701		assert_eq!(encoded.slice_ref(&decoded), &b"hello"[..]);
1702	}
1703
1704	#[cfg(feature = "bytes")]
1705	#[test]
1706	fn nested_bytes_deserialized_from_bytes_is_zero_copy() {
1707		let encoded = bytes::Bytes::from(Encode::encode(&Some(b"hello".to_vec())));
1708		let decoded = decode_from_bytes::<Option<bytes::Bytes>>(encoded.clone()).unwrap();
1709		let decoded = decoded.as_ref().unwrap();
1710		assert_eq!(decoded, &b"hello"[..]);
1711
1712		// The `slice_ref` will panic if the `decoded` is not a subslice of `encoded`.
1713		assert_eq!(encoded.slice_ref(&decoded), &b"hello"[..]);
1714	}
1715
1716	fn test_encode_length<T: Encode + Decode + DecodeLength>(thing: &T, len: usize) {
1717		assert_eq!(<T as DecodeLength>::len(&thing.encode()[..]).unwrap(), len);
1718	}
1719
1720	#[test]
1721	fn len_works_for_decode_collection_types() {
1722		let vector = vec![10; 10];
1723		let mut btree_map: BTreeMap<u32, u32> = BTreeMap::new();
1724		btree_map.insert(1, 1);
1725		btree_map.insert(2, 2);
1726		let mut btree_set: BTreeSet<u32> = BTreeSet::new();
1727		btree_set.insert(1);
1728		btree_set.insert(2);
1729		let mut vd = VecDeque::new();
1730		vd.push_front(1);
1731		vd.push_front(2);
1732		let mut bh = BinaryHeap::new();
1733		bh.push(1);
1734		bh.push(2);
1735		let mut ll = LinkedList::new();
1736		ll.push_back(1);
1737		ll.push_back(2);
1738		let t1: (Vec<_>,) = (vector.clone(),);
1739		let t2: (Vec<_>, u32) = (vector.clone(), 3u32);
1740
1741		test_encode_length(&vector, 10);
1742		test_encode_length(&btree_map, 2);
1743		test_encode_length(&btree_set, 2);
1744		test_encode_length(&vd, 2);
1745		test_encode_length(&bh, 2);
1746		test_encode_length(&ll, 2);
1747		test_encode_length(&t1, 10);
1748		test_encode_length(&t2, 10);
1749	}
1750
1751	#[test]
1752	fn vec_of_string_encoded_as_expected() {
1753		let value = vec![
1754			"Hamlet".to_owned(),
1755			"Война и мир".to_owned(),
1756			"三国演义".to_owned(),
1757			"أَلْف لَيْلَة وَلَيْلَة‎".to_owned()
1758		];
1759		let encoded = value.encode();
1760		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 \
1761			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 \
1762			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 \
1763			d9 84 d9 8e d8 a9 e2 80 8e");
1764		assert_eq!(<Vec<String>>::decode(&mut &encoded[..]).unwrap(), value);
1765	}
1766
1767	#[derive(Debug, PartialEq)]
1768	struct MyWrapper(Compact<u32>);
1769	impl Deref for MyWrapper {
1770		type Target = Compact<u32>;
1771		fn deref(&self) -> &Self::Target { &self.0 }
1772	}
1773	impl WrapperTypeEncode for MyWrapper {}
1774
1775	impl From<Compact<u32>> for MyWrapper {
1776		fn from(c: Compact<u32>) -> Self { MyWrapper(c) }
1777	}
1778	impl WrapperTypeDecode for MyWrapper {
1779		type Wrapped = Compact<u32>;
1780	}
1781
1782	#[test]
1783	fn should_work_for_wrapper_types() {
1784		let result = vec![0b1100];
1785
1786		assert_eq!(MyWrapper(3u32.into()).encode(), result);
1787		assert_eq!(MyWrapper::decode(&mut &*result).unwrap(), MyWrapper(3_u32.into()));
1788	}
1789
1790	#[test]
1791	fn codec_vec_deque_u8_and_u16() {
1792		let mut v_u8 = VecDeque::new();
1793		let mut v_u16 = VecDeque::new();
1794
1795		for i in 0..50 {
1796			v_u8.push_front(i as u8);
1797			v_u16.push_front(i as u16);
1798		}
1799		for i in 50..100 {
1800			v_u8.push_back(i as u8);
1801			v_u16.push_back(i as u16);
1802		}
1803
1804		assert_eq!(Decode::decode(&mut &v_u8.encode()[..]), Ok(v_u8));
1805		assert_eq!(Decode::decode(&mut &v_u16.encode()[..]), Ok(v_u16));
1806	}
1807
1808	#[test]
1809	fn codec_iterator() {
1810		let t1: BTreeSet<u32> = FromIterator::from_iter((0..10).flat_map(|i| 0..i));
1811		let t2: LinkedList<u32> = FromIterator::from_iter((0..10).flat_map(|i| 0..i));
1812		let t3: BinaryHeap<u32> = FromIterator::from_iter((0..10).flat_map(|i| 0..i));
1813		let t4: BTreeMap<u16, u32> = FromIterator::from_iter(
1814			(0..10)
1815				.flat_map(|i| 0..i)
1816				.map(|i| (i as u16, i + 10))
1817		);
1818		let t5: BTreeSet<Vec<u8>> = FromIterator::from_iter((0..10).map(|i| Vec::from_iter(0..i)));
1819		let t6: LinkedList<Vec<u8>> = FromIterator::from_iter((0..10).map(|i| Vec::from_iter(0..i)));
1820		let t7: BinaryHeap<Vec<u8>> = FromIterator::from_iter((0..10).map(|i| Vec::from_iter(0..i)));
1821		let t8: BTreeMap<Vec<u8>, u32> = FromIterator::from_iter(
1822			(0..10)
1823				.map(|i| Vec::from_iter(0..i))
1824				.map(|i| (i.clone(), i.len() as u32))
1825		);
1826
1827		assert_eq!(Decode::decode(&mut &t1.encode()[..]), Ok(t1));
1828		assert_eq!(Decode::decode(&mut &t2.encode()[..]), Ok(t2));
1829		assert_eq!(
1830			Decode::decode(&mut &t3.encode()[..]).map(BinaryHeap::into_sorted_vec),
1831			Ok(t3.into_sorted_vec()),
1832		);
1833		assert_eq!(Decode::decode(&mut &t4.encode()[..]), Ok(t4));
1834		assert_eq!(Decode::decode(&mut &t5.encode()[..]), Ok(t5));
1835		assert_eq!(Decode::decode(&mut &t6.encode()[..]), Ok(t6));
1836		assert_eq!(
1837			Decode::decode(&mut &t7.encode()[..]).map(BinaryHeap::into_sorted_vec),
1838			Ok(t7.into_sorted_vec()),
1839		);
1840		assert_eq!(Decode::decode(&mut &t8.encode()[..]), Ok(t8));
1841	}
1842
1843	#[test]
1844	fn io_reader() {
1845		let mut io_reader = IoReader(std::io::Cursor::new(&[1u8, 2, 3][..]));
1846
1847		let mut v = [0; 2];
1848		io_reader.read(&mut v[..]).unwrap();
1849		assert_eq!(v, [1, 2]);
1850
1851		assert_eq!(io_reader.read_byte().unwrap(), 3);
1852
1853		assert_eq!(io_reader.read_byte(), Err("io error: UnexpectedEof".into()));
1854	}
1855
1856	#[test]
1857	fn shared_references_implement_encode() {
1858		Arc::new(10u32).encode();
1859		Rc::new(10u32).encode();
1860	}
1861
1862	#[test]
1863	fn not_limit_input_test() {
1864		use crate::Input;
1865
1866		struct NoLimit<'a>(&'a [u8]);
1867
1868		impl<'a> Input for NoLimit<'a> {
1869			fn remaining_len(&mut self) -> Result<Option<usize>, Error> {
1870				Ok(None)
1871			}
1872
1873			fn read(&mut self, into: &mut [u8]) -> Result<(), Error> {
1874				self.0.read(into)
1875			}
1876		}
1877
1878		let len = MAX_PREALLOCATION * 2 + 1;
1879		let mut i = Compact(len as u32).encode();
1880		i.resize(i.len() + len, 0);
1881		assert_eq!(<Vec<u8>>::decode(&mut NoLimit(&i[..])).unwrap(), vec![0u8; len]);
1882
1883		let i = Compact(len as u32).encode();
1884		assert_eq!(
1885			<Vec<u8>>::decode(&mut NoLimit(&i[..])).err().unwrap().to_string(),
1886			"Not enough data to fill buffer",
1887		);
1888
1889		let i = Compact(1000u32).encode();
1890		assert_eq!(
1891			<Vec<u8>>::decode(&mut NoLimit(&i[..])).err().unwrap().to_string(),
1892			"Not enough data to fill buffer",
1893		);
1894	}
1895
1896	#[test]
1897	fn boolean() {
1898		assert_eq!(true.encode(), vec![1]);
1899		assert_eq!(false.encode(), vec![0]);
1900		assert_eq!(bool::decode(&mut &[1][..]).unwrap(), true);
1901		assert_eq!(bool::decode(&mut &[0][..]).unwrap(), false);
1902	}
1903
1904	#[test]
1905	fn some_encode_like() {
1906		fn t<B: EncodeLike>() {}
1907		t::<&[u8]>();
1908		t::<&str>();
1909		t::<NonZeroU32>();
1910	}
1911
1912	#[test]
1913	fn vec_deque_encode_like_vec() {
1914		let data: VecDeque<u32> = vec![1, 2, 3, 4, 5, 6].into();
1915		let encoded = data.encode();
1916
1917		let decoded = Vec::<u32>::decode(&mut &encoded[..]).unwrap();
1918		assert!(decoded.iter().all(|v| data.contains(&v)));
1919		assert_eq!(data.len(), decoded.len());
1920
1921		let encoded = decoded.encode();
1922		let decoded = VecDeque::<u32>::decode(&mut &encoded[..]).unwrap();
1923		assert_eq!(data, decoded);
1924	}
1925
1926	#[test]
1927	fn vec_decode_right_capacity() {
1928		let data: Vec<u32> = vec![1, 2, 3];
1929		let mut encoded = data.encode();
1930		encoded.resize(encoded.len() * 2, 0);
1931		let decoded = Vec::<u32>::decode(&mut &encoded[..]).unwrap();
1932		assert_eq!(data, decoded);
1933		assert_eq!(decoded.capacity(), decoded.len());
1934		// Check with non-integer type
1935		let data: Vec<String> = vec!["1".into(), "2".into(), "3".into()];
1936		let mut encoded = data.encode();
1937		encoded.resize(65536, 0);
1938		let decoded = Vec::<String>::decode(&mut &encoded[..]).unwrap();
1939		assert_eq!(data, decoded);
1940		assert_eq!(decoded.capacity(), decoded.len());
1941	}
1942
1943	#[test]
1944	fn duration() {
1945		let num_secs = 13;
1946		let num_nanos = 37;
1947
1948		let duration = Duration::new(num_secs, num_nanos);
1949		let expected = (num_secs, num_nanos as u32).encode();
1950
1951		assert_eq!(duration.encode(), expected);
1952		assert_eq!(Duration::decode(&mut &expected[..]).unwrap(), duration);
1953	}
1954
1955	#[test]
1956	fn malformed_duration_encoding_fails() {
1957		// This test should fail, as the number of nanoseconds encoded is exactly 10^9.
1958		let invalid_nanos = A_BILLION;
1959		let encoded = (0u64, invalid_nanos).encode();
1960		assert!(Duration::decode(&mut &encoded[..]).is_err());
1961
1962		let num_secs = 1u64;
1963		let num_nanos = 37u32;
1964		let invalid_nanos = num_secs as u32 * A_BILLION + num_nanos;
1965		let encoded = (num_secs, invalid_nanos).encode();
1966		// This test should fail, as the number of nano seconds encoded is bigger than 10^9.
1967		assert!(Duration::decode(&mut &encoded[..]).is_err());
1968
1969		// Now constructing a valid duration and encoding it. Those asserts should not fail.
1970		let duration = Duration::from_nanos(invalid_nanos as u64);
1971		let expected = (num_secs, num_nanos).encode();
1972
1973		assert_eq!(duration.encode(), expected);
1974		assert_eq!(Duration::decode(&mut &expected[..]).unwrap(), duration);
1975	}
1976
1977	#[test]
1978	fn u64_max() {
1979		let num_secs = u64::MAX;
1980		let num_nanos = 0;
1981		let duration = Duration::new(num_secs, num_nanos);
1982		let expected = (num_secs, num_nanos).encode();
1983
1984		assert_eq!(duration.encode(), expected);
1985		assert_eq!(Duration::decode(&mut &expected[..]).unwrap(), duration);
1986	}
1987
1988	#[test]
1989	fn decoding_does_not_overflow() {
1990		let num_secs = u64::MAX;
1991		let num_nanos = A_BILLION;
1992
1993		// `num_nanos`' carry should make `num_secs` overflow if we were to call `Duration::new()`.
1994		// This test checks that the we do not call `Duration::new()`.
1995		let encoded = (num_secs, num_nanos).encode();
1996		assert!(Duration::decode(&mut &encoded[..]).is_err());
1997	}
1998
1999	#[test]
2000	fn string_invalid_utf8() {
2001		// `167, 10` is not a valid utf8 sequence, so this should be an error.
2002		let mut bytes: &[u8] = &[20, 114, 167, 10, 20, 114];
2003
2004		let obj = <String>::decode(&mut bytes);
2005		assert!(obj.is_err());
2006	}
2007
2008	#[test]
2009	fn empty_array_encode_and_decode() {
2010		let data: [u32; 0] = [];
2011		let encoded = data.encode();
2012		assert!(encoded.is_empty());
2013		<[u32; 0]>::decode(&mut &encoded[..]).unwrap();
2014	}
2015
2016
2017	macro_rules! test_array_encode_and_decode {
2018		( $( $name:ty ),* $(,)? ) => {
2019			$(
2020				paste::item! {
2021					#[test]
2022					fn [<test_array_encode_and_decode _ $name>]() {
2023						let data: [$name; 32] = [123 as $name; 32];
2024						let encoded = data.encode();
2025						let decoded: [$name; 32] = Decode::decode(&mut &encoded[..]).unwrap();
2026						assert_eq!(decoded, data);
2027					}
2028				}
2029			)*
2030		}
2031	}
2032
2033	test_array_encode_and_decode!(u8, i8, u16, i16, u32, i32, u64, i64, u128, i128);
2034
2035	test_array_encode_and_decode!(f32, f64);
2036
2037	fn test_encoded_size(val: impl Encode) {
2038		let length = val.using_encoded(|v| v.len());
2039
2040		assert_eq!(length, val.encoded_size());
2041	}
2042
2043	struct TestStruct {
2044		data: Vec<u32>,
2045		other: u8,
2046		compact: Compact<u128>,
2047	}
2048
2049	impl Encode for TestStruct {
2050		fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
2051			self.data.encode_to(dest);
2052			self.other.encode_to(dest);
2053			self.compact.encode_to(dest);
2054		}
2055	}
2056
2057	#[test]
2058	fn encoded_size_works() {
2059		test_encoded_size(120u8);
2060		test_encoded_size(30u16);
2061		test_encoded_size(1u32);
2062		test_encoded_size(2343545u64);
2063		test_encoded_size(34358394245459854u128);
2064		test_encoded_size(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10u32]);
2065		test_encoded_size(Compact(32445u32));
2066		test_encoded_size(Compact(34353454453545u128));
2067		test_encoded_size(TestStruct {
2068			data: vec![1, 2, 4, 5, 6],
2069			other: 45,
2070			compact: Compact(123234545),
2071		});
2072	}
2073
2074	#[test]
2075	fn ranges() {
2076		let range = Range { start: 1, end: 100 };
2077		let range_bytes = (1, 100).encode();
2078		assert_eq!(range.encode(), range_bytes);
2079		assert_eq!(Range::decode(&mut &range_bytes[..]), Ok(range));
2080
2081		let range_inclusive = RangeInclusive::new(1, 100);
2082		let range_inclusive_bytes = (1, 100).encode();
2083		assert_eq!(range_inclusive.encode(), range_inclusive_bytes);
2084		assert_eq!(RangeInclusive::decode(&mut &range_inclusive_bytes[..]), Ok(range_inclusive));
2085	}
2086}