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