title: How can I implement a "default iterator" for a trait?
body: <p>I am trying to implement a default iterator for structs implementing a trait. My trait is called <code>DataRow</code>, represents a row of table cells, and looks like this:</p>
  
  <pre><code>pub trait DataRow&lt;'a&gt; {
      // Gets a cell by index
      fn getCell(&amp;self, i: usize) -&gt; &amp;DataCell&lt;'a&gt;;
  
      // Gets the number of cells in the row
      fn getNumCells(&amp;self) -&gt; usize;
  }
  </code></pre>
  
  <p>The default iterator I want to provide should use those two methods to iterate over the row and return cell references. In Java this would boil down to an abstract class <code>DataRow</code> that implements <code>Iterable</code>. In Rust I tried first with <code>IntoIterator</code>:</p>
  
  <pre><code>impl&lt;'a, T&gt; IntoIterator for &amp;'a T
  where
      T: DataRow&lt;'a&gt;,
  {
      type Item = &amp;'a DataCell&lt;'a&gt;;
      type IntoIter = DataRowIterator&lt;'a, T&gt;;
  
      fn into_iter(self) -&gt; DataRowIterator&lt;'a, T&gt; {
          return DataRowIterator::new(self);
      }
  }
  </code></pre>
  
  <p>This does not work as anyone could implement their own iterator for their own implementation of the <code>DataRow</code> trait.</p>
  
  <p>My second try was adding an <code>iter</code> method to the trait which creates the iterator and returns it:</p>
  
  <pre><code>fn iter(&amp;self) -&gt; DataRowIterator&lt;'a, Self&gt; {
      return DataRowIterator::new(self);
  }
  </code></pre>
  
  <p>This does not work either, because the size of <code>Self</code> is not known at compile time. Since <code>DataRow</code> can contain an arbitrary number of cells, I also cannot mark it as <code>Sized</code> to get around that. </p>
  
  <p><a href="https://play.rust-lang.org/?gist=dc128a133ffb5d7471d47d130520936d&amp;version=stable" rel="nofollow noreferrer">My demo code including notes on the occurring errors</a></p>
  
  <p>How would someone implement such a "default iterator" for a custom trait?</p>