Expand description
Standard library for use in Gear programs.
This library should be used as a standard library when writing Gear
programs. Compared to gcore crate,
this library provides higher-level primitives that allow you to develop more
complex dApps. Choose this library if you are ready to spend more gas but
receive refined code.
gstd crate provides many advanced tools for a developer, such as
asynchronous programming primitives, arbitrary types encoding/decoding,
providing convenient instruments for creating programs from programs, etc.
Crate features
Default features
panic-handler(enabled by default) — When enabled, a panic handler is provided by this crate.
Nightly features
nightly— Enables all features below. These features depend on unstable Rust API and require nightly toolchain.panic-messages— When enabled, additional context information is available from panic messages in debug mode. Relies onpanic_info_message.oom-handler— When enabled, an OOM error handler is provided. Relies onalloc_error_handler,
Additional features
debug— Enables debug logging; this heavily impacts gas cost and is therefore disabled by default.
Examples
Decode input payload using a custom type:
#![no_std]
use gstd::{msg, prelude::*};
#[derive(Decode, Encode, TypeInfo)]
#[codec(crate = gstd::codec)]
#[scale_info(crate = gstd::scale_info)]
struct Payload {
question: String,
answer: u8,
}
#[no_mangle]
extern "C" fn handle() {
let payload: Payload = msg::load().expect("Unable to decode payload");
if payload.question == "life-universe-everything" {
msg::reply(payload.answer, 0).expect("Unable to reply");
}
}Asynchronous program example.
It sends empty messages to three addresses and waits for at least two
replies (“approvals”) during initialization. When invoked, it handles only
PING messages and sends empty messages to the three addresses, and waits
for just one approval. If approval is obtained, the program replies with
PONG.
#![no_std]
use futures::future;
use gstd::{msg, prelude::*, ActorId};
static mut APPROVERS: [ActorId; 3] = [ActorId::zero(); 3];
#[derive(Debug, Decode, TypeInfo)]
#[codec(crate = gstd::codec)]
#[scale_info(crate = gstd::scale_info)]
pub struct Input {
pub approvers: [ActorId; 3],
}
#[gstd::async_init]
async fn init() {
let payload: Input = msg::load().expect("Failed to decode input");
unsafe { APPROVERS = payload.approvers };
let mut requests: Vec<_> = unsafe { APPROVERS }
.iter()
.map(|addr| msg::send_bytes_for_reply(*addr, b"", 0, 0))
.collect::<Result<_, _>>()
.unwrap();
let mut threshold = 0;
while !requests.is_empty() {
let (.., remaining) = future::select_all(requests).await;
threshold += 1;
if threshold >= 2 {
break;
}
requests = remaining;
}
}
#[gstd::async_main]
async fn main() {
let message = msg::load_bytes().expect("Failed to load payload bytes");
if message != b"PING" {
return;
}
let requests: Vec<_> = unsafe { APPROVERS }
.iter()
.map(|addr| msg::send_bytes_for_reply(*addr, b"", 0, 0))
.collect::<Result<_, _>>()
.unwrap();
_ = future::select_all(requests).await;
msg::reply(b"PONG", 0).expect("Unable to reply");
}Re-exports
pub use prelude::*;
Modules
- Type definitions and helpers for error handling.
- Utility functions related to the current execution context or program execution flow.
- Extensions for additional features.
- Messaging API for Gear programs.
- The
gstddefault prelude. Re-imports defaultstdmodules and traits.stdcan be safely replaced togstdin the Rust programs. - Functions and helpers for creating programs from programs.
- Data access synchronization objects.
- Utility functions.
Macros
- Unwrap
Result<T, E>toTif it isOk(T)or panic with the provided message if the result isErr(E). - Prints and returns the value of a given expression for quick and dirty debugging.
- Add a debug message to the log.
Structs
- Program (actor) identifier.
- Code identifier.
- The set of broadly used internal parameters.
- Type representing converter between gas and value.
- Message identifier.
- Basic struct for working with integer percentages allowing values greater than 100.
- Stores additional data along with
ReservationIdto track its state. - Reservation identifier.
- Reservation manager.
Functions
- Default signal handler.
- The main asynchronous message handling loop.
- Default reply handler.
Type Aliases
- Represents block count type.
- Represents block number type.
- Represents gas type.
- Represents value type.
Attribute Macros
- Mark async function to be the program initialization method.
- Mark the main async function to be the program entry point.