Search code examples
rustborrow-checkerhyperrust-tokio

Request message value must be valid for the static lifetime


I want to implement a blocking function that sends a POST request with a JSON body and returns the JSON object of the response:

extern crate tokio_core;
extern crate rustc_serialize;
extern crate hyper;
extern crate futures;

use std::str;
use rustc_serialize::json;
use rustc_serialize::{Decoder, Decodable};
use hyper::{Method, Uri};
use hyper::client::{Client, Request};
use self::tokio_core::reactor::Core;
use self::futures::{Future, Stream};

#[derive(Debug, Clone)]
pub struct FooBar {
    pub foo: String,
    pub bar: String
}

impl Decodable for FooBar {
    fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
        d.read_struct("root", 0, |d| {
            Ok(FooBar {
                foo: try!(d.read_struct_field("foo", 0, |d| Decodable::decode(d))),
                bar: try!(d.read_struct_field("bar", 1, |d| Decodable::decode(d)))
            })
        })
    }
}

fn send_request(url: Uri, obj: json::Object) -> Option<FooBar> {
    let mut core = Core::new().unwrap();
    let client = Client::new(&core.handle());
    let msg = json::encode(&obj).unwrap();

    let mut request = Request::new(Method::Post, url);
    request.set_body(msg.as_bytes());

    let mut response = client.request(request).wait().unwrap();
    assert_eq!(response.status(), hyper::Ok);

    let res_vec = response.body().concat2().wait().unwrap().to_vec();
    let res_str = str::from_utf8(&res_vec).unwrap();
    return match json::decode(&res_str) {
        Ok(res_obj) => Some(res_obj),
        Err(err) => {
            println!("{}", err);
            None
        }
    }
}

I get the error that msg does not live long enough:

error[E0597]: `msg` does not live long enough
  --> src/test.rs:37:22
   |
37 |     request.set_body(msg.as_bytes());
   |                      ^^^ does not live long enough
...
51 | }
   | - borrowed value only lives until here
   |
   = note: borrowed value must be valid for the static lifetime...

At this point I have two questions:

  1. What is the component that wants msg to be valid for the static lifetime? It is not clear to me from the message.
  2. How can I implement such a function without making msg valid for the static lifetime? In my case it is not a viable solution.

Dependencies:

rustc-serialize = "0.3"
futures = "0.1"
hyper = "0.11"
tokio-core = "0.1"

Solution

  • request.set_body() takes a parameter that needs to be convertible into hyper::Body (the default for B in hyper::client::Request<B>).

    If you take a look at the list of From (the "dual" trait for Into) implementations for hyper::Body you'll see impl From<&'static [u8]> for Body - this is where you're static lifetime requirement comes from (there is no impl<'a> From<&'a [u8]> for Body that would take any other reference to "bytes").

    But you'll also see impl From<String> for Body - so it should be fine to just pass msg (which should be a String as far as I can tell) instead of msg.as_bytes() to request.set_body(). It will take ownership of the string msg, so you can't use it yourself afterwards anymore.