lmat - Reinstate Monica
lmat - Reinstate Monica

Reputation: 7788

Actix Message Returning Tuple of Two Things Including Vec

I have tried to make a minimum reproducing example; I don't think I can make it smaller:

/*
[dependencies]
actix-web = "4"
actix = "0.13.0"
actix-web-actors = "4.2.0"
actix-files = "0.6.2"
bytestring = "1.2.0"
*/

use actix::Actor;

struct Session;

#[derive(actix::Message)]
#[rtype(result="(usize, Vec<usize>)")]
struct AddClient;

impl actix::Actor for Session {
    type Context = actix::Context<Self>;
}

impl actix::Handler<AddClient> for Session {
    type Result = (usize, Vec<usize>);
    fn handle(&mut self, _msg: AddClient, _ctx: &mut Self::Context) -> (usize, Vec<usize>) {
        (0, vec!(1,2,3))
    }
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let app_state: actix::Addr<Session> = Session.start();
    let nums = match app_state.send(AddClient).await {
        Err(_)=>{println!("error"); return Ok(());},
        Ok(nums)=>nums,
    };
    println!("{:?}", nums);
    Ok(())
}

gives errors that I don't understand:

error[E0277]: the trait bound `(usize, Vec<usize>): MessageResponse<Session, AddClient>` is not satisfied
  --> src/main.rs:24:19
   |
23 |     type Result = (usize, Vec<usize>);
   |                   ^^^^^^^^^^^^^^^^^^^ the trait `MessageResponse<Session, AddClient>` is not implemented for `(usize, Vec<usize>)`
   |
   = help: the trait `MessageResponse<A, M>` is implemented for `()`
note: required by a bound in `actix::Handler::Result`
  --> /home/lawsa/.cargo/registry/src/github.com-1ecc6299db9ec823/actix-0.13.0/src/handler.rs:25:18
   |
24 |     type Result: MessageResponse<Self, M>;
   |                  ^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `actix::Handler::Result`

This doesn't happen if I change the return type to (usize, usize) nor if I change the return type to Vec<usize> (I don't know how to make a one-member tuple). How do I write a message that returns a usize and a Vec<usize>?

Upvotes: 0

Views: 143

Answers (1)

Chayim Friedman
Chayim Friedman

Reputation: 71555

You can use MessageResult:

impl actix::Handler<AddClient> for Session {
    type Result = actix::MessageResult<AddClient>;
    fn handle(&mut self, _msg: AddClient, _ctx: &mut Self::Context) -> Self::Result {
        actix::MessageResult((0, vec![]))
    }
}

Upvotes: 1

Related Questions