title: Implementing Nested Traits
body: <p>I have some traits which (after removing functions and some parameter bloat) look like:</p>
  
  <pre><code>trait Foo { }
  trait Boo { }
  trait Bar&lt;T: Foo&gt; { }
  trait Baz { }
  </code></pre>
  
  <p>If <code>U</code> implements <code>Bar&lt;T&gt;</code> for some <code>T</code> implementing <code>Foo</code> <em>and</em> <code>U</code> implements <code>Boo</code>, then one is able to derive an implementation of <code>Baz</code> for <code>U</code>. However, I wasn't able to write valid Rust code doing this.</p>
  
  <p>A few tries were:</p>
  
  <pre><code>impl&lt;T: Foo, U: Bar&lt;T&gt; + Boo&gt; Baz for U { }
  </code></pre>
  
  <p>which gives</p>
  
  <blockquote>
    <p>error: the type parameter <code>T</code> is not constrained by the impl trait, self type, or predicates [E0207]</p>
  </blockquote>
  
  <p>whereas</p>
  
  <pre><code>impl&lt;U: Bar&lt;T&gt; + Boo&gt; Baz for U { }
  </code></pre>
  
  <p>yields</p>
  
  <blockquote>
    <p>error: type name <code>T</code> is undefined or not in scope [E0412]</p>
  </blockquote>
  
  <p>Could one/how could one do this in (stable) Rust (hopefully without any dynamic dispatch)?</p>
  
  <p><strong>Edit</strong>: Some people hinted at some similar questions for which there were essentially two approaches (and I find both of them unsuitable for my situation):</p>
  
  <ol>
  <li>Using associated types. I don't want to do this because I want to keep track of <code>T</code>, e.g. I want to write some functions which have a signature like <code>fn bla&lt;T: Foo, U: Bar&lt;T&gt;, V: Bar&lt;T&gt;&gt;()</code> where I want to know that <code>U</code> and <code>V</code> implement <code>Bar&lt;T&gt;</code> for the <em>same</em> <code>T</code>. (Or is there way of doing this with associated types?)</li>
  <li>Using some kind of wrapping by putting <code>U</code> and <code>T</code> in a struct. I don't want to use this either because I have several levels of such "trait dependencies", so wrapping things in each level would bloat the code a lot.</li>
  </ol>
  
  <p>So the updated question would be: <em>Is there a solution to this problem without using associated types or wrappers?</em></p>