如何解决:无法推断出自动强制的合适寿命 [英] How to fix: cannot infer an appropriate lifetime for automatic coercion
问题描述
我设法再次遇到一生的问题,我似乎无法自行解决.
I managed to run into a lifetime issue again that I seem to be unable to resolve on my own.
编译器告诉我无法推断出自动强制的适当生存期
我试图遵循编译器的建议,并在 handle_request
方法中引入了生命周期注释.
I tried to follow the compilers suggestion and introduced the lifetime annotations in the handle_request
method.
fn handle_request<'a>(&self, req: &Request, res: &'a mut ResponseWriter) {
fn set_headers(req: &Request, res: &mut ResponseWriter) {
//probably not important for the example
}
match &req.request_uri {
&AbsolutePath(ref url) => {
match self.router.match_route(req.method.clone(), url.clone()) {
Some(route_result) => {
set_headers(req, res);
let floor_req = request::Request{
origin: req,
params: route_result.params.clone()
};
let floor_res = response::Response{
origin: res
};
(route_result.route.handler)(floor_req, &mut floor_res);
},
None => {}
}
},
_ => set_headers(req, res)
}
}
我之前有代码工作,但现在我想将 http::server::ResponseWriter
包装在我自己的 Response
结构中.我之前对 Request
所做的操作完全相同,但是就生命周期而言,情况似乎略有不同.也许是因为它是& mut
而不是简单的&
参考.
I had the code working before but now I wanted to wrap the http::server::ResponseWriter
in my own Response
struct. I did exactly the same for the Request
before but in terms of lifetimes the case seems to be slightly different. Maybe because it's a &mut
instead of just a simple &
reference.
这是我的 ResponseWriter
结构.
This is my ResponseWriter
struct.
use http;
///A container for the response
pub struct Response<'a> {
///the original `http::server::ResponseWriter`
pub origin: &'a mut http::server::ResponseWriter<'a>,
}
只要万一撒玛利亚人想在本地编译代码,我就将其推送到 lifetime_crazyness 分支中:
Just incase any Samaritan wants to compile the code locally, I pushed it into the lifetime_crazyness branch here: https://github.com/cburgdorf/Floor/commits/lifetime_craziness
只需运行 make floor
进行编译即可.
Just run make floor
to compile it.
推荐答案
好,所以我删除了您的代码以尝试对其进行编译.确实,如果我们看一下您的 Response
结构的定义,我们会看到:
OK, so I pulled down your code to try and compile it. Indeed, if we take a look at the definition of your Response
struct, we see this:
pub struct Response<'a> {
///the original `http::server::ResponseWriter`
pub origin: &'a mut http::server::ResponseWriter<'a>,
}
从编译器的角度来看,该定义声称 http :: server :: ResponseWriter
的指针的生存期与内的生存期相同代码>http::server::ResponseWriter.我看不出有什么特别的理由为什么这应该是正确的,而且看起来编译器也不能.因此,要解决此问题,您需要引入另一个生命周期参数,以允许存在不同生命周期的可能性:
From the compiler's perspective, this definition claims that the lifetime of the pointer to a http::server::ResponseWriter
is the same as whatever lifetime is inside of http::server::ResponseWriter
. I don't see any particular reason why this should be true, and it looks like the compiler can't either. So to fix it, you need to introduce another lifetime parameter so that you allow for the possibility that the lifetimes are distinct:
pub struct Response<'a, 'b> {
///the original `http::server::ResponseWriter`
pub origin: &'a mut http::server::ResponseWriter<'b>,
}
这是为您提供的PR中的修复程序:/a>
Here's the fix in a PR for you: https://github.com/BurntSushi/Floor/commit/127962b9afc2779c9103c28f37e52e8d292f9ff2
这篇关于如何解决:无法推断出自动强制的合适寿命的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!