pub trait ReservationIdExt: Sealed + Sized {
// Required methods
fn reserve(amount: u64, duration: u32) -> Result<Self>;
fn unreserve(self) -> Result<u64>;
}Available on non-crate feature
ethexe only.Expand description
Reservation identifier extension.
The identifier is used to reserve and unreserve gas amount for program execution later.
§Examples
ⓘ
use gstd::{prelude::*, ReservationId};
static mut RESERVED: Option<ReservationId> = None;
#[unsafe(no_mangle)]
extern "C" fn init() {
let reservation_id = ReservationId::reserve(50_000_000, 7).expect("Unable to reserve");
unsafe { RESERVED = Some(reservation_id) };
}
#[unsafe(no_mangle)]
extern "C" fn handle() {
let reservation_id = unsafe { RESERVED.take().expect("Empty `RESERVED`") };
reservation_id.unreserve().expect("Unable to unreserve");
}Required Methods§
Sourcefn reserve(amount: u64, duration: u32) -> Result<Self>
fn reserve(amount: u64, duration: u32) -> Result<Self>
Reserve the amount of gas for further usage.
duration is the block count within which the reserve must be used.
This function returns ReservationId, which one can use for gas
unreserving.
§Examples
Reserve 50 million of gas for one block, send a reply, then unreserve gas back:
use gstd::{ReservationId, msg, prelude::*};
#[unsafe(no_mangle)]
extern "C" fn handle() {
let reservation_id = ReservationId::reserve(50_000_000, 1).expect("Unable to reserve");
msg::reply_bytes_from_reservation(reservation_id.clone(), b"PONG", 0)
.expect("Unable to reply");
let reservation_left = reservation_id.unreserve().expect("Unable to unreserve");
}Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.