A drop-in replacement for std::io::BufReader
with seeking support.
use std::io::{self, Cursor, Read, Seek, SeekFrom};
use seek_bufread::BufReader;
let inner = Cursor::new([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
let mut reader = BufReader::new(inner);
reader.seek(SeekFrom::Current(4)).unwrap();
let mut buf = [0; 8];
// read bytes from internal buffer
reader.read(&mut buf).unwrap();
assert_eq!(buf, [4, 5, 6, 7, 8, 9, 10, 11]);
Put this in your Cargo.toml
:
[dependencies]
seek_bufread = "~1.2"
And this in your crate root:
extern crate seek_bufread;
Tests with the suffix _std
are using the standard std::io::BufRead
implementation. The Overall performance without seek operations is
quite similar between both. With seek operations seek_bufread::BufRead
is significantly faster.
test tests::read_10mb_default_from_cursor ... bench: 6,044,915 ns/iter (+/- 275,518)
test tests::read_10mb_default_from_cursor_std ... bench: 6,038,466 ns/iter (+/- 227,145)
test tests::read_10mb_default_from_file ... bench: 4,213,179 ns/iter (+/- 116,043)
test tests::read_10mb_default_from_file_std ... bench: 4,224,658 ns/iter (+/- 132,629)
test tests::read_10mb_fullbuf_from_file ... bench: 7,280,470 ns/iter (+/- 209,827)
test tests::read_10mb_fullbuf_from_file_std ... bench: 7,448,666 ns/iter (+/- 2,720,199)
test tests::read_10mb_halfbuf_from_file ... bench: 5,962,017 ns/iter (+/- 415,952)
test tests::read_10mb_halfbuf_from_file_std ... bench: 5,904,902 ns/iter (+/- 240,471)
test tests::read_seek_10mb_default_from_file ... bench: 6,621 ns/iter (+/- 145)
test tests::read_seek_10mb_default_from_file_std ... bench: 59,651 ns/iter (+/- 1,993)
test tests::read_seek_10mb_halfbuf_from_file ... bench: 1,168,511 ns/iter (+/- 63,956)
test tests::read_seek_10mb_halfbuf_from_file_std ... bench: 62,872,335 ns/iter (+/- 5,344,766)
Apache-2.0