Trait actix::dev::MessageResponse[][src]

pub trait MessageResponse<A: Actor, M: Message> {
    fn handle(self, ctx: &mut A::Context, tx: Option<OneshotSender<M::Result>>);
}
Expand description

A trait which defines message responses.

We offer implementation for some common language types, if you need to respond with a new type you can use MessageResult.

If Actor::Context implements AsyncContext it’s possible to handle the message asynchronously. For asynchronous message handling we offer the following possible response types:

  • ResponseFuture should 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 to it and it won’t need Actor state to continue.
  • ResponseActFuture should be used when the future returned will, at some point, need to access Actor’s internal state or context in order to finish.
  • AtomicResponse should be used when the future returned needs exclusive access to Actor’s internal state or context.

Required methods

Implementations on Foreign Types

Implementors

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