title: How can I implement Deref for a struct that holds an Rc<Refcell<Trait>>?
body: <p>My goal is to delegate method calls against my struct to a Trait's methods, where the Trait object is inside an <code>Rc</code> of <code>RefCell</code>.</p>
  
  <p>I tried to follow the advice from this question:
  <a href="https://stackoverflow.com/questions/52124007/how-can-i-obtain-an-a-reference-from-a-rcrefcella?rq=1">How can I obtain an &amp;A reference from a Rc&lt;RefCell&lt;A&gt;&gt;?</a></p>
  
  <p>I get a compile error.</p>
  
  <pre><code>use std::rc::Rc;
  use std::cell::RefCell;
  use std::fmt::*;
  use std::ops::Deref;
  
  pub struct ShyObject {
      pub association: Rc&lt;RefCell&lt;dyn Display&gt;&gt;
  }
  
  impl Deref for ShyObject {
      type Target = dyn Display;
      fn deref&lt;'a&gt;(&amp;'a self) -&gt; &amp;(dyn Display + 'static) {
          &amp;*self.association.borrow()
      }
  }
  
  fn main() {}
  </code></pre>
  
  <p>Here is the error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0515]: cannot return value referencing temporary value
    --&gt; src/main.rs:13:9
     |
  13 |         &amp;*self.association.borrow()
     |         ^^-------------------------
     |         | |
     |         | temporary value created here
     |         returns a value referencing data owned by the current function
  </code></pre>
  
  <p>My example uses <code>Display</code> as the trait; in reality I have a Trait with a dozen methods. I am trying to avoid the boilerplate of having to implement all those methods and just burrow down to the Trait object in each call.</p>