Crate parity_scale_codec[][src]

Parity SCALE Codec

Rust implementation of the SCALE (Simple Concatenated Aggregate Little-Endian) data format for types used in the Parity Substrate framework.

SCALE is a light-weight format which allows encoding (and decoding) which makes it highly suitable for resource-constrained execution environments like blockchain runtimes and low-power, low-memory devices.

It is important to note that the encoding context (knowledge of how the types and data structures look) needs to be known separately at both encoding and decoding ends. The encoded data does not include this contextual information.

To get a better understanding of how the encoding is done for different types, take a look at the low-level data formats overview page at the Substrate docs site.

Implementation

The codec is implemented using the following traits:

Encode

The Encode trait is used for encoding of data into the SCALE format. The Encode trait contains the following functions:

Note: Implementations should override using_encoded for value types and encode_to for allocating types. size_hint should be implemented for all types, wherever possible. Wrapper types should override all methods.

Decode

The Decode trait is used for deserialization/decoding of encoded data into the respective types.

CompactAs

The CompactAs trait is used for wrapping custom types/structs as compact types, which makes them even more space/memory efficient. The compact encoding is described here.

HasCompact

The HasCompact trait, if implemented, tells that the corresponding type is a compact encode-able type.

EncodeLike

The EncodeLike trait needs to be implemented for each type manually. When using derive, it is done automatically for you. Basically the trait gives you the opportunity to accept multiple types to a function that all encode to the same representation.

Usage Examples

Following are some examples to demonstrate usage of the codec.

Simple types


use parity_scale_codec::{Encode, Decode};

#[derive(Debug, PartialEq, Encode, Decode)]
enum EnumType {
	#[codec(index = "15")]
	A,
	B(u32, u64),
	C {
		a: u32,
		b: u64,
	},
}

let a = EnumType::A;
let b = EnumType::B(1, 2);
let c = EnumType::C { a: 1, b: 2 };

a.using_encoded(|ref slice| {
    assert_eq!(slice, &b"\x0f");
});

b.using_encoded(|ref slice| {
    assert_eq!(slice, &b"\x01\x01\0\0\0\x02\0\0\0\0\0\0\0");
});

c.using_encoded(|ref slice| {
    assert_eq!(slice, &b"\x02\x01\0\0\0\x02\0\0\0\0\0\0\0");
});

let mut da: &[u8] = b"\x0f";
assert_eq!(EnumType::decode(&mut da).ok(), Some(a));

let mut db: &[u8] = b"\x01\x01\0\0\0\x02\0\0\0\0\0\0\0";
assert_eq!(EnumType::decode(&mut db).ok(), Some(b));

let mut dc: &[u8] = b"\x02\x01\0\0\0\x02\0\0\0\0\0\0\0";
assert_eq!(EnumType::decode(&mut dc).ok(), Some(c));

let mut dz: &[u8] = &[0];
assert_eq!(EnumType::decode(&mut dz).ok(), None);

Compact type with HasCompact


use parity_scale_codec::{Encode, Decode, Compact, HasCompact};

#[derive(Debug, PartialEq, Encode, Decode)]
struct Test1CompactHasCompact<T: HasCompact> {
    #[codec(compact)]
    bar: T,
}

#[derive(Debug, PartialEq, Encode, Decode)]
struct Test1HasCompact<T: HasCompact> {
    #[codec(encoded_as = "<T as HasCompact>::Type")]
    bar: T,
}

let test_val: (u64, usize) = (0u64, 1usize);

let encoded = Test1HasCompact { bar: test_val.0 }.encode();
assert_eq!(encoded.len(), test_val.1);
assert_eq!(<Test1CompactHasCompact<u64>>::decode(&mut &encoded[..]).unwrap().bar, test_val.0);

Type with CompactAs


use serde_derive::{Serialize, Deserialize};
use parity_scale_codec::{Encode, Decode, Compact, HasCompact, CompactAs};

#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
#[derive(PartialEq, Eq, Clone)]
struct StructHasCompact(u32);

impl CompactAs for StructHasCompact {
    type As = u32;

    fn encode_as(&self) -> &Self::As {
        &12
    }

    fn decode_from(_: Self::As) -> Self {
        StructHasCompact(12)
    }
}

impl From<Compact<StructHasCompact>> for StructHasCompact {
    fn from(_: Compact<StructHasCompact>) -> Self {
        StructHasCompact(12)
    }
}

#[derive(Debug, PartialEq, Encode, Decode)]
enum TestGenericHasCompact<T> {
    A {
        #[codec(compact)] a: T
    },
}

let a = TestGenericHasCompact::A::<StructHasCompact> {
    a: StructHasCompact(12325678),
};

let encoded = a.encode();
assert_eq!(encoded.len(), 2);

Derive attributes

The derive implementation supports the following attributes:

Structs

Compact

Compact-encoded variant of T. This is more space-efficient but less compute-efficient.

Error

Descriptive error type

IoReader

Wrapper that implements Input for any Read and Seek type.

OptionBool

Shim type because we can’t do a specialised implementation for Option<bool> directly.

Ref

Reference wrapper that implement encode like any type that is encoded like its inner type.

Traits

Codec

Trait that allows zero-copy read/write of value-references to/from slices in LE format.

CompactAs

Allow foreign structs to be wrap in Compact

CompactLen

Something that can return the compact encoded length for a given value.

Decode

Trait that allows zero-copy read of value-references from slices in LE format.

DecodeAll

Extension trait to Decode that ensures that the given input data is consumed completly while decoding.

DecodeLength

Trait that allows the length of a collection to be read, without having to read and decode the entire elements.

DecodeLimit

Extension trait to Decode that ensures that the given input data is consumed completly while decoding.

Encode

Trait that allows zero-copy write of value-references to slices in LE format.

EncodeAppend

Trait that allows to append items to an encoded representation without decoding all previous added items.

EncodeAsRef

Something that can be encoded as a reference.

EncodeLike

A marker trait that tells the compiler that a type encode to the same representation as another type.

FullCodec

Trait that bound EncodeLike along with Codec. Usefull for generic being used in function with EncodeLike parameters.

FullEncode

Trait that bound EncodeLike along with Encode. Usefull for generic being used in function with EncodeLike parameters.

HasCompact

Trait that tells you if a given type can be encoded/decoded in a compact way.

Input

Trait that allows reading of data into a slice.

Joiner

Trait to allow itself to be serialised into a value which can be extended by bytes.

KeyedVec

Trait to allow itself to be serialised and prepended by a given slice.

Output

Trait that allows writing of data.

WrapperTypeEncode

A marker trait for types that wrap other encodable type.

Derive Macros

CompactAs

Derive parity_scale_codec::Compact and parity_scale_codec::CompactAs for struct with single field.

Decode

Derive parity_scale_codec::Decode and for struct and enum.

Encode

Derive parity_scale_codec::Encode and parity_scale_codec::EncodeLike for struct and enum.