title: How to read (std::io::Read) from a Vec or Slice?
body: <p><code>Vec</code>s support <code>std::io::Write</code>, so code can be written that takes a <code>File</code> or <code>Vec</code>, for example. From the API reference, it looks like neither <code>Vec</code> nor slices support <code>std::io::Read</code>.</p>
  
  <p>Is there a convenient way to achieve this? Does it require writing a wrapper struct?</p>
  
  <p>Here is an example of working code, that reads and writes a file, with a single line commented that should read a vector.</p>
  
  <pre><code>use ::std::io;
  
  // Generic IO
  fn write_4_bytes&lt;W&gt;(mut file: W) -&gt; Result&lt;usize, io::Error&gt;
      where W: io::Write,
  {
      let len = file.write(b"1234")?;
      Ok(len)
  }
  
  fn read_4_bytes&lt;R&gt;(mut file: R) -&gt; Result&lt;[u8; 4], io::Error&gt;
      where R: io::Read,
  {
      let mut buf: [u8; 4] = [0; 4];
      file.read(&amp;mut buf)?;
      Ok(buf)
  }
  
  // Type specific
  
  fn write_read_vec() {
      let mut vec_as_file: Vec&lt;u8&gt; = Vec::new();
  
      {   // Write
          println!("Writing Vec... {}", write_4_bytes(&amp;mut vec_as_file).unwrap());
      }
  
      {   // Read
  //      println!("Reading File... {:?}", read_4_bytes(&amp;vec_as_file).unwrap());
          //                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
          //                               Comment this line above to avoid an error!
      }
  }
  
  fn write_read_file() {
      let filepath = "temp.txt";
      {   // Write
          let mut file_as_file = ::std::fs::File::create(filepath).expect("open failed");
          println!("Writing File... {}", write_4_bytes(&amp;mut file_as_file).unwrap());
      }
  
      {   // Read
          let mut file_as_file = ::std::fs::File::open(filepath).expect("open failed");
          println!("Reading File... {:?}", read_4_bytes(&amp;mut file_as_file).unwrap());
      }
  }
  
  fn main() {
      write_read_vec();
      write_read_file();
  }
  </code></pre>
  
  <p>This fails with the error:</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0277]: the trait bound `std::vec::Vec&lt;u8&gt;: std::io::Read` is not satisfied
    --&gt; src/main.rs:29:42
     |
  29 |         println!("Reading File... {:?}", read_4_bytes(&amp;vec_as_file).unwrap());
     |                                          ^^^^^^^^^^^^ the trait `std::io::Read` is not implemented for `std::vec::Vec&lt;u8&gt;`
     |
     = note: required by `read_4_bytes`
  </code></pre>
  
  <p>I'd like to write tests for a file format encoder/decoder, without having to write to the file-system.</p>