io::read_max always returns an empty array
Squirrelcoding opened this issue · comments
I was investigating another issue in the esp32 std demo and this seems to be the problem. Judging by the code, this is what happens:
- Initiate
offset
andsize
variables - Read some data into the buffer
- break the loop since
size
is 0 and not manipulated yet - Return the buffer up until the index
size
, which is 0. This makes the function always return an empty array
pub fn read_max<R: Read>(mut read: R, buf: &mut [u8]) -> Result<(&[u8], usize), R::Error> {
let mut offset = 0;
let mut size = 0;
loop {
let r = read.read(&mut buf[offset..])?;
if size == 0 {
break;
}
offset += r;
size += r;
}
Ok((&buf[..size], size))
}
I couldn't find the code in this repo but its right here on docs.rs
As mentioned in the other thread, the function is buggy, but the bug is fixed in master
. And the function is renamed.