title: Lifetime error using traits and async function on protobuffers
body: <p>I'm having some issues understanding lifetimes in Rust. It may also be the way I implement my design.</p>
  <pre class="lang-none prettyprint-override"><code>error[E0597]: `request` does not live long enough
    --&gt; src/service/session/server.rs:25:23
     |
  25 |         let msg_ref = request.get_ref();
     |                       ^^^^^^^ borrowed value does not live long enough
  ...
  32 |         let body: Box&lt;dyn Body&gt; = Box::new(signup);
     |                                   ---------------- cast requires that `request` is borrowed for `'static`
  ...
  44 |     }
     |     - `request` dropped here while still borrowed
  </code></pre>
  <p>The main source:</p>
  <pre class="lang-rust prettyprint-override"><code>#[tonic::async_trait]
  impl Session for SessionImplementation {
      async fn signup(
          &amp;self,
          request: Request&lt;SignupRequest&gt;,
      ) -&gt; Result&lt;Response&lt;SessionResponse&gt;, Status&gt; {
          let msg_ref = request.get_ref();
          let signup = TxSignup::new(&amp;msg_ref.name, &amp;msg_ref.addr, &amp;msg_ref.pwd);
  
          let body: Box&lt;dyn Body&gt; = Box::new(signup);
          let tx = Transaction::new(body);
          let mut tx_signup: Box&lt;dyn Tx&gt; = Box::new(tx);
          tx_signup.execute();
  
          let response = SessionResponse {
              deadline: 0,
              cookie: &quot;&quot;.to_string(),
              status: 0,
          };
  
          Ok(Response::new(response))
      }
  
      /* more code */
  }
  </code></pre>
  <h2>Background</h2>
  <p>The idea is to have a <code>Transaction</code>, that implements <code>Tx { execute(&amp;self), result(&amp;self) ... };</code>. This <code>Transaction</code> has a parameter <code>body</code> of the type <code>Box&lt;dyn Box&gt;</code>, being the trait <code>Body { /*some fn*/ }</code>. Having this, I'm pretending to implement some kind of hierarchy.</p>
  <h2>The code above</h2>
  <p>On line 24 I'm getting some requests of the type <code>SignupRequest</code> (from proto file). This is the implementation of proto's server, using Tonic and Tokio.
  After this, I have also an object <code>TxSignup</code> with some parameters of the type <code>&amp;str</code> (set in from line 27 till 29). <code>TxSignup</code> implements the <code>Body</code> trait so I'm able to turn it into a <code>Tx</code> trait, apparently. The <code>Transaction</code> object wraps a <code>Body</code> implementation. I call the <code>execute()</code> function from the given trait <code>Tx</code>. All that explained has been done from line 32 till 35.</p>
  <h2>The problem</h2>
  <p>If I replace the <code>&amp;str</code> type from <code>TxSignup</code> by type <code>String</code> it works. However, if I want them to be of the type <code>&amp;str</code>, a lot of &quot;incongruencies&quot; with lifetimes emerge. I want it to be <code>&amp;str</code> because none of these values will change. I think it is better to keep them on the stack instead of in the heap. Am I wrong?</p>
  <p>If I want <code>&amp;str</code>, I'm coerced to define <code>TxSignup</code> with <code>&lt;'a&gt;</code>, and here is where I get lost. I get why a lifetime is required, but not why all these problems appear.</p>
  <p>As far I do understand, all elements inside the function should have the same lifetime, being killed at the end of its block (line 44). I will never send them outside.</p>
  <p>I have tried giving to <code>Body</code> trait also an <code>&lt;'a&gt;</code>, and even to <code>Tx</code> trait (meaning the <code>Transaction</code> object must have one too to match the trait).</p>
  <p>Is there any way to make it work? Am I misunderstanding the Trait use and how they work, or this patter design will never work?</p>
  <h2>Reproduction on GitHub</h2>
  <p>I have reproduced this same error in my <a href="https://github.com/HectorMRC/rust-proto.git" rel="nofollow noreferrer">rust-proto</a> repository. Running <code>cargo run</code> should be enough.</p>
  <hr />
  <p>I come from Go development and some C++, Java and Python, so I have a way of coding that may not be the most appropriate one using Rust. That's what I want to solve.</p>