How to fix: cannot infer an appropriate lifetime for automatic coercion

后端 未结 1 1857
面向向阳花
面向向阳花 2021-01-13 13:56

I managed to run into a lifetime issue again that I seem to be unable to resolve on my own.

The compiler tells me cannot infer an appropriate lifetime for automa

相关标签:
1条回答
  • 2021-01-13 14:05

    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>,
    }
    

    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>,
    }
    

    Here's the fix in a PR for you: https://github.com/BurntSushi/Floor/commit/127962b9afc2779c9103c28f37e52e8d292f9ff2

    0 讨论(0)
提交回复
热议问题