I have a reference to an uninitialized memory buffer in form of &mut [mem::MaybeUninit<u8>]
and an AsyncRead
implementation. I want to asynchronously read some data and write them to the buffer.
How to do that with Tokio?
async fn read_into(src: impl AsyncRead, dest: &mut [mem::MaybeUninit<u8>]) -> usize {
/// how to asynchronously copy available data from `src` to `dest` and return the number of bytes copied?
}
I found the read_buf
method in AsyncReadExt
which looks like something I should probably use, however, that method does not accept my uninit buffer, but a BufMut implementation.
I have the following ideas:
- wrap my buffer into a custom struct and implement
BufMut
for it by hand. - maybe Tokio or some other crate already provides such implementation? Looked at
BufRead
for a while, which can be initialized with my buffer, but it does not implementBufMut
I don't have control over the creation of the uninitialized buffer, I get it from another crate, and I want to avoid additional copying (sure I could read into a temporary buffer and then copy, but this is a performance critical path). Therefore I don't think I can use bytes::BytesMut
, because it looks like it maintains its own buffer and cannot wrap an existing one.
Thanks for help