rust - How to fix: cannot infer an appropriate lifetime for automatic coercion -
rust - How to fix: cannot infer an appropriate lifetime for automatic coercion -
i managed run lifetime issue 1 time again seem unable resolve on own.
the compiler tells me cannot infer appropriate lifetime automatic coercion
i tried follow compilers suggestion , introduced lifetime annotations in handle_request
method.
fn handle_request<'a>(&self, req: &request, res: &'a mut responsewriter) { fn set_headers(req: &request, res: &mut responsewriter) { //probably not of import illustration } match &req.request_uri { &absolutepath(ref url) => { match self.router.match_route(req.method.clone(), url.clone()) { some(route_result) => { set_headers(req, res); allow floor_req = request::request{ origin: req, params: route_result.params.clone() }; allow floor_res = response::response{ origin: res }; (route_result.route.handler)(floor_req, &mut floor_res); }, none => {} } }, _ => set_headers(req, res) } }
i had code working before wanted wrap http::server::responsewriter
in own response
struct. did same request
before in terms of lifetimes case seems different. maybe because it's &mut
instead of simple &
reference.
this responsewriter
struct.
use http; ///a container response pub struct response<'a> { ///the original `http::server::responsewriter` pub origin: &'a mut http::server::responsewriter<'a>, }
just incase samaritan wants compile code locally, pushed lifetime_crazyness branch here: https://github.com/cburgdorf/floor/commits/lifetime_craziness
just run make floor
compile it.
ok, pulled downwards code seek , compile it. indeed, if take @ definition of response
struct, see this:
pub struct response<'a> { ///the original `http::server::responsewriter` pub origin: &'a mut http::server::responsewriter<'a>, }
from compiler's perspective, definition claims lifetime of pointer http::server::responsewriter
the same whatever lifetime within of http::server::responsewriter
. don't see particular reason why should true, , looks compiler can't either. prepare it, need introduce lifetime parameter allow possibility lifetimes distinct:
pub struct response<'a, 'b> { ///the original `http::server::responsewriter` pub origin: &'a mut http::server::responsewriter<'b>, }
here's prepare in pr you: https://github.com/burntsushi/floor/commit/127962b9afc2779c9103c28f37e52e8d292f9ff2
rust lifetime
Comments
Post a Comment