pub async fn read_exact_uninit<R>(reader: &mut R, len: usize) -> Result<Vec<u8>>Expand description
Read exactly len bytes from an async reader without zero-initializing
the buffer first.
This avoids the double-write overhead of vec![0u8; len] followed by
read_exact — read_buf appends directly into the Vec’s spare capacity,
so the zeroing pass is skipped. For large payloads this can yield
measurable performance gains.
The returned Vec has exactly len initialized bytes.
This used to be an unsafe function that handed read_exact a
&mut [u8] over uninitialized memory via from_raw_parts_mut. That was
unsound: tokio::io::ReadBuf::new(&mut [u8]) asserts the whole buffer is
initialized, so a reader conforming to the ReadBuf contract would be
entitled to read the (uninitialized) [filled, initialized) region. The
read_buf-based implementation below is fully safe and keeps the same
zero-overhead property, miri-clean by construction.
§Errors
Returns io::ErrorKind::UnexpectedEof if the reader reaches EOF before
len bytes have been read.