util: update to bytes 0.6 (#3071)

Copies the implementation of poll_read_buf() from tokio::io::util::read_buf.
This commit is contained in:
Dirkjan Ochtman
2020-10-29 10:45:19 -07:00
committed by GitHub
parent a3ef4e4cf5
commit 3965d91a5e
3 changed files with 16 additions and 8 deletions
+14 -6
View File
@@ -65,6 +65,7 @@ mod util {
use bytes::BufMut;
use futures_core::ready;
use std::io;
use std::mem::MaybeUninit;
use std::pin::Pin;
use std::task::{Context, Poll};
@@ -77,17 +78,24 @@ mod util {
return Poll::Ready(Ok(0));
}
let orig = buf.bytes_mut().as_ptr() as *const u8;
let mut b = ReadBuf::uninit(buf.bytes_mut());
let n = {
let dst = buf.bytes_mut();
let dst = unsafe { &mut *(dst as *mut _ as *mut [MaybeUninit<u8>]) };
let mut buf = ReadBuf::uninit(dst);
let ptr = buf.filled().as_ptr();
ready!(io.poll_read(cx, &mut buf)?);
ready!(io.poll_read(cx, &mut b))?;
let n = b.filled().len();
// Ensure the pointer does not change from under us
assert_eq!(ptr, buf.filled().as_ptr());
buf.filled().len()
};
// Safety: we can assume `n` bytes were read, since they are in`filled`.
assert_eq!(orig, b.filled().as_ptr());
// Safety: This is guaranteed to be the number of initialized (and read)
// bytes due to the invariants provided by `ReadBuf::filled`.
unsafe {
buf.advance_mut(n);
}
Poll::Ready(Ok(n))
}
}