Struct gstd::msg::CodecMessageFuture
source · pub struct CodecMessageFuture<T> {
pub waiting_reply_to: MessageId,
/* private fields */
}
Expand description
Same as MessageFuture
, but allows decoding the reply’s payload instead
of getting a byte vector.
Generic T
type should implement the Decode
trait.
§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, CodecMessageFuture},
prelude::*,
};
# use gstd::ActorId;
#[derive(Decode)]
#[codec(crate = gstd::codec)]
struct Reply {
field: String,
}
#[gstd::async_main]
async fn main() {
# let dest = ActorId::zero();
let future: CodecMessageFuture<Reply> =
msg::send_bytes_for_reply_as(dest, b"PING", 0, 0).expect("Unable to send");
let reply: Reply = future.await.expect("Unable to get a reply");
let field: String = reply.field;
}
# fn main() {}
Fields§
§waiting_reply_to: MessageId
A message identifier for an expected reply.
Implementations§
source§impl<D: Decode> CodecMessageFuture<D>
impl<D: Decode> CodecMessageFuture<D>
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<D: Decode> FusedFuture for CodecMessageFuture<D>
impl<D: Decode> FusedFuture for CodecMessageFuture<D>
source§fn is_terminated(&self) -> bool
fn is_terminated(&self) -> bool
true
if the underlying future should no longer be polled.source§impl<D: Decode> Future for CodecMessageFuture<D>
impl<D: Decode> Future for CodecMessageFuture<D>
Auto Trait Implementations§
impl<T> Freeze for CodecMessageFuture<T>
impl<T> RefUnwindSafe for CodecMessageFuture<T>where
T: RefUnwindSafe,
impl<T> Send for CodecMessageFuture<T>where
T: Send,
impl<T> Sync for CodecMessageFuture<T>where
T: Sync,
impl<T> Unpin for CodecMessageFuture<T>where
T: Unpin,
impl<T> UnwindSafe for CodecMessageFuture<T>where
T: UnwindSafe,
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