Struct gstd::msg::MessageFuture
source · pub struct MessageFuture {
pub waiting_reply_to: MessageId,
/* private fields */
}
Expand description
Future returned by async functions related to message sending that wait for
a reply (see sending functions with _for_reply
suffix, e.g.
send_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.
§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::{self, MessageFuture};
# use gstd::ActorId;
#[gstd::async_main]
async fn main() {
# let dest = ActorId::zero();
let future: MessageFuture =
msg::send_bytes_for_reply(dest, b"PING", 0, 0).expect("Unable to send");
let reply: Vec<u8> = future.await.expect("Unable to get a reply");
}
# fn main() {}
Fields§
§waiting_reply_to: MessageId
A message identifier for an expected reply.
This identifier is generated by the corresponding send function (e.g.
send_bytes
).
Implementations§
source§impl MessageFuture
impl MessageFuture
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 MessageFuture
impl FusedFuture for MessageFuture
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 MessageFuture
impl Future for MessageFuture
Auto Trait Implementations§
impl Freeze for MessageFuture
impl RefUnwindSafe for MessageFuture
impl Send for MessageFuture
impl Sync for MessageFuture
impl Unpin for MessageFuture
impl UnwindSafe for MessageFuture
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