Crate bufreaderwriter

Source
Expand description

The BufReaderWriterRand<RW> and BufReaderWriterSeq<RW> are convenience structs that facilitate automatic switching between buffered reading and writing from a single underlying IO instance. BufReaderWriterRand is for random access IO (i.e. Read + Write + Seek, such as std::fs::File), while BufReaderWriterSeq is for sequential IO (i.e. Read + Write, such as std::net::TcpStream).

Both structs move the underlying IO instance between a BufReader and BufWriter as needed. However, when switching from reading to writing, BufReaderWriterRand discards any buffered data and seeks the underlying IO instance back to the current BufReader position, while BufReaderWriterSeq saves any buffered data and makes it available for subsequent reads.

§Example

use bufreaderwriter::rand::BufReaderWriterRand;
use tempfile::tempfile;

fn main() -> io::Result<()> {
    let mut brw = BufReaderWriterRand::new_writer(tempfile()?);
    let data = "The quick brown fox jumps over the lazy dog".to_owned();
    brw.write(data.as_bytes())?;

    brw.seek(SeekFrom::Start(0))?;
    let mut bin = vec![0; data.len()];
    brw.read(&mut bin)?;
    Ok(())
}

Modules§

rand
seq