title: Writing to statically-sized file with offset
body: <p>I am trying to write bytes to a file with random access in the context of downloading random chunks of the file and assembling them in-place. The <code>std::io::Write</code> seem to be limited to sequential writing so I am currently using the following implementation which first needs to download all the chunks and then do big <a href="https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all" rel="nofollow noreferrer"><code>write_all</code></a> once all the chunks are in memory.</p>
  <pre class="lang-rust prettyprint-override"><code>use rand::prelude::*;
  use rand; // 0.7.3
  
  fn download&lt;W: std::io::Write&gt;(writer: &amp;mut W) -&gt; std::io::Result&lt;()&gt;
  {
      // the size (20) is known
      let mut buf = [0u8; 20];
      
      // simulate downloading four 5 byte chunks at random disjoint slices in *buf*
      for n in (0..20).step_by(5).collect::&lt;Vec&lt;usize&gt;&gt;().choose_multiple(&amp;mut thread_rng(), 4) {
          let n = *n as usize;
          let chunk: [u8; 5] = rand::random();
          buf[n..n+5].clone_from_slice(&amp;chunk);
      }
      
      println!(&quot;filled buffer: {:?}&quot;, buf);
      
      // finally, do (unbuffered) write
      writer.write_all(&amp;buf)?;
      
      Ok(())
  }
  
  fn main() -&gt; std::io::Result&lt;()&gt; {
  
      let mut f = std::fs::File::create(&quot;foo.txt&quot;)?;
      download(&amp;mut f)?;
      
      Ok(())
  }
  </code></pre>
  <p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2021&amp;gist=7d1f2c972ce49400929ad7953b6b5e23" rel="nofollow noreferrer">playground link</a></p>
  <p>With large files I think this solution may consume to much memory and I would rather write each <code>chunk</code> directly into the file without any intermediate buffer. I see there is <a href="https://doc.rust-lang.org/std/os/unix/fs/trait.FileExt.html#tymethod.write_at" rel="nofollow noreferrer"><code>write_at</code></a> in the documentation, but is this method unique for unix systems? I am looking for a generic solution.</p>
  <p>Intuitively, I would want something like this to work:</p>
  <pre class="lang-rust prettyprint-override"><code>use rand::prelude::*;
  use rand; // 0.7.3
  
  fn download&lt;W: std::io::Write&gt;(writer: &amp;mut W) -&gt; std::io::Result&lt;()&gt;
  {
      // the size (20) is known
      writer.reserve(20)?;
  
      // simulate downloading four 5 byte chunks at random disjoint slices in *buf*
      for n in (0..20).step_by(5).collect::&lt;Vec&lt;usize&gt;&gt;().choose_multiple(&amp;mut thread_rng(), 4) {
          let n = *n as usize;
          let chunk: [u8; 5] = rand::random();
          writer.write_at(chunk, n)?;
      }
  
      Ok(())
  }
  
  fn main() -&gt; std::io::Result&lt;()&gt; {
  
      let mut f = std::fs::File::create(&quot;foo.txt&quot;)?;
      download(&amp;mut f)?;
  
      Ok(())
  }
  </code></pre>
  <p>Is there any specialization for writing to random access file output? Can I use a memory mapped file? Doing a quick search I could only find the <a href="https://github.com/vasi/positioned-io" rel="nofollow noreferrer">positioned-io</a> crate, which has not been updated in 3 years. Is there anything similar to this crate in the rust standard library?</p>