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.
The repository is at https://github.com/vasi/positioned-io
Examples
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("foo.data")); 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("foo.data")); 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("foo.data")); 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);
Structs
ByteIo |
Read or write with a given inherent byte-order. |
Cursor |
Adapts a |
SizeCursor |
Adapts a |
Slice |
A window into another |
Traits
ReadAt |
Trait for reading at an offset. |
ReadBytesExt |
Extends |
ReadInt |
Trait for reading integers. |
ReadIntAt |
Trait for reading positioned integers. |
Size |
Trait to get the size of an I/O object. |
WriteAt |
Trait for writing at an offset. |
WriteBytesExt |
Extends |
WriteInt |
Trait for writing integers. |
WriteIntAt |
Trait for writing positioned integers. |