title: Associated type lifetime when passing internal reference to a generic closure
body: <p>Here's an example that compiles fine:</p>
  
  <pre><code>use std::cell::RefCell;
  use std::rc::Rc;
  
  struct Foo&lt;'a&gt; {
      val: Rc&lt;RefCell&lt;i32&gt;&gt;,
      dummy: Option&lt;&amp;'a i32&gt;,
  }
  
  fn consume&lt;T&gt;(_: T) {}
  
  impl&lt;'a&gt; Foo&lt;'a&gt; {
      // Note that &amp;i32 has no lifetime markers
      fn subscribe&lt;F&gt;(self, func: F)
      where
          F: Fn(&amp;i32) + 'a,
      {
          let val = self.val.clone();
          consume(move |x: i32| {
              *val.borrow_mut() = x;
              func(&amp;*val.borrow())
          })
      }
  }
  </code></pre>
  
  <p>Here's what what I'm trying to achieve and doesn't compile:</p>
  
  <pre><code>use std::cell::RefCell;
  use std::rc::Rc;
  
  trait Stream&lt;'a&gt; {
      type Item: 'a;
  
      fn subscribe&lt;F&gt;(self, func: F)
      where
          F: Fn(Self::Item) + 'a;
  }
  
  struct Bar&lt;'a, S: Stream&lt;'a&gt;&gt; {
      stream: S,
      val: Rc&lt;RefCell&lt;S::Item&gt;&gt;,
  }
  
  impl&lt;'a, S: Stream&lt;'a&gt;&gt; Stream&lt;'a&gt; for Bar&lt;'a, S&gt; {
      type Item = &amp;'a S::Item; // 'a doesn't seem right here...
  
      fn subscribe&lt;F&gt;(self, func: F)
      where
          F: Fn(Self::Item) + 'a,
      {
          let val = self.val.clone();
          self.stream.subscribe(move |x: S::Item| {
              *val.borrow_mut() = x;
              func(&amp;*val.borrow());
          })
      }
  }
  </code></pre>
  
  <p>This example is almost identical to the first. The only difference is that because it's a trait, we have to assign an explicit lifetime to the associated type, <code>Item</code>, which is a reference. Setting it to <code>'a</code> causes lifetime conflicts (rightfully so):</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
    --&gt; src/lib.rs:27:24
     |
  27 |             func(&amp;*val.borrow());
     |                        ^^^^^^
     |
  note: first, the lifetime cannot outlive the lifetime  as defined on the body at 25:31...
    --&gt; src/lib.rs:25:31
     |
  25 |         self.stream.subscribe(move |x: S::Item| {
     |                               ^^^^^^^^^^^^^^^^^
  note: ...so that closure can access `val`
    --&gt; src/lib.rs:27:20
     |
  27 |             func(&amp;*val.borrow());
     |                    ^^^
  note: but, the lifetime must be valid for the lifetime 'a as defined on the impl at 17:6...
    --&gt; src/lib.rs:17:6
     |
  17 | impl&lt;'a, S: Stream&lt;'a&gt;&gt; Stream&lt;'a&gt; for Bar&lt;'a, S&gt; {
     |      ^^
  note: ...so that reference does not outlive borrowed content
    --&gt; src/lib.rs:27:18
     |
  27 |             func(&amp;*val.borrow());
     |                  ^^^^^^^^^^^^^^
  </code></pre>
  
  <p>In fact, the first example can be modified to fail with exactly the same error if you were to replace <code>Fn(&amp;i32)</code> with <code>Fn(&amp;'a i32)</code> in the function signature.</p>
  
  <p>Is it possible to make the second example compile? Possibly via the use of some hacks or unsafe blocks, I'd be willing to accept anything really. Changing signatures or reshuffling the logic if that's required. What should the lifetime of the associated type <code>Item</code> be?</p>