Struct buf_redux::policy::MinBuffered[][src]

pub struct MinBuffered(pub usize);

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]);

Methods

impl MinBuffered
[src]

Set the number of bytes to ensure are in the buffer.

Trait Implementations

impl Debug for MinBuffered
[src]

Formats the value using the given formatter. Read more

impl ReaderPolicy for MinBuffered
[src]

Consulted before attempting to read into the buffer. Read more

Called after bytes are consumed from the buffer. Read more

Auto Trait Implementations

impl Send for MinBuffered

impl Sync for MinBuffered