I get an unexpected error from this Rust code:
struct Container<'a> {
x: &'a i32,
}
trait Reply {}
impl Reply for i32 {}
fn json<T>(_val: &T) -> impl Reply {
3
}
fn f() -> impl Reply {
let i = 123;
let a = Container { x: &i };
json(&a)
}
The error is:
error[E0597]: `i` does not live long enough
--> src/lib.rs:14:28
|
12 | fn f() -> impl Reply {
| ---------- opaque type requires that `i` is borrowed for `'static`
13 | let i = 123;
14 | let a = Container { x: &i };
| ^^ borrowed value does not live long enough
15 | json(&a)
16 | }
| - `i` dropped here while still borrowed
Why?
If I change the declaration of json()
to either of these versions, the code compiles:
fn json(val: &Container) -> impl Reply
fn json<T>(val: &T) -> i32
It is only when there is both a type parameter and a returned trait object that the compiler rejects the code.
This is a reduction from a real issue we had with warp::reply::json()
, but I would prefer to understand it in general.