Skip to main content

Ask

Struct Ask 

Source
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,

Source

pub fn new( promise: KPromise<Response>, content: Request, ) -> Ask<Request, Response>

Produce a new Ask instance from a promise and a Request.

Source

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.

Source

pub fn request(&self) -> &Request

The request associated with this Ask.

Source

pub fn request_mut(&mut self) -> &mut Request

The request associated with this Ask (mutable).

Source

pub fn take(self) -> (KPromise<Response>, Request)

Decompose this Ask into a pair of a promise and a Request.

Source

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.

Source

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.

Source

pub async fn complete_with<F>( self, f: impl FnOnce(Request) -> F, ) -> Result<(), PromiseErr>
where F: Future<Output = Response> + Send + 'static,

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
        })
    }
}

Trait Implementations§

Source§

impl<Request, Response> Debug for Ask<Request, Response>
where Request: Debug + MessageBounds, Response: Debug + Send,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<Request, Response> Freeze for Ask<Request, Response>
where Request: Freeze,

§

impl<Request, Response> !RefUnwindSafe for Ask<Request, Response>

§

impl<Request, Response> Send for Ask<Request, Response>

§

impl<Request, Response> Sync for Ask<Request, Response>
where Request: Sync,

§

impl<Request, Response> Unpin for Ask<Request, Response>
where Request: Unpin,

§

impl<Request, Response> UnsafeUnpin for Ask<Request, Response>
where Request: UnsafeUnpin,

§

impl<Request, Response> !UnwindSafe for Ask<Request, Response>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> DispatchEvent for T
where T: Any + Send + Debug,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any + Send>

Convert this event into an erased Any payload for downcasting.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> Erased for T

Source§

impl<M> MessageBounds for M
where M: Debug + Send + 'static,