title: How can I create hashable trait objects / trait objects with generic method parameters?
body: <p>I have some structs that implement both <code>Hash</code> and <code>MyTrait</code>. I'm using them as <code>&amp;MyTrait</code> trait objects.</p>
  
  <p>Now I want <code>&amp;MyTrait</code> to also implement <code>Hash</code>. I've tried a few things:</p>
  
  <ul>
  <li><p>Naively, <code>trait MyTrait: Hash {}</code>:</p>
  
  <pre class="lang-none prettyprint-override"><code>the trait `MyTrait` cannot be made into an object
  </code></pre></li>
  <li><p>Then I tried this:</p>
  
  <pre><code>impl Hash for MyTrait {
      fn hash&lt;H: Hasher&gt;(&amp;self, hasher: &amp;mut H) {
          // ...
      }
  }
  </code></pre>
  
  <p>but I need to delegate to the <code>hash</code> method of the concrete type of <code>self</code>, I think.</p></li>
  <li><p>So the naive next step is to put this on <code>MyTrait</code>:</p>
  
  <pre><code>fn my_hash&lt;H: Hasher&gt;(&amp;self, hasher: &amp;mut H);
  </code></pre>
  
  <p>which brings me right back to the first point.</p></li>
  <li><p>I read something about using a trait object instead of generic parameter, which sounds smart, so I put this on <code>MyTrait</code></p>
  
  <pre><code>fn my_hash(&amp;self, hasher: &amp;mut H);
  </code></pre>
  
  <p>Then I need to actually implement this. Preferably not by hand for every trait:</p>
  
  <pre><code>impl&lt;T: 'static + Hash&gt; MyTrait for T {
      fn as_any(&amp;self) -&gt; &amp;Any {
          self as &amp;Any
      }
  
      fn my_hash(&amp;self, hasher: &amp;mut Hasher) {
          self.as_any().downcast_ref::&lt;T&gt;().unwrap().hash(hasher)
      }
  }
  </code></pre>
  
  <p>but then</p>
  
  <pre class="lang-none prettyprint-override"><code>the trait bound `std::hash::Hasher: std::marker::Sized` is not satisfied
  `std::hash::Hasher` does not have a constant size known at compile-time
  </code></pre>
  
  <p>So I'd have to downcast <code>Hasher</code>…</p></li>
  <li><p>If downcasting <code>Hasher</code> is the way, I need a generic parameter <code>H</code> that can convert to an <code>Any</code> <code>Hasher</code>, Let's try:</p>
  
  <pre><code>trait AnyHasher {
      fn as_any(&amp;self) -&gt; &amp;Any;
  }
  
  impl&lt;H: 'static + Hasher&gt; AnyHasher for H {
      fn as_any(&amp;self) -&gt; &amp;Any {
          self as &amp;Any
      }
  }
  </code></pre>
  
  <p>and then to downcast</p>
  
  <pre><code>impl&lt;T: 'static + Hash, H: 'static + Hasher&gt; MyTrait for T {
      // ...
      fn my_hash(&amp;self, hasher: &amp;mut AnyHasher) {
          let h = hasher.as_any().downcast_ref::&lt;H&gt;().unwrap();
          self.as_any().downcast_ref::&lt;T&gt;().unwrap().hash(h)
      }
  }
  </code></pre>
  
  <p>but alas</p>
  
  <pre class="lang-none prettyprint-override"><code>the type parameter `H` is not constrained by the impl trait, self type, or predicates
  </code></pre>
  
  <p>which I guess is true, but then I'm stuck. (Also it seems kind of ridiculous so far).</p></li>
  </ul>
  
  <p>Can this can be done? If so, how?</p>
  
  <p>I previously asked about <a href="https://stackoverflow.com/questions/49466199/default-trait-method-implementation-for-all-trait-objects"><code>PartialEq</code> for trait objects</a>, which was hard because information the concrete type of the trait object is needed. That was solved with downcasting, but I didn't manage to apply that solution here.</p>