Skip to main content

read_buf

Function read_buf 

Source
pub fn read_buf<R: Read + ?Sized>(
    reader: &mut R,
    cursor: BorrowedCursor<'_, u8>,
) -> Result<()>
Expand description

Reads from reader into cursor with a single Read::read call.

Mirrors the default implementation of the nightly Read::read_buf. The unfilled part of the underlying BorrowedBuf is zeroed first, but only once over its lifetime, so reading in a loop stays cheap.

ยงPanics

Panics if reader reports more bytes than the slice it was given.

use borrowed_buf::{BorrowedBuf, io};
use core::mem::MaybeUninit;

let mut reader: &[u8] = b"hello world";
let mut storage = [MaybeUninit::uninit(); 64];
let mut buf = BorrowedBuf::from(&mut storage[..]);

// Read until EOF, zeroing the buffer only once.
loop {
    let before = buf.len();
    io::read_buf(&mut reader, buf.unfilled())?;
    if buf.len() == before {
        break;
    }
}
assert_eq!(buf.filled(), b"hello world");