I am new to Rust and trying to learn. I am using Rocket to create an API endpoint that passes in some key/value pairs. I've defined my structs like this:
#[derive(Deserialize)]
#[serde(crate = "rocket::serde")]
#[derive(Debug)]
struct PostDocument<'r> {
fields: Vec<FieldValues<'r>>
}
#[derive(Deserialize)]
#[serde(crate = "rocket::serde")]
#[derive(Debug)]
struct FieldValues<'r> {
name: &'r str,
value: &'r str,
}
and am using them in an API endpoint like this:
#[post("/<index_name>", format="json", data="<message>")]
async fn new_document(message: Json<PostDocument<'_>>, index_name: &str) -> Json<AddDocumentResponse> {
// code
}
The code won't compile, though complaining about the lifetime de the serde deserializer apparently creates, and that
de must outlive `r. Here is the full error message:
error: lifetime may not live long enough
--> src/add_file.rs:18:5
|
14 | #[derive(Deserialize)]
| ----------- lifetime `'de` defined here
...
17 | struct PostDocument<'r> {
| -- lifetime `'r` defined here
18 | fields: Vec<FieldValues<'r>>
| ^^^^^^ requires that `'de` must outlive `'r`
|
= help: consider adding the following bound: `'de: 'r`
A: I'm not sure where or how to specify the bound: 'de: 'r
(I'm new to rust, and that syntax isn't familiar, and I haven't been able to find a reference to it in the docs)
B: I have other endpoints that I've written in other files that define very similar structs that seem to have no issues. The code itself simple loops over the Vec and adds each one to a tantivy doc. If I comment out all the code in the method body that uses that parameter at all, the error still persists.
C: if I add a lifetime parameter called 'de, the compiler complains that it cannot deserialize if there is a lifetime parameter called 'de.