title: Can Clone be implemented for a trait object with finite lifetime (without using unsafe code)?
body: <h2>First things first: What am I trying to achieve?</h2>
  <p>I'm trying to use <code>Iterator::fold()</code> with an accumulator that itself is an <code>Iterator</code>, but in addition I need a clone of that iterator internally.</p>
  <pre><code>fn whatever&lt;T,U,V,W&gt;(some_iterator : T, other_cloneable_iterator_init : U) -&gt; impl Iterator&lt;Item=W&gt;
  where T: Iterator&lt;Item=V&gt;,
        U: Iterator&lt;Item=W&gt;+Clone
  {
      some_iterator.fold(other_cloneable_iterator_init,|other_cloneable_iterator, value| {
          let computed_value =
              some_function_that_consumes_an_iterator(other_cloneable_iterator.clone(), value);
          other_iterator.filter(|other_value| some_function(computed_value, other_value))
      }
  }
  </code></pre>
  <p>This of course does not work as written above, because the return type of the closure given to <code>fold</code> is not the same as the type of the initializer.</p>
  <h2>Why did this problem lead to this question?</h2>
  <p>They do however have in common that they both implement the <code>Iterator&lt;Item=W&gt;+Clone</code> traits. This almost screams &quot;erase the type by making it a trait object&quot;.</p>
  <p>If it were just the <code>Iterator&lt;Item=W&gt;</code> trait, I'd do just that. However, the <code>Clone</code> trait is not object-safe. Searching for &quot;clone boxed trait object&quot; online yields various discussions, which all either <a href="https://stackoverflow.com/questions/30353462/how-to-clone-a-struct-storing-a-boxed-trait-object">require <code>'static</code> lifetime</a> on the trait object (what <code>Iterators</code> usually do not have), or using unsafe code (which I would like to avoid) like the <a href="https://github.com/dtolnay/dyn-clone" rel="nofollow noreferrer">dyn-clone</a> crate.</p>
  <h2>The actual question:</h2>
  <p>So, if one wants to avoid using unsafe code, and does not have the luxury of getting <code>'static</code> lifetimes, how can one implement <code>Clone</code> for a boxed trait object?
  I'm talking about code like the following, which has lifetime issues and does not compile:</p>
  <pre><code>trait Cu32Tst&lt;'a&gt; : Iterator&lt;Item=u32&gt;+'a {
      fn box_clone(&amp;self) -&gt; Box&lt;dyn Cu32Tst+'a&gt;;
  }
  impl&lt;'a, T : Iterator&lt;Item=u32&gt;+Clone+'a&gt; Cu32Tst&lt;'a&gt; for T {
      fn box_clone(&amp;self) -&gt; Box&lt;dyn Cu32Tst+'a&gt; {
          Box::new(self.clone())
      }
  }
  impl&lt;'a&gt; Clone for Box&lt;dyn Cu32Tst&lt;'a&gt;&gt;{
      fn clone(&amp;self) -&gt; Self {
          self.box_clone()
      }
  }
  </code></pre>
  <p>The reason I want <code>Clone</code> on the <code>Box</code> itself is that in order to use it as accumulator type, I must be able to create a new <code>Box&lt;dyn IteratorTraitObject&gt;</code> out of the output of the <code>Iterator::filter()</code> method, which is only <code>Clone</code> if the iterator it's called on is <code>Clone</code> (I'd then have to implement <code>Iterator</code> for the <code>Box</code> as well then, but that could just forward to the contained value).</p>
  <p>Long story short: Can <code>Clone</code> be implemented for a trait object with finite lifetime, and if yes, how?</p>