pub struct Ask<Request, Response>where
Request: MessageBounds,
Response: Send,{ /* private fields */ }Expand description
A message type for request-response messages.
Used together with the ask function.
Implementations§
Source§impl<Request, Response> Ask<Request, Response>where
Request: MessageBounds,
Response: Send,
impl<Request, Response> Ask<Request, Response>where
Request: MessageBounds,
Response: Send,
Sourcepub fn new(
promise: KPromise<Response>,
content: Request,
) -> Ask<Request, Response>
pub fn new( promise: KPromise<Response>, content: Request, ) -> Ask<Request, Response>
Produce a new Ask instance from a promise and a Request.
Sourcepub fn of(
content: Request,
) -> impl FnOnce(KPromise<Response>) -> Ask<Request, Response>
pub fn of( content: Request, ) -> impl FnOnce(KPromise<Response>) -> Ask<Request, Response>
Produce a function that takes a promise and returns an ask with the given Request.
Use this avoid the explicit chaining of the promise that the new function requires.
Sourcepub fn request_mut(&mut self) -> &mut Request
pub fn request_mut(&mut self) -> &mut Request
The request associated with this Ask (mutable).
Sourcepub fn take(self) -> (KPromise<Response>, Request)
pub fn take(self) -> (KPromise<Response>, Request)
Decompose this Ask into a pair of a promise and a Request.
Sourcepub fn reply(self, response: Response) -> Result<(), PromiseErr>
pub fn reply(self, response: Response) -> Result<(), PromiseErr>
Reply to this Ask with the response.
§Errors
Fails with a PromiseErr if the promise has already been fulfilled or the other end dropped the future.
Sourcepub fn complete(
self,
f: impl FnOnce(Request) -> Response,
) -> Result<(), PromiseErr>
pub fn complete( self, f: impl FnOnce(Request) -> Response, ) -> Result<(), PromiseErr>
Run f to produce a response to this Ask and reply with that reponse.
§Errors
Fails with a PromiseErr if the promise has already been fulfilled or the other end dropped the future.
Sourcepub async fn complete_with<F>(
self,
f: impl FnOnce(Request) -> F,
) -> Result<(), PromiseErr>
pub async fn complete_with<F>( self, f: impl FnOnce(Request) -> F, ) -> Result<(), PromiseErr>
Run the future produced by f to completion, and then reply with its result.
§Errors
Fails with a PromiseErr if the promise has already been fulfilled or the other end dropped the future.
§Note
As usual for async functions, you must await the future returned by this function somewhere, in order for the code to actually be executed.
You can do so, for example, by using block_on and returning the result from a message handling function:
impl Actor for ExampleComponent {
type Message = Ask<usize, usize>;
fn receive_local(&mut self, msg: Self::Message) -> HandlerResult {
Handled::block_on(self, async move |async_self| {
msg.complete_with(async move |num| {
num + 1 // produce response
})
.await
.expect("complete");
Handled::OK
})
}
}