title: How can I skip a number of bytes from a data source that implements the Read trait without a buffer?
body: <p>I'm writing a streaming binary encoding parser, pulling bytes from a data source that implements the <a href="https://doc.rust-lang.org/std/io/trait.Read.html" rel="nofollow noreferrer"><code>Read</code></a> trait. Values in the stream are prefixed with a short header that indicates what type the next value is and how many bytes it takes to represent it.</p>
  
  <p>I've been using <a href="https://doc.rust-lang.org/std/io/trait.Read.html#method.bytes" rel="nofollow noreferrer"><code>Read::bytes</code></a> to read the header. Then, if I'm interested in the value that the header represents, I can use the <a href="https://doc.rust-lang.org/std/io/trait.Read.html#tymethod.read" rel="nofollow noreferrer"><code>Read::read</code></a> method to copy the required number of bytes from the data source into a buffer. However, if I'm <em>not</em> interested in the value, I'd like to skip over those bytes without having to process them. I can achieve this by doing:</p>
  
  <pre><code>for byte_result in data_source.bytes().take(n) {
      let _byte = byte_result?;
  }
  </code></pre>
  
  <p>However, checking on the result of each individual byte feels very expensive. Is there a way I can achieve this that only requires that I check the result of an overall <code>skip</code> operation?</p>
  
  <p>The <a href="https://doc.rust-lang.org/std/io/trait.Seek.html" rel="nofollow noreferrer"><code>Seek</code></a> trait is promising, but many forms of input do not implement it (<a href="https://doc.rust-lang.org/std/net/struct.TcpStream.html" rel="nofollow noreferrer"><code>TcpStream</code></a>, for example.)</p>