pub struct MinBuffered(pub usize);
Expand description
A policy for BufReader
which ensures there is at least the given number of
bytes in the buffer, failing this only if the reader is at EOF.
If the minimum buffer length is greater than the buffer capacity, it will be resized.
§Example
use buf_redux::BufReader;
use buf_redux::policy::MinBuffered;
use std::io::{BufRead, Cursor};
let data = (1 .. 16).collect::<Vec<u8>>();
// normally you should use `BufReader::new()` or give a capacity of several KiB or more
let mut reader = BufReader::with_capacity(8, Cursor::new(data))
// always at least 4 bytes in the buffer (or until the source is empty)
.set_policy(MinBuffered(4)); // always at least 4 bytes in the buffer
// first buffer fill, same as `std::io::BufReader`
assert_eq!(reader.fill_buf().unwrap(), &[1, 2, 3, 4, 5, 6, 7, 8]);
reader.consume(3);
// enough data in the buffer, another read isn't done yet
assert_eq!(reader.fill_buf().unwrap(), &[4, 5, 6, 7, 8]);
reader.consume(4);
// `std::io::BufReader` would return `&[8]`
assert_eq!(reader.fill_buf().unwrap(), &[8, 9, 10, 11, 12, 13, 14, 15]);
reader.consume(5);
// no data left in the reader
assert_eq!(reader.fill_buf().unwrap(), &[13, 14, 15]);
Tuple Fields§
§0: usize
Implementations§
Trait Implementations§
Source§impl Debug for MinBuffered
impl Debug for MinBuffered
Source§impl ReaderPolicy for MinBuffered
impl ReaderPolicy for MinBuffered
Auto Trait Implementations§
impl Freeze for MinBuffered
impl RefUnwindSafe for MinBuffered
impl Send for MinBuffered
impl Sync for MinBuffered
impl Unpin for MinBuffered
impl UnwindSafe for MinBuffered
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more