rust - actix-web:处理使用生命周期有限的异步/ future 响应

标签 rust lifetime

使用这个最小的异步actix web服务器

use actix_web::{http, server, App, HttpRequest, HttpResponse,  Error}; // v. 0.7.19
use futures::Future; // v. 0.1.28

/// An Asynchronous response which will either "fail fast" on the outer `Result` or
/// return a future which may itself succeed or fail
/// The lifetime parameter is required to indicate that the future cannot outlive
/// the parameter  - the `req: &HttpRequest` - of the handler (`handle_request`)
type AsyncCResponse<'a> =
Result<Box<Future<Item = HttpResponse, Error = Error> + 'a>, Error>;

/// lifetimes have been elided but here the future in the `AsyncCResponse`
/// will have the same lifetime as the `req` HttpRequest
fn handle_request(req: &HttpRequest<()>) -> AsyncCResponse {
    // handle the request
    Ok(Box::new(futures::future::ok(HttpResponse::Ok().body("Hello World"))))
}

fn main() {
    // instantiation of an actix-web server
    server::new(move || {
        App::new()
            .resource("/", |r| {
                r.method(http::Method::GET).f(|r: &HttpRequest<()>| handle_request(r))
            })
    });
}


编译失败

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
  --> src/main.rs:23:69
   |
23 |                 r.method(http::Method::GET).f(|r: &HttpRequest<()>| handle_request(r))
   |                                                                     ^^^^^^^^^^^^^^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #2 defined on the body at 23:47...
  --> src/main.rs:23:47
   |
23 |                 r.method(http::Method::GET).f(|r: &HttpRequest<()>| handle_request(r))
   |                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...so that reference does not outlive borrowed content
  --> src/main.rs:23:84
   |
23 |                 r.method(http::Method::GET).f(|r: &HttpRequest<()>| handle_request(r))
   |                                                                                    ^
   = note: but, the lifetime must be valid for the static lifetime...
   = note: ...so that the types are compatible:
           expected actix_web::handler::Responder
              found actix_web::handler::Responder

actix web中f的定义是
/// Set handler function. Usually call to this method is last call
/// during route configuration, so it does not return reference to self.
pub fn f<F, R>(&mut self, handler: F)
where
    F: Fn(&HttpRequest<S>) -> R + 'static,
    R: Responder + 'static,
{
    self.handler = InnerHandler::new(handler);
}

我是否正确理解编译失败是因为
f的定义要求处理程序返回一个生存期为F的响应
当给定处理程序返回的响应具有绑定到'static参数的生存期时?
在不改变handle_request定义的情况下,我能做些什么来解决这个问题吗?

最佳答案

没有直接的解决办法。

关于rust - actix-web:处理使用生命周期有限的异步/ future 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56986561/

相关文章:

generics - 无法创建简洁和通用的错误类型

rust - 为什么删除返回给我一个错误 : expected type `()` but found type

Rust - 结构成员的生命周期取决于另一个结构成员

iterator - 如何将 Result<(A, B), E> 序列解压缩到 (Vec<A>, Vec<B>) 并在出现第一个错误时停止?

debugging - 有没有办法在gdb或lldb中直接运行Cargo构建的程序?

rust - 在字符串切片数组中查找字符串切片

rust - 如何统一结构和特征之间的生命周期?

generics - 要求泛型类型实现复制或克隆|来自泛型固定数组的泛型向量

python - Python 类属性的生命周期

rust - 如何在Rust中为返回的元组设置静态生存期?