Type Alias actix::ResponseFuture

source ·
pub type ResponseFuture<I> = Pin<Box<dyn Future<Output = I>>>;
Expand description

A specialized future for asynchronous message handling.

Intended be used for when the future returned doesn’t need to access Actor’s internal state or context to progress, either because it’s completely agnostic to it, or because the required data has already been moved inside the future and it won’t need Actor state to continue.

§Examples

use actix::prelude::*;

#[derive(Message)]
#[rtype(result = "Result<(), ()>")]
struct Msg;

struct MyActor;

impl Actor for MyActor {
    type Context = Context<Self>;
}

impl Handler<Msg> for MyActor {
    type Result = ResponseFuture<Result<(), ()>>;

    fn handle(&mut self, _: Msg, _: &mut Context<Self>) -> Self::Result {
        Box::pin(async move {
            // Some async computation
            Ok(())
        })
    }
}

Aliased Type§

struct ResponseFuture<I> { /* private fields */ }

Trait Implementations§

source§

impl<A, M> MessageResponse<A, M> for ResponseFuture<M::Result>
where A: Actor, M: Message,

MessageResponse trait impl to enable the use of any I: 'static with asynchronous message handling

§Examples

Usage with Result<I,E>:

pub struct MyQuestion{}
impl Message for MyQuestion {
    type Result = Result<u8,u8>;
}
impl Handler<MyQuestion> for MyActorAsync {
    type Result = ResponseFuture<Result<u8,u8>>;
    fn handle(&mut self, question: MyQuestion, _ctx: &mut Context<Self>) -> Self::Result {
        Box::pin(async {Ok(5)})
    }
}

Usage with Option<I>:

pub struct MyQuestion{}
impl Message for MyQuestion {
    type Result = Option<u8>;
}
impl Handler<MyQuestion> for MyActorAsync {
    type Result = ResponseFuture<Option<u8>>;
    fn handle(&mut self, question: MyQuestion, _ctx: &mut Context<Self>) -> Self::Result {
        Box::pin(async {Some(5)})
    }
}

Usage with any I: 'static:

pub struct MyQuestion{}
impl Message for MyQuestion {
    type Result = u8;
}
impl Handler<MyQuestion> for MyActorAsync {
    type Result = ResponseFuture<u8>;
    fn handle(&mut self, question: MyQuestion, _ctx: &mut Context<Self>) -> Self::Result {
        Box::pin(async {5})
    }
}
source§

fn handle(self, _: &mut A::Context, tx: Option<OneshotSender<M::Result>>)