summaryrefslogtreecommitdiffstats
path: root/tokio/src/io/util/read_buf.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tokio/src/io/util/read_buf.rs')
-rw-r--r--tokio/src/io/util/read_buf.rs57
1 files changed, 34 insertions, 23 deletions
diff --git a/tokio/src/io/util/read_buf.rs b/tokio/src/io/util/read_buf.rs
index 696deefd..7df429d7 100644
--- a/tokio/src/io/util/read_buf.rs
+++ b/tokio/src/io/util/read_buf.rs
@@ -40,33 +40,44 @@ where
type Output = io::Result<usize>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
- use crate::io::ReadBuf;
- use std::mem::MaybeUninit;
-
- let me = self.project();
+ let mut me = self.project();
+ poll_read_buf(&mut me.reader, &mut me.buf, cx)
+ }
+}
- if !me.buf.has_remaining_mut() {
- return Poll::Ready(Ok(0));
- }
+pub(crate) fn poll_read_buf<'a, R, B>(
+ reader: &'a mut R,
+ buf: &'a mut B,
+ cx: &mut Context<'_>,
+) -> Poll<io::Result<usize>>
+where
+ R: AsyncRead + Unpin,
+ B: BufMut,
+{
+ use crate::io::ReadBuf;
+ use std::mem::MaybeUninit;
- let n = {
- let dst = me.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!(Pin::new(me.reader).poll_read(cx, &mut buf)?);
+ if !buf.has_remaining_mut() {
+ return Poll::Ready(Ok(0));
+ }
- // Ensure the pointer does not change from under us
- assert_eq!(ptr, buf.filled().as_ptr());
- buf.filled().len()
- };
+ 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!(Pin::new(reader).poll_read(cx, &mut buf)?);
- // Safety: This is guaranteed to be the number of initialized (and read)
- // bytes due to the invariants provided by `ReadBuf::filled`.
- unsafe {
- me.buf.advance_mut(n);
- }
+ // Ensure the pointer does not change from under us
+ assert_eq!(ptr, buf.filled().as_ptr());
+ buf.filled().len()
+ };
- Poll::Ready(Ok(n))
+ // 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))
}