tokio-rs / bytes

Utilities for working with bytes
MIT License
1.91k stars 288 forks source link

Implement BufMut for `&mut [MaybeUninit<u8>]` #597

Closed mina86 closed 1 year ago

mina86 commented 1 year ago

This will allow using BufMut without having to initialise memory first. In particular, this will allow simple integration with tokio:

#![feature(maybe_uninit_slice)]

use tokio::io::AsyncReadExt;

let mut tokio_file = /* ... */;
let mut buf = [MaybeUninit::uninit(); 4096];
let len = tokio_file.read_buf(&mut &mut buf[..]);
// SAFETY: read initialised first len bytes of the buffer.
let buf = unsafe {
    MaybeUninit::slice_assume_init_mut(&buf[..len])
};
mina86 commented 1 year ago

Ha, sorry, was actually in the middle of redoing tests. Now &mut [u8] and &mut [MaybeUninit<u8>] have the same tests.