title: How can I get impl Trait to use the appropriate lifetime for a mutable reference to a value with another lifetime in it?
body: <p>I have a struct with a lifetime:</p>
  
  <pre><code>struct HasLifetime&lt;'a&gt;( /* ... */ );
  </code></pre>
  
  <p>There is there is an implementation of the trait <code>Foo</code>:</p>
  
  <pre><code>impl&lt;'a, 'b: 'a&gt; Foo for &amp;'a mut HasLifetime&lt;'b&gt; { }
  </code></pre>
  
  <p>I want to implement the following function:</p>
  
  <pre><code>fn bar_to_foo&lt;'a, 'b: 'a&gt;(bar: &amp;'a mut Lifetime&lt;'b&gt;) -&gt; impl Foo {
      bar
  }
  </code></pre>
  
  <p>This won't compile because the returned <code>impl</code> is only valid for <code>'a</code>. However, specifying <code>impl Foo + 'a</code> results in:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0909]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
   --&gt; src/main.rs:7:60
    |
  7 | fn bar_to_foo&lt;'a, 'b: 'a&gt;(bar: &amp;'a mut HasLifetime&lt;'b&gt;) -&gt; impl Trait + 'a {
    |                                                            ^^^^^^^^^^^^^^^
    |
  note: hidden type `&amp;'a mut HasLifetime&lt;'b&gt;` captures the lifetime 'b as defined on the function body at 7:1
   --&gt; src/main.rs:7:1
    |
  7 | fn bar_to_foo&lt;'a, 'b: 'a&gt;(bar: &amp;'a mut HasLifetime&lt;'b&gt;) -&gt; impl Trait + 'a {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  </code></pre>
  
  <p>The seemingly equivalent function with a boxed trait object compiles:</p>
  
  <pre><code>fn bar_to_foo&lt;'a, 'b: 'a&gt;(bar: &amp;'a mut Lifetime&lt;'b&gt;) -&gt; Box&lt;Foo + 'a&gt; {
      Box::new(bar)
  }
  </code></pre>
  
  <p>How can I define <code>bar_to_foo</code> with <code>impl Trait</code>?</p>
  
  <p><a href="https://play.rust-lang.org/?gist=b8f293acecf4b7e1f3c5963f15c36b1e&amp;version=stable&amp;mode=debug" rel="noreferrer">Playground link</a></p>