title: How do you implement deref on a generic type containing a trait in rust?
body: <p>It would be rather convenient to be able to use Deref to generate a &amp;TraitType from a generic container, rather than calling instance.as_ref(). ie:</p>
  
  <pre><code>(*my_container).do_thing();
  </code></pre>
  
  <p>vs.</p>
  
  <pre><code>my_container.as_ref().do_thing();
  </code></pre>
  
  <p>To do this I tried to implement Deref on the container type, but I get this error:</p>
  
  <pre><code>&lt;anon&gt;:9:28: 9:29 error: expected a reference to a trait [E0172]
  &lt;anon&gt;:9 impl&lt;T&gt; Deref for HasTrait&lt;T + Send&gt; {
  </code></pre>
  
  <p>From:</p>
  
  <pre class="lang-rust prettyprint-override"><code>use std::ops::Deref;
  
  trait Foo {}
  
  struct HasTrait&lt;T&gt; {
    data:Box&lt;T&gt;
  }
  
  impl&lt;T&gt; Deref for HasTrait&lt;T + Send&gt; {
    type Target = T;
    fn deref&lt;'a&gt;(&amp;'a self) -&gt; &amp;'a T {
      return self.as_ref();
    }
  }
  
  struct IsFoo;
  unsafe impl Send for IsFoo {}
  impl Foo for IsFoo {}
  
  
  fn main() {
    let is_foo = IsFoo;
    let foo:Box&lt;Foo&gt; = box is_foo as Box&lt;Foo&gt;;
    let has_foo = HasTrait { data: foo };
    let foo_ref:&amp;Foo = *has_foo; 
  }
  </code></pre>
  
  <p>I've tried using ?Sized to increase the bounds of T to allow traits, but it didn't seem to help?</p>
  
  <p>What's the right way to do this?</p>