1

I have an asynchronous responder impl, but something is wrong with the lifetime of the objects. The code:

#[rocket::async_trait]
impl<'r> Responder<'r, 'static> for LoginUser {
    async fn respond_to(self, _: &'r Request) -> response::Result<'static> {
        use LicensesStatus::*;

        let status = self.check_licenses().await;
        let json = serde_json::json!({"status": format!("{:?}", status)}).to_string();

        let response = Response::build()
            .sized_body(json.len(), Cursor::new(&json))
            .header(ContentType::JSON);

        match status {
            Valid => response.ok(),
            _ => response.status(Status::Conflict).ok()
        }
    }
}

Compilation error:

error[E0195]: lifetime parameters or bounds on method `respond_to` do not match the trait declaration
  --> src/views/login.rs:64:14
   |
64 |     async fn respond_to(self, _: &'r Request<'_>) -> response::Result<'static> {
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetimes do not match method in trait
  • Just curious, why are you only checking the license in the responder? Shouldn't this happen earlier either in the handler or more preferably in a request guard? – kmdreko Jan 30 '22 at 19:06

1 Answers1

1

The Responder trait and its respond_to method are not async, so your implementation will not match the trait. You'll need to rethink your object such that the LoginUser already knows the license status, or move this logic into the handler itself.

kmdreko
  • 42,554
  • 6
  • 57
  • 106