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
// This file is part of Gear.
// Copyright (C) 2021-2024 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Standard library for use in Gear programs.
//!
//! This library should be used as a standard library when writing Gear
//! programs. Compared to [`gcore`](https://docs.gear.rs/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.
//!
//! # Minimum supported Rust version
//! This crate requires **Rust >= 1.81** due to the implementation of the panic
//! handler in the stable version.
//!
//! # Crate features
#![cfg_attr(
feature = "document-features",
cfg_attr(doc, doc = ::document_features::document_features!())
)]
//! # Examples
//!
//! Decode input payload using a custom type:
//!
//! ```
//! # const _: &'static str = stringify! {
//! #![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`.
//!
//! ```ignored
//! # const _: &'static str = stringify! {
//! #![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");
//! }
//! # fn main() {}
//! ```
#![no_std]
#![warn(missing_docs)]
#![cfg_attr(
all(target_arch = "wasm32", feature = "oom-handler"),
feature(alloc_error_handler)
)]
#![doc(html_logo_url = "https://docs.gear.rs/logo.svg")]
#![doc(html_favicon_url = "https://gear-tech.io/favicons/favicon.ico")]
#![doc(test(attr(deny(warnings), allow(unused_variables, unused_assignments))))]
#![allow(ambiguous_glob_reexports)]
extern crate alloc;
#[cfg(target_arch = "wasm32")]
extern crate galloc;
mod async_runtime;
mod common;
mod config;
#[cfg(not(feature = "ethexe"))]
pub mod critical;
pub mod exec;
mod macros;
pub mod msg;
pub mod prelude;
pub mod prog;
#[cfg(not(feature = "ethexe"))]
mod reservations;
pub mod sync;
pub mod util;
pub use async_runtime::{handle_reply_with_hook, message_loop};
pub use common::errors;
pub use config::{Config, SYSTEM_RESERVE};
pub use gcore::{
debug, ext, ActorId, BlockCount, BlockNumber, CodeId, EnvVars, Gas, GasMultiplier, MessageId,
Percent, Ss58Address, Value,
};
pub use gstd_codegen::{actor_id, async_init, async_main};
pub use prelude::*;
#[cfg(not(feature = "ethexe"))]
pub use {
async_runtime::handle_signal, common::primitives_ext::*, gcore::ReservationId, reservations::*,
};
// This allows all casts from u32 into usize be safe.
const _: () = assert!(size_of::<u32>() <= size_of::<usize>());