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: MessageIdA message identifier for an expected reply.
Implementations§
Source§impl<D> CodecMessageFuture<D>where
D: Decode,
impl<D> CodecMessageFuture<D>where
D: Decode,
Sourcepub fn up_to(
self,
duration: Option<u32>,
) -> Result<CodecMessageFuture<D>, Error>
pub fn up_to( self, duration: Option<u32>, ) -> Result<CodecMessageFuture<D>, Error>
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<CodecMessageFuture<D>, Error>
pub fn exactly( self, duration: Option<u32>, ) -> Result<CodecMessageFuture<D>, Error>
Postpone handling for a given specific amount of blocks.
Sourcepub fn handle_reply<F>(self, f: F) -> Result<CodecMessageFuture<D>, Error>where
F: FnOnce() + 'static,
pub fn handle_reply<F>(self, f: F) -> Result<CodecMessageFuture<D>, Error>where
F: FnOnce() + 'static,
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> FusedFuture for CodecMessageFuture<D>where
D: Decode,
impl<D> FusedFuture for CodecMessageFuture<D>where
D: Decode,
Source§fn is_terminated(&self) -> bool
fn is_terminated(&self) -> bool
true if the underlying future should no longer be polled.Source§impl<D> Future for CodecMessageFuture<D>where
D: Decode,
impl<D> Future for CodecMessageFuture<D>where
D: Decode,
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
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn map<U, F>(self, f: F) -> Map<Self, F> ⓘ
fn map<U, F>(self, f: F) -> Map<Self, F> ⓘ
Source§fn map_into<U>(self) -> MapInto<Self, U> ⓘ
fn map_into<U>(self) -> MapInto<Self, U> ⓘ
Source§fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F> ⓘ
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F> ⓘ
f. Read moreSource§fn left_future<B>(self) -> Either<Self, B> ⓘ
fn left_future<B>(self) -> Either<Self, B> ⓘ
Source§fn right_future<A>(self) -> Either<A, Self> ⓘ
fn right_future<A>(self) -> Either<A, Self> ⓘ
Source§fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
Source§fn flatten(self) -> Flatten<Self> ⓘ
fn flatten(self) -> Flatten<Self> ⓘ
Source§fn flatten_stream(self) -> FlattenStream<Self>
fn flatten_stream(self) -> FlattenStream<Self>
Source§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 moreSource§fn inspect<F>(self, f: F) -> Inspect<Self, F> ⓘ
fn inspect<F>(self, f: F) -> Inspect<Self, F> ⓘ
Source§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>>
Source§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,
Source§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 = ()>.Source§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>.