Newer
Older
// Copyright 2017, 2018 Parity Technologies
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::alloc::vec::Vec;
use crate::alloc::boxed::Box;
#[cfg(any(feature = "std", feature = "full"))]
use crate::alloc::{
string::String,
borrow::{Cow, ToOwned},
};
use core::{mem, slice};
use arrayvec::ArrayVec;
use core::marker::PhantomData;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#[cfg(feature = "std")]
use std::fmt;
#[cfg_attr(feature = "std", derive(Debug))]
#[derive(PartialEq)]
#[cfg(feature = "std")]
pub struct Error(&'static str);
#[cfg(not(feature = "std"))]
#[derive(PartialEq)]
pub struct Error;
impl Error {
#[cfg(feature = "std")]
pub fn what(&self) -> &'static str {
self.0
}
#[cfg(not(feature = "std"))]
pub fn what(&self) -> &'static str {
""
}
}
#[cfg(feature = "std")]
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {
fn description(&self) -> &str {
self.0
}
}
impl From<&'static str> for Error {
#[cfg(feature = "std")]
fn from(s: &'static str) -> Error {
return Error(s)
}
#[cfg(not(feature = "std"))]
fn from(_s: &'static str) -> Error {
return Error
}
}
/// Trait that allows reading of data into a slice.
pub trait Input {
/// Read into the provided input slice. Returns the number of bytes read.
fn read(&mut self, into: &mut [u8]) -> Result<usize, Error>;
fn read_byte(&mut self) -> Result<u8, Error> {
self.read(&mut buf[..])?;
Ok(buf[0])
}
}
#[cfg(not(feature = "std"))]
impl<'a> Input for &'a [u8] {
fn read(&mut self, into: &mut [u8]) -> Result<usize, Error> {
if into.len() > self.len() {
return Err("".into());
}
let len = ::core::cmp::min(into.len(), self.len());
into[..len].copy_from_slice(&self[..len]);
*self = &self[len..];
Ok(len)
}
}
#[cfg(feature = "std")]
impl From<std::io::Error> for Error {
fn from(_err: std::io::Error) -> Self {
"io error".into()
impl<R: std::io::Read> Input for R {
fn read(&mut self, into: &mut [u8]) -> Result<usize, Error> {
(self as &mut dyn std::io::Read).read_exact(into)?;
Ok(into.len())
struct PrefixInput<'a, T> {
prefix: Option<u8>,
input: &'a mut T,
}
impl<'a, T: 'a + Input> Input for PrefixInput<'a, T> {
fn read(&mut self, buffer: &mut [u8]) -> Result<usize, Error> {
match self.prefix.take() {
Some(v) if buffer.len() > 0 => {
buffer[0] = v;
let res = 1 + self.input.read(&mut buffer[1..])?;
Ok(res)
}
_ => self.input.read(buffer)
}
}
}
/// Trait that allows writing of data.
pub trait Output: Sized {
/// Write to the output.
fn write(&mut self, bytes: &[u8]);
fn push_byte(&mut self, byte: u8) {
self.write(&[byte]);
}
fn push<V: Encode + ?Sized>(&mut self, value: &V) {
value.encode_to(self);
}
}
#[cfg(not(feature = "std"))]
impl Output for Vec<u8> {
fn write(&mut self, bytes: &[u8]) {
self.extend(bytes);
}
}
#[cfg(feature = "std")]
impl<W: std::io::Write> Output for W {
(self as &mut dyn std::io::Write).write_all(bytes).expect("Codec outputs are infallible");
struct ArrayVecWrapper<T: arrayvec::Array>(ArrayVec<T>);
impl<T: arrayvec::Array<Item=u8>> Output for ArrayVecWrapper<T> {
fn write(&mut self, bytes: &[u8]) {
for byte in bytes {
self.push_byte(*byte);
}
}
fn push_byte(&mut self, byte: u8) {
self.0.push(byte);
}
}
/// Trait that allows zero-copy write of value-references to slices in LE format.
/// Implementations should override `using_encoded` for value types and `encode_to` for allocating types.
pub trait Encode {
/// Convert self to a slice and append it to the destination.
fn encode_to<T: Output>(&self, dest: &mut T) {
self.using_encoded(|buf| dest.write(buf));
}
/// Convert self to an owned vector.
fn encode(&self) -> Vec<u8> {
let mut r = Vec::new();
self.encode_to(&mut r);
r
}
/// Convert self to a slice and then invoke the given closure with it.
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
f(&self.encode())
}
}
/// Trait that allows zero-copy read of value-references from slices in LE format.
pub trait Decode: Sized {
/// Attempt to deserialise the value from input.
fn decode<I: Input>(value: &mut I) -> Result<Self, Error>;
}
/// Trait that allows zero-copy read/write of value-references to/from slices in LE format.
pub trait Codec: Decode + Encode {}
/// Compact-encoded variant of T. This is more space-efficient but less compute-efficient.
#[derive(Eq, PartialEq, Clone, Copy, Ord, PartialOrd)]
impl<T> From<T> for Compact<T> {
fn from(x: T) -> Compact<T> { Compact(x) }
}
impl<'a, T: Copy> From<&'a T> for Compact<T> {
fn from(x: &'a T) -> Compact<T> { Compact(*x) }
}
/// Allow foreign structs to be wrap in Compact
type As;
fn encode_as(&self) -> &Self::As;
fn decode_from(_: Self::As) -> Self;
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
}
impl<T> Encode for Compact<T>
where
T: CompactAs,
for<'a> CompactRef<'a, <T as CompactAs>::As>: Encode,
{
fn encode_to<W: Output>(&self, dest: &mut W) {
CompactRef(self.0.encode_as()).encode_to(dest)
}
}
impl<'a, T> Encode for CompactRef<'a, T>
where
T: CompactAs,
for<'b> CompactRef<'b, <T as CompactAs>::As>: Encode,
{
fn encode_to<Out: Output>(&self, dest: &mut Out) {
CompactRef(self.0.encode_as()).encode_to(dest)
}
}
impl<T> Decode for Compact<T>
where
T: CompactAs,
Compact<<T as CompactAs>::As>: Decode,
{
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
Compact::<T::As>::decode(input)
.map(|x| Compact(<T as CompactAs>::decode_from(x.0)))
}
}
macro_rules! impl_from_compact {
( $( $ty:ty ),* ) => {
$(
impl From<Compact<$ty>> for $ty {
fn from(x: Compact<$ty>) -> $ty { x.0 }
}
)*
}
impl_from_compact! { (), u8, u16, u32, u64, u128 }
/// Compact-encoded variant of &'a T. This is more space-efficient but less compute-efficient.
#[derive(Eq, PartialEq, Clone, Copy)]
pub struct CompactRef<'a, T>(pub &'a T);
impl<'a, T> From<&'a T> for CompactRef<'a, T> {
fn from(x: &'a T) -> Self { CompactRef(x) }
}
impl<T> ::core::fmt::Debug for Compact<T> where T: ::core::fmt::Debug {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
impl<T> serde::Serialize for Compact<T> where T: serde::Serialize {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
T::serialize(&self.0, serializer)
}
}
#[cfg(feature = "std")]
impl<'de, T> serde::Deserialize<'de> for Compact<T> where T: serde::Deserialize<'de> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> {
pub trait MaybeDebugSerde: core::fmt::Debug + serde::Serialize + for<'a> serde::Deserialize<'a> {}
impl<T> MaybeDebugSerde for T where T: core::fmt::Debug + serde::Serialize + for<'a> serde::Deserialize<'a> {}
#[cfg(not(feature = "std"))]
pub trait MaybeDebugSerde {}
#[cfg(not(feature = "std"))]
impl<T> MaybeDebugSerde for T {}
/// Trait that tells you if a given type can be encoded/decoded in a compact way.
/// The compact type; this can be
type Type: for<'a> EncodeAsRef<'a, Self> + Decode + From<Self> + Into<Self> + Clone +
PartialEq + Eq + MaybeDebugSerde;
/// Something that can be encoded as a reference.
pub trait EncodeAsRef<'a, T: 'a> {
/// The reference type that is used for encoding.
type RefType: Encode + From<&'a T>;
}
impl<'a, T: 'a> EncodeAsRef<'a, T> for Compact<T> where CompactRef<'a, T>: Encode + From<&'a T> {
type RefType = CompactRef<'a, T>;
}
impl<T: 'static> HasCompact for T where
Compact<T>: for<'a> EncodeAsRef<'a, T> + Decode + From<Self> + Into<Self> + Clone +
PartialEq + Eq + MaybeDebugSerde,
type Type = Compact<T>;
}
// compact encoding:
// 0b00 00 00 00 / 00 00 00 00 / 00 00 00 00 / 00 00 00 00
// xx xx xx 00 (0 .. 2**6) (u8)
// yL yL yL 01 / yH yH yH yL (2**6 .. 2**14) (u8, u16) low LH high
// zL zL zL 10 / zM zM zM zL / zM zM zM zM / zH zH zH zM (2**14 .. 2**30) (u16, u32) low LMMH high
// nn nn nn 11 [ / zz zz zz zz ]{4 + n} (2**30 .. 2**536) (u32, u64, u128, U256, U512, U520) straight LE-encoded
// Note: we use *LOW BITS* of the LSB in LE encoding to encode the 2 bit key.
impl<'a> Encode for CompactRef<'a, ()> {
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
f(&[])
}
}
impl Encode for Compact<()> {
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
f(&[])
}
}
impl<'a> Encode for CompactRef<'a, u8> {
fn encode_to<W: Output>(&self, dest: &mut W) {
match self.0 {
0..=0b00111111 => dest.push_byte(self.0 << 2),
_ => (((*self.0 as u16) << 2) | 0b01).encode_to(dest),
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
let mut r = ArrayVecWrapper(ArrayVec::<[u8; 2]>::new());
self.encode_to(&mut r);
f(&r.0)
}
impl Encode for Compact<u8> {
fn encode_to<W: Output>(&self, dest: &mut W) {
CompactRef(&self.0).encode_to(dest)
}
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
let mut r = ArrayVecWrapper(ArrayVec::<[u8; 2]>::new());
self.encode_to(&mut r);
f(&r.0)
}
}
impl<'a> Encode for CompactRef<'a, u16> {
fn encode_to<W: Output>(&self, dest: &mut W) {
match self.0 {
0..=0b00111111 => dest.push_byte((*self.0 as u8) << 2),
0..=0b00111111_11111111 => ((*self.0 << 2) | 0b01).encode_to(dest),
_ => (((*self.0 as u32) << 2) | 0b10).encode_to(dest),
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
let mut r = ArrayVecWrapper(ArrayVec::<[u8; 4]>::new());
self.encode_to(&mut r);
f(&r.0)
}
impl Encode for Compact<u16> {
fn encode_to<W: Output>(&self, dest: &mut W) {
CompactRef(&self.0).encode_to(dest)
}
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
let mut r = ArrayVecWrapper(ArrayVec::<[u8; 4]>::new());
self.encode_to(&mut r);
f(&r.0)
}
}
impl<'a> Encode for CompactRef<'a, u32> {
fn encode_to<W: Output>(&self, dest: &mut W) {
match self.0 {
0..=0b00111111 => dest.push_byte((*self.0 as u8) << 2),
0..=0b00111111_11111111 => (((*self.0 as u16) << 2) | 0b01).encode_to(dest),
0..=0b00111111_11111111_11111111_11111111 => ((*self.0 << 2) | 0b10).encode_to(dest),
_ => {
dest.push_byte(0b11);
self.0.encode_to(dest);
}
}
}
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
let mut r = ArrayVecWrapper(ArrayVec::<[u8; 5]>::new());
self.encode_to(&mut r);
f(&r.0)
}
impl Encode for Compact<u32> {
fn encode_to<W: Output>(&self, dest: &mut W) {
CompactRef(&self.0).encode_to(dest)
}
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
let mut r = ArrayVecWrapper(ArrayVec::<[u8; 5]>::new());
self.encode_to(&mut r);
f(&r.0)
}
}
impl<'a> Encode for CompactRef<'a, u64> {
fn encode_to<W: Output>(&self, dest: &mut W) {
match self.0 {
0..=0b00111111 => dest.push_byte((*self.0 as u8) << 2),
0..=0b00111111_11111111 => (((*self.0 as u16) << 2) | 0b01).encode_to(dest),
0..=0b00111111_11111111_11111111_11111111 => (((*self.0 as u32) << 2) | 0b10).encode_to(dest),
let bytes_needed = 8 - self.0.leading_zeros() / 8;
assert!(bytes_needed >= 4, "Previous match arm matches anyting less than 2^30; qed");
dest.push_byte(0b11 + ((bytes_needed - 4) << 2) as u8);
let mut v = *self.0;
for _ in 0..bytes_needed {
dest.push_byte(v as u8);
v >>= 8;
}
assert_eq!(v, 0, "shifted sufficient bits right to lead only leading zeros; qed")
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
let mut r = ArrayVecWrapper(ArrayVec::<[u8; 9]>::new());
self.encode_to(&mut r);
f(&r.0)
}
impl Encode for Compact<u64> {
fn encode_to<W: Output>(&self, dest: &mut W) {
CompactRef(&self.0).encode_to(dest)
}
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
let mut r = ArrayVecWrapper(ArrayVec::<[u8; 9]>::new());
self.encode_to(&mut r);
f(&r.0)
}
}
impl<'a> Encode for CompactRef<'a, u128> {
fn encode_to<W: Output>(&self, dest: &mut W) {
match self.0 {
0..=0b00111111 => dest.push_byte((*self.0 as u8) << 2),
0..=0b00111111_11111111 => (((*self.0 as u16) << 2) | 0b01).encode_to(dest),
0..=0b00111111_11111111_11111111_11111111 => (((*self.0 as u32) << 2) | 0b10).encode_to(dest),
let bytes_needed = 16 - self.0.leading_zeros() / 8;
assert!(bytes_needed >= 4, "Previous match arm matches anyting less than 2^30; qed");
dest.push_byte(0b11 + ((bytes_needed - 4) << 2) as u8);
let mut v = *self.0;
for _ in 0..bytes_needed {
dest.push_byte(v as u8);
v >>= 8;
}
assert_eq!(v, 0, "shifted sufficient bits right to lead only leading zeros; qed")
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
let mut r = ArrayVecWrapper(ArrayVec::<[u8; 17]>::new());
self.encode_to(&mut r);
f(&r.0)
}
impl Encode for Compact<u128> {
fn encode_to<W: Output>(&self, dest: &mut W) {
CompactRef(&self.0).encode_to(dest)
}
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
let mut r = ArrayVecWrapper(ArrayVec::<[u8; 17]>::new());
self.encode_to(&mut r);
f(&r.0)
}
fn decode<I: Input>(_input: &mut I) -> Result<Self, Error> {
Ok(Compact(()))
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
Ok(Compact(match prefix % 4 {
0 => prefix as u8 >> 2,
1 => {
let x = u16::decode(&mut PrefixInput{prefix: Some(prefix), input})? >> 2;
if x < 256 {
x as u8
} else {
return Err("out of range decoding Compact<u8>".into());
_ => return Err("unexpected prefix decoding Compact<u8>".into()),
}))
}
}
impl Decode for Compact<u16> {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
Ok(Compact(match prefix % 4 {
0 => prefix as u16 >> 2,
1 => u16::decode(&mut PrefixInput{prefix: Some(prefix), input})? as u16 >> 2,
2 => {
let x = u32::decode(&mut PrefixInput{prefix: Some(prefix), input})? >> 2;
if x < 65536 {
x as u16
} else {
return Err("out of range decoding Compact<u16>".into());
_ => return Err("unexpected prefix decoding Compact<u16>".into()),
}))
}
}
impl Decode for Compact<u32> {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
Ok(Compact(match prefix % 4 {
0 => prefix as u32 >> 2,
1 => u16::decode(&mut PrefixInput{prefix: Some(prefix), input})? as u32 >> 2,
2 => u32::decode(&mut PrefixInput{prefix: Some(prefix), input})? as u32 >> 2,
3|_ => { // |_. yeah, i know.
if prefix >> 2 == 0 {
// just 4 bytes. ok.
u32::decode(input)?
} else {
// Out of range for a 32-bit quantity.
return Err("out of range decoding Compact<u32>".into());
impl Decode for Compact<u64> {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
let prefix = input.read_byte()?;
Ok(Compact(match prefix % 4 {
0 => prefix as u64 >> 2,
1 => u16::decode(&mut PrefixInput{prefix: Some(prefix), input})? as u64 >> 2,
2 => u32::decode(&mut PrefixInput{prefix: Some(prefix), input})? as u64 >> 2,
3|_ => match (prefix >> 2) + 4 {
4 => u32::decode(input)? as u64,
8 => u64::decode(input)?,
x if x > 8 => return Err("unexpected prefix decoding Compact<u64>".into()),
bytes_needed => {
let mut res = 0;
for i in 0..bytes_needed {
res |= (input.read_byte()? as u64) << (i * 8);
}
res
}))
}
}
impl Decode for Compact<u128> {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
let prefix = input.read_byte()?;
Ok(Compact(match prefix % 4 {
0 => prefix as u128 >> 2,
1 => u16::decode(&mut PrefixInput{prefix: Some(prefix), input})? as u128 >> 2,
2 => u32::decode(&mut PrefixInput{prefix: Some(prefix), input})? as u128 >> 2,
3|_ => match (prefix >> 2) + 4 {
4 => u32::decode(input)? as u128,
8 => u64::decode(input)? as u128,
16 => u128::decode(input)?,
x if x > 16 => return Err("unexpected prefix decoding Compact<u128>".into()),
bytes_needed => {
let mut res = 0;
for i in 0..bytes_needed {
res |= (input.read_byte()? as u128) << (i * 8);
}
res
}
}
}))
}
}
impl<S: Decode + Encode> Codec for S {}
impl<T: Encode, E: Encode> Encode for Result<T, E> {
fn encode_to<W: Output>(&self, dest: &mut W) {
match *self {
Ok(ref t) => {
dest.push_byte(0);
t.encode_to(dest);
}
Err(ref e) => {
dest.push_byte(1);
e.encode_to(dest);
}
}
}
}
impl<T: Decode, E: Decode> Decode for Result<T, E> {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
0 => Ok(Ok(T::decode(input)?)),
1 => Ok(Err(E::decode(input)?)),
_ => Err("unexpected first byte decoding Result".into()),
}
}
}
/// Shim type because we can't do a specialised implementation for `Option<bool>` directly.
impl core::fmt::Debug for OptionBool {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
impl Encode for OptionBool {
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
f(&[match *self {
OptionBool(None) => 0u8,
OptionBool(Some(true)) => 1u8,
OptionBool(Some(false)) => 2u8,
}])
}
}
impl Decode for OptionBool {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
0 => Ok(OptionBool(None)),
1 => Ok(OptionBool(Some(true))),
2 => Ok(OptionBool(Some(false))),
_ => Err("unexpected first byte decoding OptionBool".into()),
}
}
}
impl<T: Encode> Encode for Option<T> {
fn encode_to<W: Output>(&self, dest: &mut W) {
match *self {
Some(ref t) => {
dest.push_byte(1);
t.encode_to(dest);
}
None => dest.push_byte(0),
}
}
}
impl<T: Decode> Decode for Option<T> {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
0 => Ok(None),
1 => Ok(Some(T::decode(input)?)),
_ => Err("unexpecded first byte decoding Option".into()),
( $( $n:expr, )* ) => { $(
impl<T: Encode> Encode for [T; $n] {
fn encode_to<W: Output>(&self, dest: &mut W) {
for item in self.iter() {
item.encode_to(dest);
}
}
}
impl<T: Decode> Decode for [T; $n] {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
let mut r = ArrayVec::new();
for _ in 0..$n {
r.push(T::decode(input)?);
}
let i = r.into_inner();
match i {
Ok(a) => Ok(a),
Err(_) => Err("failed to get inner array from ArrayVec".into()),
}
impl_array!(
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108,
109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124,
125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140,
141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156,
157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172,
173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188,
189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204,
205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220,
221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236,
237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252,
253, 254, 255, 256, 384, 512, 768, 1024, 2048, 4096, 8192, 16384, 32768,
);
impl<T: Encode> Encode for Box<T> {
fn encode_to<W: Output>(&self, dest: &mut W) {
self.as_ref().encode_to(dest)
}
}
impl<T: Decode> Decode for Box<T> {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
Ok(Box::new(T::decode(input)?))
}
}
impl Encode for [u8] {
fn encode_to<W: Output>(&self, dest: &mut W) {
let len = self.len();
assert!(len <= u32::max_value() as usize, "Attempted to serialize a collection with too many elements.");
dest.write(self)
}
}
impl Encode for Vec<u8> {
fn encode_to<W: Output>(&self, dest: &mut W) {
self.as_slice().encode_to(dest)
}
}
impl Decode for Vec<u8> {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
<Compact<u32>>::decode(input).and_then(move |Compact(len)| {
let len = len as usize;
let mut vec = vec![0; len];
input.read(&mut vec[..len])?;
Ok(vec)
})
}
}
impl<'a> Encode for &'a str {
fn encode_to<W: Output>(&self, dest: &mut W) {
self.as_bytes().encode_to(dest)
}
}
#[cfg(any(feature = "std", feature = "full"))]
impl<'a, T: ToOwned + ?Sized + 'a> Encode for Cow<'a, T> where
Cow::Owned(ref x) => x.encode_to(dest),
Cow::Borrowed(x) => x.encode_to(dest),
#[cfg(any(feature = "std", feature = "full"))]
impl<'a, T: ToOwned + ?Sized> Decode for Cow<'a, T>
where
<T as ToOwned>::Owned: Decode,
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
Ok(Cow::Owned(Decode::decode(input)?))
impl<T> Encode for PhantomData<T> {
fn encode_to<W: Output>(&self, _dest: &mut W) {}
impl<T> Decode for PhantomData<T> {
fn decode<I: Input>(_input: &mut I) -> Result<Self, Error> {
Ok(PhantomData)
#[cfg(any(feature = "std", feature = "full"))]
impl Encode for String {
fn encode_to<W: Output>(&self, dest: &mut W) {
self.as_bytes().encode_to(dest)
}
}
#[cfg(any(feature = "std", feature = "full"))]
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
Ok(Self::from_utf8_lossy(&Vec::decode(input)?).into())
}
}
impl<T: Encode> Encode for [T] {
fn encode_to<W: Output>(&self, dest: &mut W) {
let len = self.len();
assert!(len <= u32::max_value() as usize, "Attempted to serialize a collection with too many elements.");
for item in self {
item.encode_to(dest);
}
}
}
impl<T: Encode> Encode for Vec<T> {
fn encode_to<W: Output>(&self, dest: &mut W) {
self.as_slice().encode_to(dest)
}
}
impl<T: Decode> Decode for Vec<T> {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
<Compact<u32>>::decode(input).and_then(move |Compact(len)| {
let mut r = Vec::with_capacity(len as usize);
for _ in 0..len {
r.push(T::decode(input)?);
}
impl<K: Encode + Ord, V: Encode> Encode for BTreeMap<K, V> {
fn encode_to<W: Output>(&self, dest: &mut W) {
let len = self.len();
assert!(len <= u32::max_value() as usize, "Attempted to serialize a collection with too many elements.");
(len as u32).encode_to(dest);
for i in self.iter() {
i.encode_to(dest);
}
}
}
impl<K: Decode + Ord, V: Decode> Decode for BTreeMap<K, V> {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
u32::decode(input).and_then(move |len| {
let mut r: BTreeMap<K, V> = BTreeMap::new();
for _ in 0..len {
let (key, v) = <(K, V)>::decode(input)?;
r.insert(key, v);
}
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
impl Encode for () {
fn encode_to<T: Output>(&self, _dest: &mut T) {
}
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
f(&[])
}
fn encode(&self) -> Vec<u8> {
Vec::new()
}
}
impl<'a, T: 'a + Encode + ?Sized> Encode for &'a T {
fn encode_to<D: Output>(&self, dest: &mut D) {
(&**self).encode_to(dest)
}
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
(&**self).using_encoded(f)
}
fn encode(&self) -> Vec<u8> {
(&**self).encode()
}
}
impl Decode for () {
fn decode<I: Input>(_: &mut I) -> Result<(), Error> {
Ok(())
}
}
macro_rules! tuple_impl {
($one:ident,) => {
impl<$one: Encode> Encode for ($one,) {
fn encode_to<T: Output>(&self, dest: &mut T) {
self.0.encode_to(dest);
}
}
impl<$one: Decode> Decode for ($one,) {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
Err(e) => Err(e),
Ok($one) => Ok(($one,)),
}
}
}
};
($first:ident, $($rest:ident,)+) => {
impl<$first: Encode, $($rest: Encode),+>
Encode for
($first, $($rest),+) {
fn encode_to<T: Output>(&self, dest: &mut T) {
let (
ref $first,
$(ref $rest),+
) = *self;
$first.encode_to(dest);
$($rest.encode_to(dest);)+
}
}
impl<$first: Decode, $($rest: Decode),+>
Decode for
($first, $($rest),+) {
fn decode<INPUT: Input>(input: &mut INPUT) -> Result<Self, super::Error> {
Ok((
Ok(x) => x,
Err(e) => return Err(e),
Ok(x) => x,
Err(e) => return Err(e),
},)+
))
}
}
tuple_impl!($($rest,)+);
}
}
#[allow(non_snake_case)]
mod inner_tuple_impl {
use super::{Error, Input, Output, Decode, Encode};
tuple_impl!(A, B, C, D, E, F, G, H, I, J, K,);
}
/// Trait to allow conversion to a know endian representation when sensitive.
/// Types implementing this trait must have a size > 0.
// note: the copy bound and static lifetimes are necessary for safety of `Codec` blanket
// implementation.
trait EndianSensitive: Copy + 'static {
fn to_le(self) -> Self { self }