#random-access #io #data-access #reader-writer #buffered #structs #file-io

bufreaderwriter

Rust convenience structs that facilitate automatic switching between buffered reading and writing from a single underlying IO instance. BufReaderWriterRand for random IO and BufReaderWriterSeq for sequential IO.

6 releases

0.2.4 May 22, 2023
0.2.3 May 9, 2023
0.1.2 Feb 24, 2021
0.1.1 Dec 6, 2020

#307 in Filesystem

Download history 376/week @ 2023-12-11 456/week @ 2023-12-18 98/week @ 2023-12-25 104/week @ 2024-01-01 138/week @ 2024-01-08 114/week @ 2024-01-15 159/week @ 2024-01-22 169/week @ 2024-01-29 277/week @ 2024-02-05 281/week @ 2024-02-12 334/week @ 2024-02-19 474/week @ 2024-02-26 317/week @ 2024-03-04 234/week @ 2024-03-11 187/week @ 2024-03-18 208/week @ 2024-03-25

1,007 downloads per month
Used in 2 crates

MIT/Apache

26KB
478 lines

BufReaderWriter

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.


lib.rs:

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(())
}

No runtime deps