jam_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`.
627#[cfg(target_has_atomic = "ptr")]
628impl<T: DecodeWithMemTracking> DecodeWithMemTracking for Arc<T> {}
629
630impl<T, X> Decode for X
631where
632	T: Decode + Into<X>,
633	X: WrapperTypeDecode<Wrapped = T>,
634{
635	#[inline]
636	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
637		Self::decode_wrapped(input)
638	}
639}
640
641/// A macro that matches on a [`TypeInfo`] and expands a given macro per variant.
642///
643/// The first parameter to the given macro will be the type of variant (e.g. `u8`, `u32`, etc.) and
644/// other parameters given to this macro.
645///
646/// The last parameter is the code that should be executed for the `Unknown` type info.
647macro_rules! with_type_info {
648	( $type_info:expr, $macro:ident $( ( $( $params:ident ),* ) )?, { $( $unknown_variant:tt )* }, ) => {
649		match $type_info {
650			TypeInfo::U8 => { $macro!(u8 $( $( , $params )* )? ) },
651			TypeInfo::I8 => { $macro!(i8 $( $( , $params )* )? ) },
652			TypeInfo::U16 => { $macro!(u16 $( $( , $params )* )? ) },
653			TypeInfo::I16 => { $macro!(i16 $( $( , $params )* )? ) },
654			TypeInfo::U32 => { $macro!(u32 $( $( , $params )* )? ) },
655			TypeInfo::I32 => { $macro!(i32 $( $( , $params )* )? ) },
656			TypeInfo::U64 => { $macro!(u64 $( $( , $params )* )? ) },
657			TypeInfo::I64 => { $macro!(i64 $( $( , $params )* )? ) },
658			TypeInfo::U128 => { $macro!(u128 $( $( , $params )* )? ) },
659			TypeInfo::I128 => { $macro!(i128 $( $( , $params )* )? ) },
660			TypeInfo::Unknown => { $( $unknown_variant )* },
661			TypeInfo::F32 => { $macro!(f32 $( $( , $params )* )? ) },
662			TypeInfo::F64 => { $macro!(f64 $( $( , $params )* )? ) },
663		}
664	};
665}
666
667/// Something that can be encoded as a reference.
668pub trait EncodeAsRef<'a, T: 'a> {
669	/// The reference type that is used for encoding.
670	type RefType: Encode + From<&'a T>;
671}
672
673impl<T: Encode, E: Encode> Encode for Result<T, E> {
674	fn size_hint(&self) -> usize {
675		1 + match *self {
676			Ok(ref t) => t.size_hint(),
677			Err(ref t) => t.size_hint(),
678		}
679	}
680
681	fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
682		match *self {
683			Ok(ref t) => {
684				dest.push_byte(0);
685				t.encode_to(dest);
686			},
687			Err(ref e) => {
688				dest.push_byte(1);
689				e.encode_to(dest);
690			},
691		}
692	}
693}
694
695impl<T, LikeT, E, LikeE> EncodeLike<Result<LikeT, LikeE>> for Result<T, E>
696where
697	T: EncodeLike<LikeT>,
698	LikeT: Encode,
699	E: EncodeLike<LikeE>,
700	LikeE: Encode,
701{
702}
703
704impl<T: Decode, E: Decode> Decode for Result<T, E> {
705	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
706		match input
707			.read_byte()
708			.map_err(|e| e.chain("Could not result variant byte for `Result`"))?
709		{
710			0 => Ok(Ok(T::decode(input).map_err(|e| e.chain("Could not Decode `Result::Ok(T)`"))?)),
711			1 => Ok(Err(
712				E::decode(input).map_err(|e| e.chain("Could not decode `Result::Error(E)`"))?
713			)),
714			_ => Err("unexpected first byte decoding Result".into()),
715		}
716	}
717}
718
719impl<T: DecodeWithMemTracking, E: DecodeWithMemTracking> DecodeWithMemTracking for Result<T, E> {}
720
721/// Shim type because we can't do a specialised implementation for `Option<bool>` directly.
722#[derive(Eq, PartialEq, Clone, Copy)]
723pub struct OptionBool(pub Option<bool>);
724
725impl fmt::Debug for OptionBool {
726	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
727		self.0.fmt(f)
728	}
729}
730
731impl Encode for OptionBool {
732	fn size_hint(&self) -> usize {
733		1
734	}
735
736	fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
737		f(&[match *self {
738			OptionBool(None) => 0u8,
739			OptionBool(Some(true)) => 1u8,
740			OptionBool(Some(false)) => 2u8,
741		}])
742	}
743}
744
745impl EncodeLike for OptionBool {}
746
747impl Decode for OptionBool {
748	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
749		match input.read_byte()? {
750			0 => Ok(OptionBool(None)),
751			1 => Ok(OptionBool(Some(true))),
752			2 => Ok(OptionBool(Some(false))),
753			_ => Err("unexpected first byte decoding OptionBool".into()),
754		}
755	}
756}
757
758impl DecodeWithMemTracking for OptionBool {}
759
760impl<T: EncodeLike<U>, U: Encode> EncodeLike<Option<U>> for Option<T> {}
761
762impl<T: Encode> Encode for Option<T> {
763	fn size_hint(&self) -> usize {
764		1 + match *self {
765			Some(ref t) => t.size_hint(),
766			None => 0,
767		}
768	}
769
770	fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
771		match *self {
772			Some(ref t) => {
773				dest.push_byte(1);
774				t.encode_to(dest);
775			},
776			None => dest.push_byte(0),
777		}
778	}
779}
780
781impl<T: Decode> Decode for Option<T> {
782	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
783		match input
784			.read_byte()
785			.map_err(|e| e.chain("Could not decode variant byte for `Option`"))?
786		{
787			0 => Ok(None),
788			1 => Ok(Some(
789				T::decode(input).map_err(|e| e.chain("Could not decode `Option::Some(T)`"))?,
790			)),
791			_ => Err("unexpected first byte decoding Option".into()),
792		}
793	}
794}
795
796impl<T: DecodeWithMemTracking> DecodeWithMemTracking for Option<T> {}
797
798macro_rules! impl_for_non_zero {
799	( $( $name:ty ),* $(,)? ) => {
800		$(
801			impl Encode for $name {
802				fn size_hint(&self) -> usize {
803					self.get().size_hint()
804				}
805
806				fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
807					self.get().encode_to(dest)
808				}
809
810				fn encode(&self) -> Vec<u8> {
811					self.get().encode()
812				}
813
814				fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
815					self.get().using_encoded(f)
816				}
817			}
818
819			impl EncodeLike for $name {}
820
821			impl Decode for $name {
822				fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
823					Self::new(Decode::decode(input)?)
824						.ok_or_else(|| Error::from("cannot create non-zero number from 0"))
825				}
826			}
827
828			impl DecodeWithMemTracking for $name {}
829		)*
830	}
831}
832
833/// Encode the slice without prepending the len.
834///
835/// This is equivalent to encoding all the element one by one, but it is optimized for some types.
836pub(crate) fn encode_slice_no_len<T: Encode, W: Output + ?Sized>(slice: &[T], dest: &mut W) {
837	macro_rules! encode_to {
838		( u8, $slice:ident, $dest:ident ) => {{
839			let typed = unsafe { mem::transmute::<&[T], &[u8]>(&$slice[..]) };
840			$dest.write(&typed)
841		}};
842		( i8, $slice:ident, $dest:ident ) => {{
843			// `i8` has the same size as `u8`. We can just convert it here and write to the
844			// dest buffer directly.
845			let typed = unsafe { mem::transmute::<&[T], &[u8]>(&$slice[..]) };
846			$dest.write(&typed)
847		}};
848		( $ty:ty, $slice:ident, $dest:ident ) => {{
849			if cfg!(target_endian = "little") {
850				let typed = unsafe { mem::transmute::<&[T], &[$ty]>(&$slice[..]) };
851				$dest.write(<[$ty] as AsByteSlice<$ty>>::as_byte_slice(typed))
852			} else {
853				for item in $slice.iter() {
854					item.encode_to(dest);
855				}
856			}
857		}};
858	}
859
860	with_type_info! {
861		<T as Encode>::TYPE_INFO,
862		encode_to(slice, dest),
863		{
864			for item in slice.iter() {
865				item.encode_to(dest);
866			}
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 { Ok(array.assume_init()) }
914	}
915
916	fn decode_into<I: Input>(
917		input: &mut I,
918		dst: &mut MaybeUninit<Self>,
919	) -> Result<DecodeFinished, Error> {
920		let is_primitive = match <T as Decode>::TYPE_INFO {
921			| TypeInfo::U8 | TypeInfo::I8 => true,
922			| TypeInfo::U16 |
923			TypeInfo::I16 |
924			TypeInfo::U32 |
925			TypeInfo::I32 |
926			TypeInfo::U64 |
927			TypeInfo::I64 |
928			TypeInfo::U128 |
929			TypeInfo::I128 |
930			TypeInfo::F32 |
931			TypeInfo::F64 => cfg!(target_endian = "little"),
932			TypeInfo::Unknown => false,
933		};
934
935		if is_primitive {
936			// Let's read the array in bulk as that's going to be a lot
937			// faster than just reading each element one-by-one.
938
939			let ptr: *mut [T; N] = dst.as_mut_ptr();
940			let ptr: *mut u8 = ptr.cast();
941
942			let bytesize = calculate_array_bytesize::<T, N>();
943
944			// TODO: This is potentially slow; it'd be better if `Input` supported
945			//       reading directly into uninitialized memory.
946			//
947			// SAFETY: The pointer is valid and points to a memory `bytesize` bytes big.
948			unsafe {
949				ptr.write_bytes(0, bytesize);
950			}
951
952			// SAFETY: We've zero-initialized everything so creating a slice here is safe.
953			let slice: &mut [u8] = unsafe { core::slice::from_raw_parts_mut(ptr, bytesize) };
954
955			input.read(slice)?;
956
957			// SAFETY: We've initialized the whole slice so calling this is safe.
958			unsafe {
959				return Ok(DecodeFinished::assert_decoding_finished());
960			}
961		}
962
963		let slice: &mut [MaybeUninit<T>; N] = {
964			let ptr: *mut [T; N] = dst.as_mut_ptr();
965			let ptr: *mut [MaybeUninit<T>; N] = ptr.cast();
966			// SAFETY: Casting `&mut MaybeUninit<[T; N]>` into `&mut [MaybeUninit<T>; N]` is safe.
967			unsafe { &mut *ptr }
968		};
969
970		/// A wrapper type to make sure the partially read elements are always
971		/// dropped in case an error occurs or the underlying `decode` implementation panics.
972		struct State<'a, T, const N: usize> {
973			count: usize,
974			slice: &'a mut [MaybeUninit<T>; N],
975		}
976
977		impl<T, const N: usize> Drop for State<'_, T, N> {
978			fn drop(&mut self) {
979				if !mem::needs_drop::<T>() {
980					// If the types don't actually need to be dropped then don't even
981					// try to run the loop below.
982					//
983					// Most likely won't make a difference in release mode, but will
984					// make a difference in debug mode.
985					return;
986				}
987
988				// TODO: Use `MaybeUninit::slice_assume_init_mut` + `core::ptr::drop_in_place`
989				//       once `slice_assume_init_mut` is stable.
990				for item in &mut self.slice[..self.count] {
991					// SAFETY: Each time we've read a new element we incremented `count`,
992					//         and we only drop at most `count` elements here,
993					//         so all of the elements we drop here are valid.
994					unsafe {
995						item.assume_init_drop();
996					}
997				}
998			}
999		}
1000
1001		let mut state = State { count: 0, slice };
1002
1003		while state.count < state.slice.len() {
1004			T::decode_into(input, &mut state.slice[state.count])?;
1005			state.count += 1;
1006		}
1007
1008		// We've successfully read everything, so disarm the `Drop` impl.
1009		mem::forget(state);
1010
1011		// SAFETY: We've initialized the whole slice so calling this is safe.
1012		unsafe { Ok(DecodeFinished::assert_decoding_finished()) }
1013	}
1014
1015	fn skip<I: Input>(input: &mut I) -> Result<(), Error> {
1016		if Self::encoded_fixed_size().is_some() {
1017			// Should skip the bytes, but Input does not support skip.
1018			for _ in 0..N {
1019				T::skip(input)?;
1020			}
1021		} else {
1022			Self::decode(input)?;
1023		}
1024		Ok(())
1025	}
1026
1027	fn encoded_fixed_size() -> Option<usize> {
1028		Some(<T as Decode>::encoded_fixed_size()? * N)
1029	}
1030}
1031
1032impl<T: DecodeWithMemTracking, const N: usize> DecodeWithMemTracking for [T; N] {}
1033
1034impl<T: EncodeLike<U>, U: Encode, const N: usize> EncodeLike<[U; N]> for [T; N] {}
1035
1036impl Encode for str {
1037	fn size_hint(&self) -> usize {
1038		self.as_bytes().size_hint()
1039	}
1040
1041	fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
1042		self.as_bytes().encode_to(dest)
1043	}
1044
1045	fn encode(&self) -> Vec<u8> {
1046		self.as_bytes().encode()
1047	}
1048
1049	fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
1050		self.as_bytes().using_encoded(f)
1051	}
1052}
1053
1054impl<T: ToOwned + ?Sized> Decode for Cow<'_, T>
1055where
1056	<T as ToOwned>::Owned: Decode,
1057{
1058	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1059		Ok(Cow::Owned(Decode::decode(input)?))
1060	}
1061}
1062
1063impl<'a, T: ToOwned + ?Sized> DecodeWithMemTracking for Cow<'a, T>
1064where
1065	Cow<'a, T>: Decode,
1066	T::Owned: DecodeWithMemTracking,
1067{
1068}
1069
1070impl<T> EncodeLike for PhantomData<T> {}
1071
1072impl<T> Encode for PhantomData<T> {
1073	fn encode_to<W: Output + ?Sized>(&self, _dest: &mut W) {}
1074}
1075
1076impl<T> Decode for PhantomData<T> {
1077	fn decode<I: Input>(_input: &mut I) -> Result<Self, Error> {
1078		Ok(PhantomData)
1079	}
1080}
1081
1082impl<T> DecodeWithMemTracking for PhantomData<T> where PhantomData<T>: Decode {}
1083
1084impl Decode for String {
1085	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1086		Self::from_utf8(Vec::decode(input)?).map_err(|_| "Invalid utf8 sequence".into())
1087	}
1088}
1089
1090impl DecodeWithMemTracking for String {}
1091
1092/// Writes the compact encoding of `len` do `dest`.
1093pub(crate) fn compact_encode_len_to<W: Output + ?Sized>(
1094	dest: &mut W,
1095	len: usize,
1096) -> Result<(), Error> {
1097	if len > u32::MAX as usize {
1098		return Err("Attempted to serialize a collection with too many elements.".into());
1099	}
1100
1101	Compact(len as u32).encode_to(dest);
1102	Ok(())
1103}
1104
1105impl<T: Encode> Encode for [T] {
1106	fn size_hint(&self) -> usize {
1107		mem::size_of::<u32>() + mem::size_of_val(self)
1108	}
1109
1110	fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
1111		compact_encode_len_to(dest, self.len()).expect("Compact encodes length");
1112
1113		encode_slice_no_len(self, dest)
1114	}
1115}
1116
1117fn decode_vec_chunked<T, I: Input, F>(
1118	input: &mut I,
1119	len: usize,
1120	mut decode_chunk: F,
1121) -> Result<Vec<T>, Error>
1122where
1123	F: FnMut(&mut I, &mut Vec<T>, usize) -> Result<(), Error>,
1124{
1125	// const { assert!(INITIAL_PREALLOCATION >= mem::size_of::<T>()) }
1126	// we have to account for the fact that `mem::size_of::<T>` can be 0 for types like `()`
1127	// for example.
1128	let mut chunk_len = INITIAL_PREALLOCATION.checked_div(mem::size_of::<T>()).unwrap_or(1);
1129
1130	let mut decoded_vec = vec![];
1131	let mut num_undecoded_items = len;
1132	while num_undecoded_items > 0 {
1133		let bounded_chunk_len = chunk_len.min(num_undecoded_items);
1134		input.on_before_alloc_mem(bounded_chunk_len.saturating_mul(mem::size_of::<T>()))?;
1135		decoded_vec.reserve_exact(bounded_chunk_len);
1136
1137		decode_chunk(input, &mut decoded_vec, bounded_chunk_len)?;
1138
1139		num_undecoded_items -= bounded_chunk_len;
1140		chunk_len = decoded_vec.len();
1141	}
1142
1143	Ok(decoded_vec)
1144}
1145
1146/// Create a `Vec<T>` by casting directly from a buffer of read `u8`s
1147///
1148/// The encoding of `T` must be equal to its binary representation, and size of `T` must be less
1149/// or equal to [`INITIAL_PREALLOCATION`].
1150fn read_vec_from_u8s<T, I>(input: &mut I, len: usize) -> Result<Vec<T>, Error>
1151where
1152	T: ToMutByteSlice + Default + Clone,
1153	I: Input,
1154{
1155	let byte_len = len
1156		.checked_mul(mem::size_of::<T>())
1157		.ok_or("Item is too big and cannot be allocated")?;
1158
1159	// Check if there is enough data in the input buffer.
1160	if let Some(input_len) = input.remaining_len()? {
1161		if input_len < byte_len {
1162			return Err("Not enough data to decode vector".into());
1163		}
1164	}
1165
1166	decode_vec_chunked(input, len, |input, decoded_vec, chunk_len| {
1167		let decoded_vec_len = decoded_vec.len();
1168		let decoded_vec_size = decoded_vec_len * mem::size_of::<T>();
1169		unsafe {
1170			decoded_vec.set_len(decoded_vec_len + chunk_len);
1171		}
1172
1173		let bytes_slice = decoded_vec.as_mut_byte_slice();
1174		input.read(&mut bytes_slice[decoded_vec_size..])
1175	})
1176}
1177
1178fn decode_vec_from_items<T, I>(input: &mut I, len: usize) -> Result<Vec<T>, Error>
1179where
1180	T: Decode,
1181	I: Input,
1182{
1183	input.descend_ref()?;
1184	let vec = decode_vec_chunked(input, len, |input, decoded_vec, chunk_len| {
1185		for _ in 0..chunk_len {
1186			decoded_vec.push(T::decode(input)?);
1187		}
1188
1189		Ok(())
1190	})?;
1191	input.ascend_ref();
1192
1193	Ok(vec)
1194}
1195
1196/// Decode the vec (without a prepended len).
1197///
1198/// This is equivalent to decode all elements one by one, but it is optimized in some
1199/// situation.
1200pub fn decode_vec_with_len<T: Decode, I: Input>(
1201	input: &mut I,
1202	len: usize,
1203) -> Result<Vec<T>, Error> {
1204	macro_rules! decode {
1205		( $ty:ty, $input:ident, $len:ident ) => {{
1206			if cfg!(target_endian = "little") || mem::size_of::<T>() == 1 {
1207				let vec = read_vec_from_u8s::<$ty, _>($input, $len)?;
1208				Ok(unsafe { mem::transmute::<Vec<$ty>, Vec<T>>(vec) })
1209			} else {
1210				decode_vec_from_items::<T, _>($input, $len)
1211			}
1212		}};
1213	}
1214
1215	with_type_info! {
1216		<T as Decode>::TYPE_INFO,
1217		decode(input, len),
1218		{
1219			decode_vec_from_items::<T, _>(input, len)
1220		},
1221	}
1222}
1223
1224impl<T> WrapperTypeEncode for Vec<T> {}
1225impl<T: EncodeLike<U>, U: Encode> EncodeLike<Vec<U>> for Vec<T> {}
1226impl<T: EncodeLike<U>, U: Encode> EncodeLike<&[U]> for Vec<T> {}
1227impl<T: EncodeLike<U>, U: Encode> EncodeLike<Vec<U>> for &[T] {}
1228
1229impl<T: Decode> Decode for Vec<T> {
1230	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1231		<Compact<u32>>::decode(input)
1232			.and_then(move |Compact(len)| decode_vec_with_len(input, len as usize))
1233	}
1234}
1235
1236impl<T: DecodeWithMemTracking> DecodeWithMemTracking for Vec<T> {}
1237
1238macro_rules! impl_encode_for_collection {
1239	($(
1240		$type:ident
1241		{ $( $generics:ident $( : $decode_additional:ident )? ),* }
1242		{ $( $type_like_generics:ident ),* }
1243		{ $( $impl_like_generics:tt )* }
1244	)*) => {$(
1245		impl<$( $generics: Encode ),*> Encode for $type<$( $generics, )*> {
1246			fn size_hint(&self) -> usize {
1247				mem::size_of::<u32>() + mem::size_of::<($($generics,)*)>().saturating_mul(self.len())
1248			}
1249
1250			fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
1251				compact_encode_len_to(dest, self.len()).expect("Compact encodes length");
1252
1253				for i in self.iter() {
1254					i.encode_to(dest);
1255				}
1256			}
1257		}
1258
1259		impl<$( $impl_like_generics )*> EncodeLike<$type<$( $type_like_generics ),*>>
1260			for $type<$( $generics ),*> {}
1261		impl<$( $impl_like_generics )*> EncodeLike<&[( $( $type_like_generics, )* )]>
1262			for $type<$( $generics ),*> {}
1263		impl<$( $impl_like_generics )*> EncodeLike<$type<$( $type_like_generics ),*>>
1264			for &[( $( $generics, )* )] {}
1265	)*}
1266}
1267
1268impl_encode_for_collection! {
1269	BTreeMap { K: Ord, V } { LikeK, LikeV}
1270		{ K: EncodeLike<LikeK>, LikeK: Encode, V: EncodeLike<LikeV>, LikeV: Encode }
1271}
1272
1273impl<K: Decode + Ord, V: Decode> Decode for BTreeMap<K, V> {
1274	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1275		<Compact<u32>>::decode(input).and_then(move |Compact(len)| {
1276			input.descend_ref()?;
1277			input.on_before_alloc_mem(super::btree_utils::mem_size_of_btree::<(K, V)>(len))?;
1278			let result = Result::from_iter((0..len).map(|_| Decode::decode(input)));
1279			input.ascend_ref();
1280			result
1281		})
1282	}
1283}
1284
1285impl<K: DecodeWithMemTracking, V: DecodeWithMemTracking> DecodeWithMemTracking for BTreeMap<K, V> where
1286	BTreeMap<K, V>: Decode
1287{
1288}
1289
1290impl_encode_for_collection! {
1291	BTreeSet { T: Ord } { LikeT }
1292		{ T: EncodeLike<LikeT>, LikeT: Encode }
1293}
1294
1295impl<T: Decode + Ord> Decode for BTreeSet<T> {
1296	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1297		<Compact<u32>>::decode(input).and_then(move |Compact(len)| {
1298			input.descend_ref()?;
1299			input.on_before_alloc_mem(super::btree_utils::mem_size_of_btree::<T>(len))?;
1300			let result = Result::from_iter((0..len).map(|_| Decode::decode(input)));
1301			input.ascend_ref();
1302			result
1303		})
1304	}
1305}
1306impl<T: DecodeWithMemTracking> DecodeWithMemTracking for BTreeSet<T> where BTreeSet<T>: Decode {}
1307
1308impl_encode_for_collection! {
1309	LinkedList { T } { LikeT }
1310		{ T: EncodeLike<LikeT>, LikeT: Encode }
1311}
1312
1313impl<T: Decode> Decode for LinkedList<T> {
1314	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1315		<Compact<u32>>::decode(input).and_then(move |Compact(len)| {
1316			input.descend_ref()?;
1317			// We account for the size of the `prev` and `next` pointers of each list node,
1318			// plus the decoded element.
1319			input.on_before_alloc_mem((len as usize).saturating_mul(mem::size_of::<(
1320				usize,
1321				usize,
1322				T,
1323			)>()))?;
1324			let result = Result::from_iter((0..len).map(|_| Decode::decode(input)));
1325			input.ascend_ref();
1326			result
1327		})
1328	}
1329}
1330
1331impl<T: DecodeWithMemTracking> DecodeWithMemTracking for LinkedList<T> where LinkedList<T>: Decode {}
1332
1333impl_encode_for_collection! {
1334	BinaryHeap { T: Ord } { LikeT }
1335		{ T: EncodeLike<LikeT>, LikeT: Encode }
1336}
1337
1338impl<T: Decode + Ord> Decode for BinaryHeap<T> {
1339	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1340		Ok(Vec::decode(input)?.into())
1341	}
1342}
1343impl<T: DecodeWithMemTracking> DecodeWithMemTracking for BinaryHeap<T> where BinaryHeap<T>: Decode {}
1344
1345impl<T: Encode> EncodeLike for VecDeque<T> {}
1346impl<T: EncodeLike<U>, U: Encode> EncodeLike<&[U]> for VecDeque<T> {}
1347impl<T: EncodeLike<U>, U: Encode> EncodeLike<VecDeque<U>> for &[T] {}
1348impl<T: EncodeLike<U>, U: Encode> EncodeLike<Vec<U>> for VecDeque<T> {}
1349impl<T: EncodeLike<U>, U: Encode> EncodeLike<VecDeque<U>> for Vec<T> {}
1350
1351impl<T: Encode> Encode for VecDeque<T> {
1352	fn size_hint(&self) -> usize {
1353		mem::size_of::<u32>() + mem::size_of::<T>() * self.len()
1354	}
1355
1356	fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
1357		compact_encode_len_to(dest, self.len()).expect("Compact encodes length");
1358
1359		let slices = self.as_slices();
1360		encode_slice_no_len(slices.0, dest);
1361		encode_slice_no_len(slices.1, dest);
1362	}
1363}
1364
1365impl<T: Decode> Decode for VecDeque<T> {
1366	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1367		Ok(<Vec<T>>::decode(input)?.into())
1368	}
1369}
1370
1371impl<T: DecodeWithMemTracking> DecodeWithMemTracking for VecDeque<T> {}
1372
1373impl EncodeLike for () {}
1374
1375impl Encode for () {
1376	fn encode_to<W: Output + ?Sized>(&self, _dest: &mut W) {}
1377
1378	fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
1379		f(&[])
1380	}
1381
1382	fn encode(&self) -> Vec<u8> {
1383		Vec::new()
1384	}
1385}
1386
1387impl Decode for () {
1388	fn decode<I: Input>(_: &mut I) -> Result<(), Error> {
1389		Ok(())
1390	}
1391}
1392
1393macro_rules! impl_len {
1394	( $( $type:ident< $($g:ident),* > ),* ) => { $(
1395		impl<$($g),*> DecodeLength for $type<$($g),*> {
1396			fn len(mut self_encoded: &[u8]) -> Result<usize, Error> {
1397				usize::try_from(u32::from(Compact::<u32>::decode(&mut self_encoded)?))
1398					.map_err(|_| "Failed convert decoded size into usize.".into())
1399			}
1400		}
1401	)*}
1402}
1403
1404// Collection types that support compact decode length.
1405impl_len!(Vec<T>, BTreeSet<T>, BTreeMap<K, V>, VecDeque<T>, BinaryHeap<T>, LinkedList<T>);
1406
1407macro_rules! tuple_impl {
1408	(
1409		($one:ident, $extra:ident),
1410	) => {
1411		impl<$one: Encode> Encode for ($one,) {
1412			fn size_hint(&self) -> usize {
1413				self.0.size_hint()
1414			}
1415
1416			fn encode_to<T: Output + ?Sized>(&self, dest: &mut T) {
1417				self.0.encode_to(dest);
1418			}
1419
1420			fn encode(&self) -> Vec<u8> {
1421				self.0.encode()
1422			}
1423
1424			fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
1425				self.0.using_encoded(f)
1426			}
1427		}
1428
1429		impl<$one: Decode> Decode for ($one,) {
1430			fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1431				match $one::decode(input) {
1432					Err(e) => Err(e),
1433					Ok($one) => Ok(($one,)),
1434				}
1435			}
1436		}
1437
1438		impl<$one: DecodeLength> DecodeLength for ($one,) {
1439			fn len(self_encoded: &[u8]) -> Result<usize, Error> {
1440				$one::len(self_encoded)
1441			}
1442		}
1443
1444		impl<$one: EncodeLike<$extra>, $extra: Encode> crate::EncodeLike<($extra,)> for ($one,) {}
1445	};
1446	(($first:ident, $fextra:ident), $( ( $rest:ident, $rextra:ident ), )+) => {
1447		impl<$first: Encode, $($rest: Encode),+> Encode for ($first, $($rest),+) {
1448			fn size_hint(&self) -> usize {
1449				let (
1450					ref $first,
1451					$(ref $rest),+
1452				) = *self;
1453				$first.size_hint()
1454				$( + $rest.size_hint() )+
1455			}
1456
1457			fn encode_to<T: Output + ?Sized>(&self, dest: &mut T) {
1458				let (
1459					ref $first,
1460					$(ref $rest),+
1461				) = *self;
1462
1463				$first.encode_to(dest);
1464				$($rest.encode_to(dest);)+
1465			}
1466		}
1467
1468		impl<$first: Decode, $($rest: Decode),+> Decode for ($first, $($rest),+) {
1469			fn decode<INPUT: Input>(input: &mut INPUT) -> Result<Self, super::Error> {
1470				Ok((
1471					match $first::decode(input) {
1472						Ok(x) => x,
1473						Err(e) => return Err(e),
1474					},
1475					$(match $rest::decode(input) {
1476						Ok(x) => x,
1477						Err(e) => return Err(e),
1478					},)+
1479				))
1480			}
1481		}
1482
1483		impl<$first: EncodeLike<$fextra>, $fextra: Encode,
1484			$($rest: EncodeLike<$rextra>, $rextra: Encode),+> crate::EncodeLike<($fextra, $( $rextra ),+)>
1485			for ($first, $($rest),+) {}
1486
1487		impl<$first: DecodeLength, $($rest),+> DecodeLength for ($first, $($rest),+) {
1488			fn len(self_encoded: &[u8]) -> Result<usize, Error> {
1489				$first::len(self_encoded)
1490			}
1491		}
1492
1493		tuple_impl!( $( ($rest, $rextra), )+ );
1494	}
1495}
1496
1497#[allow(non_snake_case)]
1498mod inner_tuple_impl {
1499	use super::*;
1500
1501	tuple_impl!(
1502		(A0, A1),
1503		(B0, B1),
1504		(C0, C1),
1505		(D0, D1),
1506		(E0, E1),
1507		(F0, F1),
1508		(G0, G1),
1509		(H0, H1),
1510		(I0, I1),
1511		(J0, J1),
1512		(K0, K1),
1513		(L0, L1),
1514		(M0, M1),
1515		(N0, N1),
1516		(O0, O1),
1517		(P0, P1),
1518		(Q0, Q1),
1519		(R0, R1),
1520	);
1521}
1522
1523macro_rules! impl_endians {
1524	( $( $t:ty; $ty_info:ident ),* ) => { $(
1525		impl EncodeLike for $t {}
1526
1527		impl Encode for $t {
1528			const TYPE_INFO: TypeInfo = TypeInfo::$ty_info;
1529
1530			fn size_hint(&self) -> usize {
1531				mem::size_of::<$t>()
1532			}
1533
1534			fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
1535				let buf = self.to_le_bytes();
1536				f(&buf[..])
1537			}
1538		}
1539
1540		impl Decode for $t {
1541			const TYPE_INFO: TypeInfo = TypeInfo::$ty_info;
1542
1543			fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1544				let mut buf = [0u8; mem::size_of::<$t>()];
1545				input.read(&mut buf)?;
1546				Ok(<$t>::from_le_bytes(buf))
1547			}
1548
1549			fn encoded_fixed_size() -> Option<usize> {
1550				Some(mem::size_of::<$t>())
1551			}
1552		}
1553
1554		impl DecodeWithMemTracking for $t {}
1555	)* }
1556}
1557macro_rules! impl_one_byte {
1558	( $( $t:ty; $ty_info:ident ),* ) => { $(
1559		impl EncodeLike for $t {}
1560
1561		impl Encode for $t {
1562			const TYPE_INFO: TypeInfo = TypeInfo::$ty_info;
1563
1564			fn size_hint(&self) -> usize {
1565				mem::size_of::<$t>()
1566			}
1567
1568			fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
1569				f(&[*self as u8][..])
1570			}
1571		}
1572
1573		impl Decode for $t {
1574			const TYPE_INFO: TypeInfo = TypeInfo::$ty_info;
1575
1576			fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1577				Ok(input.read_byte()? as $t)
1578			}
1579		}
1580
1581		impl DecodeWithMemTracking for $t {}
1582	)* }
1583}
1584
1585impl_endians!(u16; U16, u32; U32, u64; U64, u128; U128, i16; I16, i32; I32, i64; I64, i128; I128);
1586impl_one_byte!(u8; U8, i8; I8);
1587
1588impl_endians!(f32; F32, f64; F64);
1589
1590impl EncodeLike for bool {}
1591
1592impl Encode for bool {
1593	fn size_hint(&self) -> usize {
1594		mem::size_of::<bool>()
1595	}
1596
1597	fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
1598		f(&[*self as u8][..])
1599	}
1600}
1601
1602impl Decode for bool {
1603	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1604		let byte = input.read_byte()?;
1605		match byte {
1606			0 => Ok(false),
1607			1 => Ok(true),
1608			_ => Err("Invalid boolean representation".into()),
1609		}
1610	}
1611
1612	fn encoded_fixed_size() -> Option<usize> {
1613		Some(1)
1614	}
1615}
1616
1617impl DecodeWithMemTracking for bool {}
1618
1619impl Encode for Duration {
1620	fn size_hint(&self) -> usize {
1621		mem::size_of::<u64>() + mem::size_of::<u32>()
1622	}
1623
1624	fn encode(&self) -> Vec<u8> {
1625		let secs = self.as_secs();
1626		let nanos = self.subsec_nanos();
1627		(secs, nanos).encode()
1628	}
1629}
1630
1631impl Decode for Duration {
1632	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1633		let (secs, nanos) = <(u64, u32)>::decode(input)
1634			.map_err(|e| e.chain("Could not decode `Duration(u64, u32)`"))?;
1635		if nanos >= A_BILLION {
1636			Err("Could not decode `Duration`: Number of nanoseconds should not be higher than 10^9.".into())
1637		} else {
1638			Ok(Duration::new(secs, nanos))
1639		}
1640	}
1641}
1642
1643impl DecodeWithMemTracking for Duration {}
1644
1645impl EncodeLike for Duration {}
1646
1647impl<T> Encode for Range<T>
1648where
1649	T: Encode,
1650{
1651	fn size_hint(&self) -> usize {
1652		2 * mem::size_of::<T>()
1653	}
1654
1655	fn encode(&self) -> Vec<u8> {
1656		(&self.start, &self.end).encode()
1657	}
1658}
1659
1660impl<T> Decode for Range<T>
1661where
1662	T: Decode,
1663{
1664	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1665		let (start, end) =
1666			<(T, T)>::decode(input).map_err(|e| e.chain("Could not decode `Range<T>`"))?;
1667		Ok(Range { start, end })
1668	}
1669}
1670
1671impl<T: DecodeWithMemTracking> DecodeWithMemTracking for Range<T> {}
1672
1673impl<T> Encode for RangeInclusive<T>
1674where
1675	T: Encode,
1676{
1677	fn size_hint(&self) -> usize {
1678		2 * mem::size_of::<T>()
1679	}
1680
1681	fn encode(&self) -> Vec<u8> {
1682		(self.start(), self.end()).encode()
1683	}
1684}
1685
1686impl<T> Decode for RangeInclusive<T>
1687where
1688	T: Decode,
1689{
1690	fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
1691		let (start, end) =
1692			<(T, T)>::decode(input).map_err(|e| e.chain("Could not decode `RangeInclusive<T>`"))?;
1693		Ok(RangeInclusive::new(start, end))
1694	}
1695}
1696
1697impl<T: DecodeWithMemTracking> DecodeWithMemTracking for RangeInclusive<T> {}
1698
1699#[cfg(test)]
1700mod tests {
1701	use super::*;
1702	use std::borrow::Cow;
1703
1704	#[test]
1705	fn vec_is_sliceable() {
1706		let v = b"Hello world".to_vec();
1707		v.using_encoded(|ref slice| assert_eq!(slice, &b"\x0bHello world"));
1708	}
1709
1710	#[test]
1711	fn encode_borrowed_tuple() {
1712		let x = vec![1u8, 2, 3, 4];
1713		let y = 128i64;
1714
1715		let encoded = (&x, &y).encode();
1716
1717		assert_eq!((x, y), Decode::decode(&mut &encoded[..]).unwrap());
1718	}
1719
1720	#[test]
1721	fn cow_works() {
1722		let x = &[1u32, 2, 3, 4, 5, 6][..];
1723		let y = Cow::Borrowed(&x);
1724		assert_eq!(x.encode(), y.encode());
1725
1726		let z: Cow<'_, [u32]> = Cow::decode(&mut &x.encode()[..]).unwrap();
1727		assert_eq!(*z, *x);
1728	}
1729
1730	#[test]
1731	fn cow_string_works() {
1732		let x = "Hello world!";
1733		let y = Cow::Borrowed(&x);
1734		assert_eq!(x.encode(), y.encode());
1735
1736		let z: Cow<'_, str> = Cow::decode(&mut &x.encode()[..]).unwrap();
1737		assert_eq!(*z, *x);
1738	}
1739
1740	fn hexify(bytes: &[u8]) -> String {
1741		bytes
1742			.iter()
1743			.map(|ref b| format!("{:02x}", b))
1744			.collect::<Vec<String>>()
1745			.join(" ")
1746	}
1747
1748	#[test]
1749	fn string_encoded_as_expected() {
1750		let value = String::from("Hello, World!");
1751		let encoded = value.encode();
1752		assert_eq!(hexify(&encoded), "0d 48 65 6c 6c 6f 2c 20 57 6f 72 6c 64 21");
1753		assert_eq!(<String>::decode(&mut &encoded[..]).unwrap(), value);
1754	}
1755
1756	#[test]
1757	fn vec_of_u8_encoded_as_expected() {
1758		let value = vec![0u8, 1, 1, 2, 3, 5, 8, 13, 21, 34];
1759		let encoded = value.encode();
1760		assert_eq!(hexify(&encoded), "0a 00 01 01 02 03 05 08 0d 15 22");
1761		assert_eq!(<Vec<u8>>::decode(&mut &encoded[..]).unwrap(), value);
1762	}
1763
1764	#[test]
1765	fn vec_of_i16_encoded_as_expected() {
1766		let value = vec![0i16, 1, -1, 2, -2, 3, -3];
1767		let encoded = value.encode();
1768		assert_eq!(hexify(&encoded), "07 00 00 01 00 ff ff 02 00 fe ff 03 00 fd ff");
1769		assert_eq!(<Vec<i16>>::decode(&mut &encoded[..]).unwrap(), value);
1770	}
1771
1772	#[test]
1773	fn vec_of_option_int_encoded_as_expected() {
1774		let value = vec![Some(1i8), Some(-1), None];
1775		let encoded = value.encode();
1776		assert_eq!(hexify(&encoded), "03 01 01 01 ff 00");
1777		assert_eq!(<Vec<Option<i8>>>::decode(&mut &encoded[..]).unwrap(), value);
1778	}
1779
1780	#[test]
1781	fn vec_of_option_bool_encoded_as_expected() {
1782		let value = vec![OptionBool(Some(true)), OptionBool(Some(false)), OptionBool(None)];
1783		let encoded = value.encode();
1784		assert_eq!(hexify(&encoded), "03 01 02 00");
1785		assert_eq!(<Vec<OptionBool>>::decode(&mut &encoded[..]).unwrap(), value);
1786	}
1787
1788	#[test]
1789	fn vec_of_empty_tuples_encoded_as_expected() {
1790		let value = vec![(), (), (), (), ()];
1791		let encoded = value.encode();
1792		assert_eq!(hexify(&encoded), "05");
1793		assert_eq!(<Vec<()>>::decode(&mut &encoded[..]).unwrap(), value);
1794	}
1795
1796	#[cfg(feature = "bytes")]
1797	#[test]
1798	fn bytes_works_as_expected() {
1799		let input = bytes::Bytes::from_static(b"hello");
1800		let encoded = Encode::encode(&input);
1801		let encoded_vec = input.to_vec().encode();
1802		assert_eq!(encoded, encoded_vec);
1803
1804		assert_eq!(&b"hello"[..], bytes::Bytes::decode(&mut &encoded[..]).unwrap(),);
1805	}
1806
1807	#[cfg(feature = "bytes")]
1808	#[test]
1809	fn bytes_deserialized_from_bytes_is_zero_copy() {
1810		let encoded = bytes::Bytes::from(Encode::encode(&b"hello".to_vec()));
1811		let decoded = decode_from_bytes::<bytes::Bytes>(encoded.clone()).unwrap();
1812		assert_eq!(decoded, &b"hello"[..]);
1813
1814		// The `slice_ref` will panic if the `decoded` is not a subslice of `encoded`.
1815		assert_eq!(encoded.slice_ref(&decoded), &b"hello"[..]);
1816	}
1817
1818	#[cfg(feature = "bytes")]
1819	#[test]
1820	fn nested_bytes_deserialized_from_bytes_is_zero_copy() {
1821		let encoded = bytes::Bytes::from(Encode::encode(&Some(b"hello".to_vec())));
1822		let decoded = decode_from_bytes::<Option<bytes::Bytes>>(encoded.clone()).unwrap();
1823		let decoded = decoded.as_ref().unwrap();
1824		assert_eq!(decoded, &b"hello"[..]);
1825
1826		// The `slice_ref` will panic if the `decoded` is not a subslice of `encoded`.
1827		assert_eq!(encoded.slice_ref(decoded), &b"hello"[..]);
1828	}
1829
1830	fn test_encode_length<T: Encode + Decode + DecodeLength>(thing: &T, len: usize) {
1831		assert_eq!(<T as DecodeLength>::len(&thing.encode()[..]).unwrap(), len);
1832	}
1833
1834	#[test]
1835	fn len_works_for_decode_collection_types() {
1836		let vector = vec![10; 10];
1837		let mut btree_map: BTreeMap<u32, u32> = BTreeMap::new();
1838		btree_map.insert(1, 1);
1839		btree_map.insert(2, 2);
1840		let mut btree_set: BTreeSet<u32> = BTreeSet::new();
1841		btree_set.insert(1);
1842		btree_set.insert(2);
1843		let mut vd = VecDeque::new();
1844		vd.push_front(1);
1845		vd.push_front(2);
1846		let mut bh = BinaryHeap::new();
1847		bh.push(1);
1848		bh.push(2);
1849		let mut ll = LinkedList::new();
1850		ll.push_back(1);
1851		ll.push_back(2);
1852		let t1: (Vec<_>,) = (vector.clone(),);
1853		let t2: (Vec<_>, u32) = (vector.clone(), 3u32);
1854
1855		test_encode_length(&vector, 10);
1856		test_encode_length(&btree_map, 2);
1857		test_encode_length(&btree_set, 2);
1858		test_encode_length(&vd, 2);
1859		test_encode_length(&bh, 2);
1860		test_encode_length(&ll, 2);
1861		test_encode_length(&t1, 10);
1862		test_encode_length(&t2, 10);
1863	}
1864
1865	#[test]
1866	fn vec_of_string_encoded_as_expected() {
1867		let value = vec![
1868			"Hamlet".to_owned(),
1869			"Война и мир".to_owned(),
1870			"三国演义".to_owned(),
1871			"أَلْف لَيْلَة وَلَيْلَة‎".to_owned(),
1872		];
1873		let encoded = value.encode();
1874		assert_eq!(
1875			hexify(&encoded),
1876			"04 06 48 61 6d 6c 65 74 14 d0 92 d0 be d0 b9 d0 bd d0 b0 20 d0 \
1877			 b8 20 d0 bc d0 b8 d1 80 0c e4 b8 89 e5 9b bd e6 bc 94 e4 b9 89 2f d8 a3 d9 8e d9 84 d9 92 \
1878			 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 \
1879			 d9 84 d9 8e d8 a9 e2 80 8e"
1880		);
1881		assert_eq!(<Vec<String>>::decode(&mut &encoded[..]).unwrap(), value);
1882	}
1883
1884	#[derive(Debug, PartialEq)]
1885	struct MyWrapper(Compact<u32>);
1886	impl Deref for MyWrapper {
1887		type Target = Compact<u32>;
1888		fn deref(&self) -> &Self::Target {
1889			&self.0
1890		}
1891	}
1892	impl WrapperTypeEncode for MyWrapper {}
1893
1894	impl From<Compact<u32>> for MyWrapper {
1895		fn from(c: Compact<u32>) -> Self {
1896			MyWrapper(c)
1897		}
1898	}
1899	impl WrapperTypeDecode for MyWrapper {
1900		type Wrapped = Compact<u32>;
1901	}
1902
1903	#[test]
1904	fn should_work_for_wrapper_types() {
1905		let result = vec![3];
1906
1907		assert_eq!(MyWrapper(3u32.into()).encode(), result);
1908		assert_eq!(MyWrapper::decode(&mut &*result).unwrap(), MyWrapper(3_u32.into()));
1909	}
1910
1911	#[test]
1912	fn codec_vec_deque_u8_and_u16() {
1913		let mut v_u8 = VecDeque::new();
1914		let mut v_u16 = VecDeque::new();
1915
1916		for i in 0..50 {
1917			v_u8.push_front(i as u8);
1918			v_u16.push_front(i as u16);
1919		}
1920		for i in 50..100 {
1921			v_u8.push_back(i as u8);
1922			v_u16.push_back(i as u16);
1923		}
1924
1925		assert_eq!(Decode::decode(&mut &v_u8.encode()[..]), Ok(v_u8));
1926		assert_eq!(Decode::decode(&mut &v_u16.encode()[..]), Ok(v_u16));
1927	}
1928
1929	#[test]
1930	fn codec_iterator() {
1931		let t1: BTreeSet<u32> = FromIterator::from_iter((0..10).flat_map(|i| 0..i));
1932		let t2: LinkedList<u32> = FromIterator::from_iter((0..10).flat_map(|i| 0..i));
1933		let t3: BinaryHeap<u32> = FromIterator::from_iter((0..10).flat_map(|i| 0..i));
1934		let t4: BTreeMap<u16, u32> =
1935			FromIterator::from_iter((0..10).flat_map(|i| 0..i).map(|i| (i as u16, i + 10)));
1936		let t5: BTreeSet<Vec<u8>> = FromIterator::from_iter((0..10).map(|i| Vec::from_iter(0..i)));
1937		let t6: LinkedList<Vec<u8>> =
1938			FromIterator::from_iter((0..10).map(|i| Vec::from_iter(0..i)));
1939		let t7: BinaryHeap<Vec<u8>> =
1940			FromIterator::from_iter((0..10).map(|i| Vec::from_iter(0..i)));
1941		let t8: BTreeMap<Vec<u8>, u32> = FromIterator::from_iter(
1942			(0..10).map(|i| Vec::from_iter(0..i)).map(|i| (i.clone(), i.len() as u32)),
1943		);
1944
1945		assert_eq!(Decode::decode(&mut &t1.encode()[..]), Ok(t1));
1946		assert_eq!(Decode::decode(&mut &t2.encode()[..]), Ok(t2));
1947		assert_eq!(
1948			Decode::decode(&mut &t3.encode()[..]).map(BinaryHeap::into_sorted_vec),
1949			Ok(t3.into_sorted_vec()),
1950		);
1951		assert_eq!(Decode::decode(&mut &t4.encode()[..]), Ok(t4));
1952		assert_eq!(Decode::decode(&mut &t5.encode()[..]), Ok(t5));
1953		assert_eq!(Decode::decode(&mut &t6.encode()[..]), Ok(t6));
1954		assert_eq!(
1955			Decode::decode(&mut &t7.encode()[..]).map(BinaryHeap::into_sorted_vec),
1956			Ok(t7.into_sorted_vec()),
1957		);
1958		assert_eq!(Decode::decode(&mut &t8.encode()[..]), Ok(t8));
1959	}
1960
1961	#[test]
1962	fn io_reader() {
1963		let mut io_reader = IoReader(std::io::Cursor::new(&[1u8, 2, 3][..]));
1964
1965		let mut v = [0; 2];
1966		io_reader.read(&mut v[..]).unwrap();
1967		assert_eq!(v, [1, 2]);
1968
1969		assert_eq!(io_reader.read_byte().unwrap(), 3);
1970
1971		assert_eq!(io_reader.read_byte(), Err("io error: UnexpectedEof".into()));
1972	}
1973
1974	#[test]
1975	fn shared_references_implement_encode() {
1976		Arc::new(10u32).encode();
1977		Rc::new(10u32).encode();
1978	}
1979
1980	#[test]
1981	fn not_limit_input_test() {
1982		use crate::Input;
1983
1984		struct NoLimit<'a>(&'a [u8]);
1985
1986		impl Input for NoLimit<'_> {
1987			fn remaining_len(&mut self) -> Result<Option<usize>, Error> {
1988				Ok(None)
1989			}
1990
1991			fn read(&mut self, into: &mut [u8]) -> Result<(), Error> {
1992				self.0.read(into)
1993			}
1994		}
1995
1996		let len = INITIAL_PREALLOCATION * 2 + 1;
1997		let mut i = Compact(len as u32).encode();
1998		i.resize(i.len() + len, 0);
1999		assert_eq!(<Vec<u8>>::decode(&mut NoLimit(&i[..])).unwrap(), vec![0u8; len]);
2000
2001		let i = Compact(len as u32).encode();
2002		assert_eq!(
2003			<Vec<u8>>::decode(&mut NoLimit(&i[..])).err().unwrap().to_string(),
2004			"Not enough data to fill buffer",
2005		);
2006
2007		let i = Compact(1000u32).encode();
2008		assert_eq!(
2009			<Vec<u8>>::decode(&mut NoLimit(&i[..])).err().unwrap().to_string(),
2010			"Not enough data to fill buffer",
2011		);
2012	}
2013
2014	#[test]
2015	fn boolean() {
2016		assert_eq!(true.encode(), vec![1]);
2017		assert_eq!(false.encode(), vec![0]);
2018		assert!(bool::decode(&mut &[1][..]).unwrap());
2019		assert!(!bool::decode(&mut &[0][..]).unwrap());
2020	}
2021
2022	#[test]
2023	fn some_encode_like() {
2024		fn t<B: EncodeLike>() {}
2025		t::<&[u8]>();
2026		t::<&str>();
2027		t::<NonZeroU32>();
2028	}
2029
2030	#[test]
2031	fn vec_deque_encode_like_vec() {
2032		let data: VecDeque<u32> = vec![1, 2, 3, 4, 5, 6].into();
2033		let encoded = data.encode();
2034
2035		let decoded = Vec::<u32>::decode(&mut &encoded[..]).unwrap();
2036		assert!(decoded.iter().all(|v| data.contains(v)));
2037		assert_eq!(data.len(), decoded.len());
2038
2039		let encoded = decoded.encode();
2040		let decoded = VecDeque::<u32>::decode(&mut &encoded[..]).unwrap();
2041		assert_eq!(data, decoded);
2042	}
2043
2044	#[test]
2045	fn vec_decode_right_capacity() {
2046		let data: Vec<u32> = vec![1, 2, 3];
2047		let mut encoded = data.encode();
2048		encoded.resize(encoded.len() * 2, 0);
2049		let decoded = Vec::<u32>::decode(&mut &encoded[..]).unwrap();
2050		assert_eq!(data, decoded);
2051		assert_eq!(decoded.capacity(), decoded.len());
2052		// Check with non-integer type
2053		let data: Vec<String> = vec!["1".into(), "2".into(), "3".into()];
2054		let mut encoded = data.encode();
2055		encoded.resize(65536, 0);
2056		let decoded = Vec::<String>::decode(&mut &encoded[..]).unwrap();
2057		assert_eq!(data, decoded);
2058		assert_eq!(decoded.capacity(), decoded.len());
2059	}
2060
2061	#[test]
2062	fn duration() {
2063		let num_secs = 13;
2064		let num_nanos = 37;
2065
2066		let duration = Duration::new(num_secs, num_nanos);
2067		let expected = (num_secs, num_nanos).encode();
2068
2069		assert_eq!(duration.encode(), expected);
2070		assert_eq!(Duration::decode(&mut &expected[..]).unwrap(), duration);
2071	}
2072
2073	#[test]
2074	fn malformed_duration_encoding_fails() {
2075		// This test should fail, as the number of nanoseconds encoded is exactly 10^9.
2076		let invalid_nanos = A_BILLION;
2077		let encoded = (0u64, invalid_nanos).encode();
2078		assert!(Duration::decode(&mut &encoded[..]).is_err());
2079
2080		let num_secs = 1u64;
2081		let num_nanos = 37u32;
2082		let invalid_nanos = num_secs as u32 * A_BILLION + num_nanos;
2083		let encoded = (num_secs, invalid_nanos).encode();
2084		// This test should fail, as the number of nano seconds encoded is bigger than 10^9.
2085		assert!(Duration::decode(&mut &encoded[..]).is_err());
2086
2087		// Now constructing a valid duration and encoding it. Those asserts should not fail.
2088		let duration = Duration::from_nanos(invalid_nanos as u64);
2089		let expected = (num_secs, num_nanos).encode();
2090
2091		assert_eq!(duration.encode(), expected);
2092		assert_eq!(Duration::decode(&mut &expected[..]).unwrap(), duration);
2093	}
2094
2095	#[test]
2096	fn u64_max() {
2097		let num_secs = u64::MAX;
2098		let num_nanos = 0;
2099		let duration = Duration::new(num_secs, num_nanos);
2100		let expected = (num_secs, num_nanos).encode();
2101
2102		assert_eq!(duration.encode(), expected);
2103		assert_eq!(Duration::decode(&mut &expected[..]).unwrap(), duration);
2104	}
2105
2106	#[test]
2107	fn decoding_does_not_overflow() {
2108		let num_secs = u64::MAX;
2109		let num_nanos = A_BILLION;
2110
2111		// `num_nanos`' carry should make `num_secs` overflow if we were to call `Duration::new()`.
2112		// This test checks that the we do not call `Duration::new()`.
2113		let encoded = (num_secs, num_nanos).encode();
2114		assert!(Duration::decode(&mut &encoded[..]).is_err());
2115	}
2116
2117	#[test]
2118	fn string_invalid_utf8() {
2119		// `167, 10` is not a valid utf8 sequence, so this should be an error.
2120		let mut bytes: &[u8] = &[20, 114, 167, 10, 20, 114];
2121
2122		let obj = <String>::decode(&mut bytes);
2123		assert!(obj.is_err());
2124	}
2125
2126	#[test]
2127	fn empty_array_encode_and_decode() {
2128		let data: [u32; 0] = [];
2129		let encoded = data.encode();
2130		assert!(encoded.is_empty());
2131		<[u32; 0]>::decode(&mut &encoded[..]).unwrap();
2132	}
2133
2134	macro_rules! test_array_encode_and_decode {
2135		( $( $name:ty ),* $(,)? ) => {
2136			$(
2137				paste::item! {
2138					#[test]
2139					fn [<test_array_encode_and_decode _ $name>]() {
2140						let data: [$name; 32] = [123 as $name; 32];
2141						let encoded = data.encode();
2142						let decoded: [$name; 32] = Decode::decode(&mut &encoded[..]).unwrap();
2143						assert_eq!(decoded, data);
2144					}
2145				}
2146			)*
2147		}
2148	}
2149
2150	test_array_encode_and_decode!(u8, i8, u16, i16, u32, i32, u64, i64, u128, i128);
2151
2152	test_array_encode_and_decode!(f32, f64);
2153
2154	fn test_encoded_size(val: impl Encode) {
2155		let length = val.using_encoded(|v| v.len());
2156
2157		assert_eq!(length, val.encoded_size());
2158	}
2159
2160	struct TestStruct {
2161		data: Vec<u32>,
2162		other: u8,
2163		compact: Compact<u128>,
2164	}
2165
2166	impl Encode for TestStruct {
2167		fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
2168			self.data.encode_to(dest);
2169			self.other.encode_to(dest);
2170			self.compact.encode_to(dest);
2171		}
2172	}
2173
2174	#[test]
2175	fn encoded_size_works() {
2176		test_encoded_size(120u8);
2177		test_encoded_size(30u16);
2178		test_encoded_size(1u32);
2179		test_encoded_size(2343545u64);
2180		test_encoded_size(34358394245459854u128);
2181		test_encoded_size(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10u32]);
2182		test_encoded_size(Compact(32445u32));
2183		test_encoded_size(Compact(34353454453545u128));
2184		test_encoded_size(TestStruct {
2185			data: vec![1, 2, 4, 5, 6],
2186			other: 45,
2187			compact: Compact(123234545),
2188		});
2189	}
2190
2191	#[test]
2192	fn ranges() {
2193		let range = Range { start: 1, end: 100 };
2194		let range_bytes = (1, 100).encode();
2195		assert_eq!(range.encode(), range_bytes);
2196		assert_eq!(Range::decode(&mut &range_bytes[..]), Ok(range));
2197
2198		let range_inclusive = RangeInclusive::new(1, 100);
2199		let range_inclusive_bytes = (1, 100).encode();
2200		assert_eq!(range_inclusive.encode(), range_inclusive_bytes);
2201		assert_eq!(RangeInclusive::decode(&mut &range_inclusive_bytes[..]), Ok(range_inclusive));
2202	}
2203}