我又遇到了一个生命周期问题,似乎无法自行解决。
编译器告诉我无法推断自动强制转换的适当生命周期
我试图按照编译器的建议,在handle_request
方法中引入生命周期注释。
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
,而不仅仅是一个简单的&
引用。
这是我自己的ResponseWriter
结构体。
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分支中:https://github.com/cburgdorf/Floor/commits/lifetime_craziness
只需运行make floor
即可编译。