title: Rust Callback: Error: Lifetime 'static required
body: <p>I'm trying to write a generic function that takes a callback as a parameter. However, I always get the following error message:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0310]: the parameter type `C` may not live long enough
    --&gt; src/lib.rs:36:5
     |
  17 | pub fn helper&lt;'de, Q, S, C&gt;(mut state : State, callback : C) -&gt; Box&lt;HandlerFuture&gt;
     |                          - help: consider adding an explicit lifetime bound `C: 'static`...
  ...
  36 |     Box::new(f)
     |     ^^^^^^^^^^^
     |
  note: ...so that the type `futures::Then&lt;futures::stream::Concat2&lt;hyper::Body&gt;, futures::FutureResult&lt;(gotham::state::State, hyper::Response), (gotham::state::State, gotham::handler::HandlerError)&gt;, [closure@src/lib.rs:24:15: 34:10 callback:&amp;C, state:gotham::state::State]&gt;` will meet its required lifetime bounds
    --&gt; src/lib.rs:36:5
     |
  36 |     Box::new(f)
     |     ^^^^^^^^^^^
  </code></pre>
  
  <p>This is a minimal compilable example that produces the error message:</p>
  
  <pre><code>extern crate futures;
  extern crate gotham;
  extern crate hyper;
  extern crate mime;
  extern crate serde;
  extern crate serde_json;
  
  use futures::{future, Future, Stream};
  use gotham::handler::{HandlerFuture, IntoHandlerError};
  use gotham::http::response::create_response;
  use gotham::state::{FromState, State};
  use hyper::{Body, StatusCode};
  use mime::APPLICATION_JSON;
  use serde::{Deserialize, Serialize};
  use serde_json::{from_str, to_string};
  
  pub fn helper&lt;'de, Q, S, C&gt;(mut state : State, callback : C) -&gt; Box&lt;HandlerFuture&gt;
      where Q : Deserialize&lt;'de&gt;,
            S : Serialize,
            C : Fn(Q) -&gt; S
  {
      let f = Body::take_from(&amp;mut state)
          .concat2()
          .then(|full_body| match full_body {
              Ok(valid_body) =&gt; {
                  let body_content = String::from_utf8(valid_body.to_vec()).unwrap();
                  let body_json = from_str::&lt;Q&gt;(&amp;body_content).unwrap();
                  let resp_json = callback(body_json);
                  let resp_content = to_string(&amp;resp_json).unwrap().into_bytes();
                  let res = create_response(&amp;state, StatusCode::Ok, Some((resp_content, APPLICATION_JSON)));
                  future::ok((state, res))
              }
              Err(e) =&gt; return future::err((state, e.into_handler_error()))
          });
  
      Box::new(f)
  }
  </code></pre>
  
  <p>Adding <code>C : 'static</code> to the <code>where</code> clause as rustc suggests, I get the following error message:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0597]: `body_content` does not live long enough
    --&gt; src/lib.rs:28:48
     |
  28 |                 let body_json = from_str::&lt;Q&gt;(&amp;body_content).unwrap();
     |                                                ^^^^^^^^^^^^ borrowed value does not live long enough
  ...
  33 |             }
     |             - borrowed value only lives until here
     |
  note: borrowed value must be valid for the lifetime 'de as defined on the function body at 17:1...
    --&gt; src/lib.rs:17:1
     |
  17 | / pub fn helper&lt;'de, Q, S, C&gt;(mut state : State, callback : C) -&gt; Box&lt;HandlerFuture&gt;
  18 | |     where Q : Deserialize&lt;'de&gt;,
  19 | |           S : Serialize,
  20 | |           C : Fn(Q) -&gt; S,
  ...  |
  37 | |     Box::new(f)
  38 | | }
     | |_^
  </code></pre>
  
  <p>This is the minimal content of my <code>Cargo.toml</code>:</p>
  
  <pre><code>[package]
  name = "test"
  version = "0.1.0"
  
  [dependencies]
  futures = "0.1"
  gotham = "0.2"
  hyper = "0.11"
  mime = "0.3"
  serde = "1.0"
  serde_json = "1.0"
  </code></pre>