mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-29 00:00:11 +02:00
io: remove poll_{read,write}_buf from traits (#2882)
These functions have object safety issues. It also has been decided to avoid vectored operations on the I/O traits. A later PR will bring back vectored operations on specific types that support them. Refs: #2879, #2716
This commit is contained in:
@@ -52,3 +52,37 @@ pub mod context;
|
||||
pub mod sync;
|
||||
|
||||
pub mod either;
|
||||
|
||||
#[cfg(any(feature = "io", feature = "codec"))]
|
||||
mod util {
|
||||
use tokio::io::{AsyncRead, ReadBuf};
|
||||
|
||||
use bytes::BufMut;
|
||||
use futures_core::ready;
|
||||
use std::io;
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
pub(crate) fn poll_read_buf<T: AsyncRead>(
|
||||
cx: &mut Context<'_>,
|
||||
io: Pin<&mut T>,
|
||||
buf: &mut impl BufMut,
|
||||
) -> Poll<io::Result<usize>> {
|
||||
if !buf.has_remaining_mut() {
|
||||
return Poll::Ready(Ok(0));
|
||||
}
|
||||
|
||||
let orig = buf.bytes_mut().as_ptr() as *const u8;
|
||||
let mut b = ReadBuf::uninit(buf.bytes_mut());
|
||||
|
||||
ready!(io.poll_read(cx, &mut b))?;
|
||||
let n = b.filled().len();
|
||||
|
||||
// Safety: we can assume `n` bytes were read, since they are in`filled`.
|
||||
assert_eq!(orig, b.filled().as_ptr());
|
||||
unsafe {
|
||||
buf.advance_mut(n);
|
||||
}
|
||||
Poll::Ready(Ok(n))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user