title: Why does calling next on an Iterator trait object give me an "out of memory" at runtime?
body: <p>I implemented a <code>BoxedIterator</code> in Rust that just boxes another <code>Iterator</code> as a trait object. The full implementation is on <a href="https://github.com/Apanatshka/cargo-benchcmp/blob/add287a33321940e9410dac0e481f3b0e7b9faab/src/iterator.rs" rel="nofollow">Github</a>. Why does Rust compile this code without complaint but fail with an "out of memory" message (OOM) when it first tries to call <code>next</code> on the <code>Iterator</code> trait object in the <code>Box</code>? </p>
  
  <p>As far as I can tell it doesn't allocate much memory before failing, so I'm inclined to think the OOM message is not correct. </p>
  
  <pre><code>//! BoxedIterator just wraps around a box of an iterator, it is an owned trait object.
  //! This allows it to be used inside other data-structures, such as a `Result`.
  //! That means that you can `.collect()` on an `I where I: Iterator&lt;Result&lt;V, E&gt;&gt;` and get out a
  //! `Result&lt;BoxedIterator&lt;V&gt;, E&gt;`. And then you can `try!` it. At least, that was my use-case.
  
  use std::iter::FromIterator;
  use std::iter::IntoIterator;
  
  pub struct BoxedIterator&lt;T&gt; {
      iter: Box&lt;Iterator&lt;Item = T&gt;&gt;,
  }
  
  impl&lt;T&gt; Iterator for BoxedIterator&lt;T&gt; {
      type Item = T;
  
      #[inline]
      fn next(&amp;mut self) -&gt; Option&lt;Self::Item&gt; {
          self.iter.next() // The OOM comes from this call of `next`
      }
  }
  
  impl&lt;T&gt; FromIterator&lt;T&gt; for BoxedIterator&lt;T&gt; {
      fn from_iter&lt;I&gt;(iter: I) -&gt; Self
          where I: IntoIterator&lt;Item = T&gt;,
                I::IntoIter: 'static
      {
          BoxedIterator { iter: Box::new(iter.into_iter()) }
      }
  }
  
  use std::fs::File;
  use std::io;
  
  fn main() {
      let iter: Result&lt;BoxedIterator&lt;File&gt;, io::Error&gt; =
          vec!["/usr/bin/vi"].iter().cloned().map(File::open).collect();
      let mut iter = iter.unwrap();
  
      println!("{:?}", iter.next());
  }
  </code></pre>
  
  <p>I don't think I'm going to use this code, as I've figured that my use case will need to traverse the <code>Iterator</code> of <code>Result</code>s completely to extract any errors so I might as well gather them in a <code>Vec</code> at that point. But I'm still curious about this OOM. </p>
  
  <p>While creating a minimal example, I found that without doing the File IO, I get a segfault:</p>
  
  <pre><code>use iterator::BoxedIterator;
  
  fn main() {
      let iter: Result&lt;BoxedIterator&lt;&amp;str&gt;, ()&gt; = 
          vec![Ok("test1"), Ok("test2")].iter().cloned().collect();
      let mut iter = iter.unwrap();
  
      println!("{:?}", iter.next());
  }
  </code></pre>
  
  <p>If I don't use any <code>Result</code>, just create a <code>BoxedIterator</code> with <code>collect</code>, the code works as expected:</p>
  
  <pre><code>use iterator::BoxedIterator;
  
  fn main() {
      let mut iter: BoxedIterator&lt;&amp;str&gt; = vec!["test1", "test2"].iter().cloned().collect();
  
      println!("{:?}", iter.next());
      // prints: Some("test1")
  }
  </code></pre>