title: Directly reading from a file to BufReader's or Cursor's underlying buffer
body: <p>I'm trying to figure out some basic stuff in Rust.</p>
  
  <p>I would like to create a tool that will read 512 bytes from a file, and copy those bytes to another file. Then take next 8 bytes from input file and skip them. Then take next 512 bytes from input file, and copy them to output file, then skip 8 bytes, etc...</p>
  
  <p>I need this tool to be fast, so I can't just perform an I/O call every 512 bytes. I've figured I would need to read a few megabytes of the input file first, then remove unneeded 8-byte blocks in memory by selectively copying it to another memory block, and then call I/O write to dump the bigger memory block at once.</p>
  
  <p>So, I would like to do something like this (pseudo-code):</p>
  
  <pre><code>let buffer = buffer of 'u8' of size 4MB;
  let buffer_out = buffer of 'u8' of size 4MB;
  
  // both buffers above take 8MB of memory
  
  let input_stream = InputStream(buffer);
  let output_stream = OutputStream(buffer_out);
  
  for(every 4MB block in the input file) {
      input.read(buffer); // read the 4MB block into 'buffer'
      input_stream.seek(0); // reset the input stream's cursor to offset 0
  
      for(every 520 byte inside the 4MB block in 'buffer') {
          output_stream.write(input_stream.read(512)); // copy important 512 bytes
          input_stream.read(8);                        // skip superfluous 8 bytes
      }
  
      output.write(buffer_out);
  }
  </code></pre>
  
  <p>The problem in Rust I have is that I'm trying to use <code>Cursor</code> object to implement streaming access to both buffers. For example, I'm allocating the buffer on the heap like this:</p>
  
  <pre><code>let mut buf: Box&lt;[u8; BUF_SIZE]&gt; = Box::new([0; BUF_SIZE]);
  </code></pre>
  
  <p>And then I'm creating a Cursor to access this array in a streaming mode:</p>
  
  <pre><code>let mut rd_cursor: Cursor&lt;&amp;[u8]&gt; = Cursor::new(buf.as_slice());
  </code></pre>
  
  <p>However, I have no idea how to read the data from the input file now. <code>buf</code> is used by the <code>Cursor</code>, so I can't access it. In C++ I would just read the data to <code>buf</code> and be done with it. And <code>Cursor</code> doesn't seem to implement anything that can be used directly by <code>BufReader.read()</code>, which I use to read data from the input file.</p>
  
  <p>Perhaps I could make it work by creating another buffer, read data from 'input' to the temporary buffer, from temporary buffer to 'buf' through the Cursor, but that would result in constant recopying of memory, which I would like to avoid.</p>
  
  <p>I can see there is a <code>fill_buf</code> function in <code>Cursor</code>, but it seems that it returns only a readonly reference to underlying buffer, so I can't modify the buffer, thus it's useless for my case.</p>
  
  <p>I have also tried using <code>BufReader</code> instead of <code>Cursor</code>. Here is my second try:</p>
  
  <pre><code>let mut rd_cursor: BufReader&lt;&amp;[u8]&gt; = BufReader::new(&amp;*buf);
  </code></pre>
  
  <p><code>BufReader&lt;R&gt;</code> contains <code>get_mut</code> returning <code>R</code>, so I think it should return <code>&amp;[u8]</code> in my case, which sounds like a good thing. But by using <code>&amp;[u8]</code>, <code>get_mut</code> complains that I need to pass a mutable thing as <code>R</code>. So I'm changing it like this:</p>
  
  <pre><code>let mut rd_cursor: BufReader&lt;&amp;mut [u8]&gt; = BufReader::new(&amp;mut *buf);
  </code></pre>
  
  <p>But Rust won't let me:</p>
  
  <pre><code>src\main.rs|88 col 47| 88:61 error: the trait `std::io::Read` is not implemented for the type `[u8]` [E0277]
  || src\main.rs:88     let mut rd_cursor: BufReader&lt;&amp;mut [u8]&gt; = BufReader::new(&amp;mut *buf);
  </code></pre>
  
  <p>Could anyone please hit me in the head to fix my understanding of what is happening here?</p>