title: Clone Box with generics
body: <p>This question is a simpler example of my last question that is still unanswered because is too complex, so following the comments, I removed some generics.</p>
  <p><a href="https://stackoverflow.com/questions/73127086/clone-boxdyn-traita-a">Clone Box&lt;dyn Trait&lt;&#39;a,A&gt;</a></p>
  <ul>
  <li>ATrait is a generator that generates BTrait implementors.</li>
  <li>MultiA is a concatenator of generators.</li>
  <li>I need to clone generators.</li>
  </ul>
  <p>This is the code:</p>
  <pre><code>trait BTrait {}
  
  trait ATrait&lt;B: BTrait&gt;: CloneATrait {
      fn do_A(&amp;mut self) -&gt; Vec&lt;B&gt;;
  }
  
  trait CloneATrait {
      fn clone_box&lt;B: BTrait&gt;(&amp;self) -&gt; Box&lt;dyn ATrait&lt;B&gt;&gt;;
  }
  
  impl&lt;B: BTrait, T&gt; CloneATrait for T
  where
      T: 'static + ATrait&lt;B&gt; + Clone,
  {
      fn clone_box(&amp;self) -&gt; Box&lt;dyn ATrait&lt;B&gt;&gt; {
          Box::new(self.clone())
      }
  }
  
  impl&lt;B: BTrait&gt; Clone for Box&lt;dyn ATrait&lt;B&gt;&gt; {
      fn clone(&amp;self) -&gt; Box&lt;dyn ATrait&lt;B&gt;&gt; {
          self.clone_box()
      }
  }
  
  #[derive(Clone)]
  struct MultiA&lt;B: BTrait&gt; {
      comps: Vec&lt;Box&lt;dyn ATrait&lt;B&gt;&gt;&gt;,
  }
  
  impl&lt;B: BTrait&gt; MultiA&lt;B&gt; {
      fn new(comps: Vec&lt;Box&lt;dyn ATrait&lt;B&gt;&gt;&gt;) -&gt; Self {
          Self { comps }
      }
  }
  
  impl&lt;B: BTrait&gt; ATrait&lt;B&gt; for MultiA&lt;B&gt; {
      fn do_A(&amp;mut self) -&gt; Vec&lt;B&gt; {
          let mut res: Vec&lt;B&gt; = Vec::new();
          for a in &amp;self.comps {
              res.append(a.do_A());
          }
          res
      }
  }
  
  fn main() {}
  </code></pre>
  <p>That gives me the following compiler error:</p>
  <pre><code> Compiling playground v0.0.1 (/playground)
  error[E0207]: the type parameter `B` is not constrained by the impl trait, self type, or predicates
    --&gt; src/main.rs:13:6
     |
  13 | impl&lt;B : BTrait, T&gt; CloneATrait for T
     |      ^ unconstrained type parameter
  
  For more information about this error, try `rustc --explain E0207`.
  error: could not compile `playground` due to previous error
  
  </code></pre>
  <p>It works fine for a simple generator:</p>
  <pre><code>trait BTrait {
    
  }
  
  trait ATrait&lt;B : BTrait&gt; {
      fn do_A(&amp;mut self) -&gt; Vec&lt;B&gt;;
  }
  
  
  impl BTrait for usize{
      
  }
  
  impl BTrait for f64{
      
  }
  
  #[derive(Clone)]
  struct SingleA {
  }
  
  
  impl ATrait&lt;usize&gt; for SingleA {
      fn do_A(&amp;mut self)  -&gt; Vec&lt;usize&gt;{
          vec![1,2,3]
      }
  }
  
  impl ATrait&lt;f64&gt; for SingleA {
      fn do_A(&amp;mut self)  -&gt; Vec&lt;f64&gt;{
          vec![1.0,2.0,3.0]
      }
  }
  
  
  fn main() {
      let mut t = SingleA{};
      &lt;SingleA as ATrait&lt;usize&gt;&gt;::do_A(&amp;mut t);
      &lt;SingleA as ATrait&lt;f64&gt;&gt;::do_A(&amp;mut t);
  }
  </code></pre>
  <p>So my problem is cloning <code>Vec&lt;Box&lt;dyn ATrait&lt;B&gt;&gt;&gt;</code></p>