0

我有一个异步响应器 impl,但是对象的生命周期有问题。编码:

#[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()
        }
    }
}

编译错误:

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
4

1 回答 1

0

Responder特征及其respond_to方法不是 ,因此async您的实现将与特征不匹配。您需要重新考虑您的对象,以便LoginUser已经知道许可证状态,或者将此逻辑移动到处理程序本身。

于 2022-01-30T19:04:12.273 回答