Data Encoding/Decoding
To optimize how data is sent and received over the network, Gear uses the parity-scale-codec - a Rust implementation of the SCALE Codec. This codec is used by the Substrate nodes' internal runtime. SCALE is a lightweight format that enables the serialization and deserialization of data. Encoding (and decoding) data using SCALE makes it highly suitable for resource-constrained execution environments like blockchain runtimes and low-power/low-memory devices.
To use the SCALE codec in a program, add it to Cargo.toml:
[dependencies]
// ...
codec = { package = "parity-scale-codec", version = "3.6", default-features = false }use codec::{Decode, Encode};
#[derive(Encode, Decode)]
enum MyType {
MyStruct { field: ... },
...
}Encoding and Decoding Traits
The Encode and Decode traits are required when using standard gstd methods:
msg::load, msg::send, msg::reply, and msg::send_for_reply.
Note on Byte Methods:
When using byte-level operations like msg::load_bytes, msg::send_bytes or msg::reply_bytes, encoding/decoding is not required as you are working directly with raw data.
Learn more about the SCALE Codec here.
scale-info
scale-info is a library to describe Rust types, intended for providing information about the structure of encodable SCALE types.
The definitions provide third-party tools (e.g., a UI client) with information about how to decode types agnostic of language. The interface that uses scale-info for Gear programs is called metadata. It defines incoming and outgoing types for all necessary entry points and allows programs and the client part to understand each other.
For a deeper dive into program configuration, see the Metadata Overview guide.
To use scale-info in a project:
[dependencies]
// ...
scale-info = { version = "2.9", default-features = false, features = ["derive"] }Learn more about scale-info here.