[][src]Crate buffered_offset_reader

BufOffsetReader is like std::io::BufReader, but it allows reading at arbitrary positions in the underlying file.

Uses std::os::unix::fs::FileExt::read_at() on unix (aka pread()) and std::os::windows::fs::FileExt::seek_read() on windows to read from the underlying file in a thread-safe manner, so only a non-mutable reference to the file is needed.

Examples

use buffered_offset_reader::{BufOffsetReader, OffsetReadMut};
use std::fs::File;

fn main() -> std::io::Result<()> {
    let f = File::open("log.txt")?;
    let mut r = BufOffsetReader::new(f);
    let mut buf = vec![0; 8];

    r.read_at(&mut buf, 0)?;  // read 8 bytes at offset 0
    r.read_at(&mut buf, 32)?; // read 8 bytes at offset 32
    Ok(())
}

NB: The buffering logic is currently very simple: if the requested range isn't completely contained in the buffer, we read capacity bytes into memory, starting at the requested offset. This works well for generally "forward" reads, but not so great for eg. iterating backward through a file.

Structs

BufOffsetReader

Traits

OffsetRead
OffsetReadMut
OffsetWrite