We have a vector Vec<u8>
that we want to deserialize to a type T
. The minumum reproducing example we could come up with is
fn wrapper<'de, T>(vec: Vec<u8>) -> Result<T, serde_json::Error>
where
T: serde::Deserialize<'de>,
{
serde_json::from_slice::<'de, T>(&vec)
}
However, the compiler gives the following error:
error[E0597]: `vec` does not live long enough
--> src/wrapper.rs:128:38
|
124 | fn wrapper<'de, T>(vec: Vec<u8>) -> Result<T, serde_json::Error>
| --- lifetime `'de` defined here
...
128 | serde_json::from_slice::<'de, T>(&vec)
| ---------------------------------^^^^-
| | |
| | borrowed value does not live long enough
| argument requires that `vec` is borrowed for `'de`
129 | }
| - `vec` dropped here while still borrowed
Since we have full ownership of the vector and the type T
doesn't have a reference to any of the original data (e.g. it could be a number or a String
), I'm stumped as to what is causing this error.
I still get the same error with the following changes:
serde::Deserialize<'static>
serde::Deserialize<'de> + 'static
serde::Deserialize<'de> + 'de