0

Is there a way to write this code with #[derive(Message)] and #[rtype(result = "...")].

pub struct MyMsg<T> {
    data: T
}

impl<T: 'static> Message for MyMsg<T> {
    type Result = Result<MyMsg<T>, Error>;
}

I tried this but the compiler complains about the required lifetime bounds.

#[derive(Message)]
#[rtype(result = "Result<MyMsg<T>, Error>")]
pub struct MyMsg<T> {
    pub data: T,
}
mehdy
  • 3,174
  • 4
  • 23
  • 44
  • *"To be more clear"* - didn't make it any clearer to me ;) What are the errors you are facing? Please show us your attempt and what errors it gives. It's hard to guess your mistake without seeing it. – Finomnis Aug 03 '22 at 15:32

1 Answers1

1

I assume you use anyhow, because otherwise Result<..., Error> wouldn't make much sense. (as Error is a trait, not a type)

Then, I don't understand your problem, it just works:

use actix_derive::Message;
use anyhow::Error;

#[derive(Message)]
#[rtype(result = "Result<MyMsg<T>, Error>")]
pub struct MyMsg<T: 'static> {
    pub data: T,
}

If you expand that with cargo expand, you can see that you get:

#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
use actix_derive::Message;
use anyhow::Error;
#[rtype(result = "Result<MyMsg<T>, Error>")]
pub struct MyMsg<T: 'static> {
    pub data: T,
}
impl<T: 'static> ::actix::Message for MyMsg<T> {
    type Result = Result<MyMsg<T>, Error>;
}
Finomnis
  • 18,094
  • 1
  • 20
  • 27
  • Thanks. it seems like if I define the lifetime specifier when I'm defining the `MyMsg` struct , It's gonna work. – mehdy Aug 03 '22 at 16:15