Function bdrck::io::read_at_most_into

source ·
pub fn read_at_most_into<R: Read>(r: &mut R, buf: &mut [u8]) -> Result<usize>
Expand description

Reads from the givne Read until the buffer is filled. If EOF is reached first, this is fine. If we hit EOF exactly when the buffer is filled, that’s also fine.

However, if there are bytes remaining after the buffer is filled (we didn’t hit EOF), this is considered an error.

This function is useful when you are reading e.g. user-provided input of unknown size, and you want to place an upper bound on it (e.g. to avoid OOMs.)

NOTE: A limitation here is that, if buf is exactly big enough to hold the data, we must read one extra byte past there (which is then discarded). This means you can’t rely on continuing to use the Read after calling this function. For the intended use cases, this is not a problem, but it needs to be kept in mind.