Crate positioned_io [] [src]

This crate allows you to specify an offset for reads and writes, without changing the current position in a file. This is similar to pread() and pwrite() in C.

The major advantages of this type of I/O are:

  • You don't need to seek before doing a random-access read or write, which is convenient.
  • Reads don't modify the file at all, so don't require mutability.


Read the fifth 512-byte sector of a file:

use positioned_io::ReadAt;

// Note that file does not need to be mut!
let file = try!(File::open(""));
let mut buf = vec![0; 512];
let bytes_read = try!(file.read_at(2048, &mut buf));

Write an integer to the middle of a file:

use positioned_io::WriteAt;
use byteorder::{ByteOrder, LittleEndian};

// Put the integer in a buffer.
let mut buf = vec![0; 4];
LittleEndian::write_u32(&mut buf, 1234);

// Write it to the file.
let mut file = try!(OpenOptions::new().write(true).open(""));
try!(file.write_all_at(1 << 20, &buf));

Or, more simply:

// Extend files with writing integers at offsets.
use positioned_io::WriteBytesExt;

let mut file = try!(OpenOptions::new().write(true).open(""));
try!(file.write_u32_at::<LittleEndian>(1 << 20, 1234));

Read from anything else that supports ReadAt, like a byte array:

use positioned_io::ReadBytesExt;

let buf = [0, 5, 254, 212, 0, 3];
let n = try!(buf.as_ref().read_i16_at::<BigEndian>(2));
assert_eq!(n, -300);



Read or write with a given inherent byte-order.


Adapts a ReadAt or WriteAt into a Read or Write.


Adapts a ReadAt or WriteAt into a Read or Write, with better seeking.


A window into another ReatAt or WriteAt.



Trait for reading at an offset.


Extends ReadAt with methods for reading numbers at offsets.


Trait to get the size of an I/O object.


Trait for writing at an offset.


Extends WriteAt with methods for writing numbers at offsets.