0

I am struggling to learn Rust with his lifetime. So I am making the method bellow in a web client wrapper.

pub async fn get_as_bytes<T: serde::Deserialize>(
    &self,
    url: &str,
    headers: Option<HeaderMap>,
) -> Result<T, Box<dyn Error>> {
    let mut req = self.reqwest_client.request(reqwest::Method::GET, url);

    if let Some(hds) = headers {
        req = req.headers(hds);
    }

    let resp: Response = req.send().await?;
    let full = resp.bytes().await?;

    let result = serde_json::from_slice(&full).unwrap();
    Ok(result)
}

When I try to compile I get that:

error[E0106]: missing lifetime specifier
  --> src/webclient.rs:53:41
   |
53 |     pub async fn get_as_bytes<T: serde::Deserialize>(&self, url: &str, headers: Option<HeaderMap>) -> Result<T, Box<dyn Error>> {
   |                                         ^^^^^^^^^^^ expected named lifetime parameter
   |
help: consider introducing a named lifetime parameter
   |
53 |     pub async fn get_as_bytes<'a, T: serde::Deserialize<'a>>(&self, url: &str, headers: Option<HeaderMap>) -> Result<T, Box<dyn Error>> {
   |                               ^^^           ^^^^^^^^^^^^^^^

error[E0597]: `full` does not live long enough
  --> src/webclient.rs:63:45
   |
63 |         let result = serde_json::from_slice(&full).unwrap();
   |                      -----------------------^^^^^-
   |                      |                      |
   |                      |                      borrowed value does not live long enough
   |                      argument requires that `full` is borrowed for `'static`
64 |         Ok(result)
65 |     }
   |     - `full` dropped here while still borrowed

How do I make &full last since I am trying to return it?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • It's hard to answer your question because it doesn't include a [MRE]. We can't tell what crates (and their versions), types, traits, fields, etc. are present in the code. It would make it easier for us to help you if you try to reproduce your error on the [Rust Playground](https://play.rust-lang.org) if possible, otherwise in a brand new Cargo project, then [edit] your question to include the additional info. There are [Rust-specific MRE tips](//stackoverflow.com/tags/rust/info) you can use to reduce your original code for posting here. Thanks! – Shepmaster Nov 25 '20 at 19:16
  • It looks like your question might be answered by the answers of [Lifetime error when creating a function that returns a value implementing serde::Deserialize](https://stackoverflow.com/q/43554679/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Nov 25 '20 at 19:17
  • @Shepmaster you nailed it. Thank you so much – Lucas Bataglião Nov 25 '20 at 19:20

1 Answers1

1

Just posting as a future reference and not deleting the question. As @Shepmaster mention in the comment of the question, que Lifetime error when creating a function that returns a value implementing serde::Deserialize resolves the matter. As as documentation, the code would be:

pub async fn get_as_bytes<T: serde::de::DeserializeOwned >(&self, url: &str, headers: Option<HeaderMap>) -> Result<T, Box<dyn Error>> {
        let mut req = self.reqwest_client.request(reqwest::Method::GET, url);

        if let Some(hds) = headers {
            req = req.headers(hds);
        }

        let resp: Response = req.send().await?;
        let full = resp.bytes().await?;

        let result = serde_json::from_slice(&full).unwrap();
        Ok(result)
    }
    ```