title: Is it possible to auto implement a trait which casts a trait object to another trait object?
body: <p>I have a trait which manages conversions to different trait objects.
  The trait looks like this: (<code>Boo</code> and <code>Gee</code> are both different Traits)</p>
  
  <pre><code>trait Foo {
      fn as_boo(&amp;mut self) -&gt; Option&lt;&amp;mut Boo&gt; {
          None
      }
  
      fn as_gee(&amp;mut self) -&gt; Option&lt;&amp;mut Gee&gt; {
          None
      }
  }
  </code></pre>
  
  <p>To reduce the amount of boilerplate code I now want to automatically change this implementation to this in case the struct implements <code>Boo</code>/<code>Gee</code>:</p>
  
  <pre><code>#[derive(Boo)]
  struct W {}
  
  impl Foo for W {
      fn as_boo(&amp;mut self) -&gt; Option&lt;&amp;mut Boo&gt; {
          Some(self)
      }
  }
  </code></pre>
  
  <p>I could do this if there is only one other trait I have to convert to by using generics:</p>
  
  <pre><code>impl&lt;T: Boo&gt; Foo for T {
      fn as_boo(&amp;mut self) -&gt; Option&lt;&amp;mut Boo&gt; {
          Some(self)
      }
  }
  </code></pre>
  
  <p>But in case I also want to automatically implement <code>Foo</code> for <code>Gee</code> it doesn't work this way due to the fact that I could not implement <code>Foo</code> twice, which is not supported by rust as far as I know.</p>
  
  <pre><code>// This does not compile because Foo might get implemented twice.
  impl&lt;T: Boo&gt; Foo for T {
      fn as_boo(&amp;mut self) -&gt; Option&lt;&amp;mut Boo&gt; {
          Some(self)
      }
  }
  
  impl&lt;T: Gee&gt; Foo for T {
      fn as_gee(&amp;mut self) -&gt; Option&lt;&amp;mut Gee&gt; {
          Some(self)
      }
  }
  </code></pre>
  
  <p>It might be possible to achieve this using <a href="https://doc.rust-lang.org/beta/reference/procedural-macros.html" rel="nofollow noreferrer"><code>Procedural Macros</code></a> but I could not find any in depth explanation of them so I am kind of stuck now.</p>