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.
The codec is implemented using the following traits:
Encode trait is used for encoding of data into the SCALE format. The
Encode trait contains the following functions:
size_hint(&self) -> usize: Gets the capacity (in bytes) required for the encoded data. This is to avoid double-allocation of memory needed for the encoding. It can be an estimate and does not need to be an exact number. If the size is not known, even no good maximum, then we can skip this function from the trait implementation. This is required to be a cheap operation, so should not involve iterations etc.
encode_to<T: Output>(&self, dest: &mut T): Encodes the value and appends it to a destination buffer.
encode(&self) -> Vec<u8>: Encodes the type data and returns a slice.
using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R: Encodes the type data and executes a closure on the encoded value. Returns the result from the executed closure.
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 trait is used for deserialization/decoding of encoded data into the respective types.
fn decode<I: Input>(value: &mut I) -> Result<Self, Error>: Tries to decode the value from SCALE format to the type it is called on. Returns an
Errif the decoding fails.
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.
encode_as(&self) -> &Self::As: Encodes the type (self) as a compact type. The type
Asis defined in the same trait and its implementation should be compact encode-able.
decode_from(_: Self::As) -> Result<Self, Error>: Decodes the type (self) from a compact encode-able type.
HasCompact trait, if implemented, tells that the corresponding type is a compact encode-able type.
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.
Following are some examples to demonstrate usage of the codec.
use ; use ; let a = A; let b = B; let c = C ; a.using_encoded; b.using_encoded; c.using_encoded; let mut da: & = b"\x0f"; assert_eq!; let mut db: & = b"\x01\x01\0\0\0\x02\0\0\0\0\0\0\0"; assert_eq!; let mut dc: & = b"\x02\x01\0\0\0\x02\0\0\0\0\0\0\0"; assert_eq!; let mut dz: & = &; assert_eq!;
Compact type with HasCompact
use ; use ; let test_val: = ; let encoded = Test1HasCompact .encode; assert_eq!; assert_eq!;
Type with CompactAs
use ; use ; ; let a = ; let encoded = a.encode; assert_eq!;
The derive implementation supports the following attributes:
codec(dumb_trait_bound): This attribute needs to be placed above the type that one of the trait should be implemented for. It will make the algorithm that determines the to-add trait bounds fall back to just use the type parameters of the type. This can be useful for situation where the algorithm includes private types in the public interface. By using this attribute, you should not get this error/warning again.
codec(skip): Needs to be placed above a field or variant and makes it to be skipped while encoding/decoding.
codec(compact): Needs to be placed above a field and makes the field use compact encoding. (The type needs to support compact encoding.)
codec(encoded_as = "OtherType"): Needs to be placed above a field and makes the field being encoded by using
codec(index = 0): Needs to be placed above an enum variant to make the variant use the given index when encoded. By default the index is determined by counting from
0beginning wth the first variant.