Struct gstd::msg::CreateProgramFuture
source · pub struct CreateProgramFuture {
pub waiting_reply_to: MessageId,
pub program_id: ActorId,
/* private fields */
}
Expand description
Async functions that relate to creating programs wait for a reply from the
program’s init function. These functions have the suffix _for_reply
, such
as crate::prog::create_program_bytes_for_reply
.
To get the reply payload (in bytes), one should use .await
syntax. After
calling a corresponding async function, the program interrupts its execution
until a reply arrives.
This future keeps the sent message identifier (MessageId
) to wake the
program after a reply arrives. Also, it keeps an identifier of a newly
created program (ActorId
).
§Examples
The following example explicitly annotates variable types for demonstration purposes only. Usually, annotating them is unnecessary because they can be inferred automatically.
use gstd::{msg::CreateProgramFuture, prog, ActorId};
# use gstd::CodeId;
#[gstd::async_main]
async fn main() {
# let code_id = CodeId::new([0; 32]);
let future: CreateProgramFuture =
prog::create_program_bytes_for_reply(code_id, b"salt", b"PING", 0, 0)
.expect("Unable to create a program");
let (prog_id, reply): (ActorId, Vec<u8>) = future.await.expect("Unable to get a reply");
}
# fn main() {}
Fields§
§waiting_reply_to: MessageId
A message identifier for an expected reply.
program_id: ActorId
An identifier of a newly created program.
Implementations§
source§impl CreateProgramFuture
impl CreateProgramFuture
sourcepub fn up_to(self, duration: Option<u32>) -> Result<Self>
pub fn up_to(self, duration: Option<u32>) -> Result<Self>
Postpone handling for a maximum amount of blocks that could be paid, that doesn’t exceed a given duration.
sourcepub fn exactly(self, duration: Option<u32>) -> Result<Self>
pub fn exactly(self, duration: Option<u32>) -> Result<Self>
Postpone handling for a given specific amount of blocks.
sourcepub fn handle_reply<F: FnOnce() + 'static>(self, f: F) -> Result<Self>
pub fn handle_reply<F: FnOnce() + 'static>(self, f: F) -> Result<Self>
Execute a function when the reply is received.
This callback will be executed in reply context and consume reply gas, so
adequate reply_deposit
should be supplied in *_for_reply
call
that comes before this. Note that the hook will still be executed on reply
even after original future resolves in timeout.
§Examples
Send message to echo program and wait for reply.
use gstd::{ActorId, msg, debug};
#[gstd::async_main]
async fn main() {
let dest = ActorId::from(1); // Replace with correct actor id
msg::send_bytes_for_reply(dest, b"PING", 0, 1_000_000)
.expect("Unable to send")
.handle_reply(|| {
debug!("reply code: {:?}", msg::reply_code());
if msg::load_bytes().unwrap_or_default() == b"PONG" {
debug!("successfully received pong");
}
})
.expect("Error setting reply hook")
.await
.expect("Received error");
}
§Panics
Panics if this is called second time.
Trait Implementations§
source§impl FusedFuture for CreateProgramFuture
impl FusedFuture for CreateProgramFuture
source§fn is_terminated(&self) -> bool
fn is_terminated(&self) -> bool
true
if the underlying future should no longer be polled.source§impl Future for CreateProgramFuture
impl Future for CreateProgramFuture
Auto Trait Implementations§
impl Freeze for CreateProgramFuture
impl RefUnwindSafe for CreateProgramFuture
impl Send for CreateProgramFuture
impl Sync for CreateProgramFuture
impl Unpin for CreateProgramFuture
impl UnwindSafe for CreateProgramFuture
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn map<U, F>(self, f: F) -> Map<Self, F> ⓘ
fn map<U, F>(self, f: F) -> Map<Self, F> ⓘ
§fn map_into<U>(self) -> MapInto<Self, U> ⓘ
fn map_into<U>(self) -> MapInto<Self, U> ⓘ
§fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F> ⓘ
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F> ⓘ
f
. Read more§fn left_future<B>(self) -> Either<Self, B> ⓘ
fn left_future<B>(self) -> Either<Self, B> ⓘ
§fn right_future<A>(self) -> Either<A, Self> ⓘ
fn right_future<A>(self) -> Either<A, Self> ⓘ
§fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
§fn flatten(self) -> Flatten<Self> ⓘ
fn flatten(self) -> Flatten<Self> ⓘ
§fn flatten_stream(self) -> FlattenStream<Self>
fn flatten_stream(self) -> FlattenStream<Self>
§fn fuse(self) -> Fuse<Self> ⓘwhere
Self: Sized,
fn fuse(self) -> Fuse<Self> ⓘwhere
Self: Sized,
poll
will never again be called once it has
completed. This method can be used to turn any Future
into a
FusedFuture
. Read more§fn inspect<F>(self, f: F) -> Inspect<Self, F> ⓘ
fn inspect<F>(self, f: F) -> Inspect<Self, F> ⓘ
§fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
§fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
§fn unit_error(self) -> UnitError<Self> ⓘwhere
Self: Sized,
fn unit_error(self) -> UnitError<Self> ⓘwhere
Self: Sized,
Future<Output = T>
into a
TryFuture<Ok = T, Error = ()
>.§fn never_error(self) -> NeverError<Self> ⓘwhere
Self: Sized,
fn never_error(self) -> NeverError<Self> ⓘwhere
Self: Sized,
Future<Output = T>
into a
TryFuture<Ok = T, Error = Never
>.source§impl<F> IntoFuture for Fwhere
F: Future,
impl<F> IntoFuture for Fwhere
F: Future,
§type IntoFuture = F
type IntoFuture = F
source§fn into_future(self) -> <F as IntoFuture>::IntoFuture
fn into_future(self) -> <F as IntoFuture>::IntoFuture
§impl<F, T, E> TryFuture for F
impl<F, T, E> TryFuture for F
§impl<Fut> TryFutureExt for Futwhere
Fut: TryFuture + ?Sized,
impl<Fut> TryFutureExt for Futwhere
Fut: TryFuture + ?Sized,
§fn flatten_sink<Item>(self) -> FlattenSink<Self, Self::Ok>where
Self::Ok: Sink<Item, Error = Self::Error>,
Self: Sized,
fn flatten_sink<Item>(self) -> FlattenSink<Self, Self::Ok>where
Self::Ok: Sink<Item, Error = Self::Error>,
Self: Sized,
Sink
]. Read more