Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

io: add ReadBuf::take #2817

Merged
merged 1 commit into from Sep 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions tokio/src/io/read_buf.rs
Expand Up @@ -77,6 +77,14 @@ impl<'a> ReadBuf<'a> {
unsafe { mem::transmute::<&mut [MaybeUninit<u8>], &mut [u8]>(slice) }
}

/// Returns a new `ReadBuf` comprised of the unfilled section up to `n`.
#[inline]
pub fn take(&mut self, n: usize) -> ReadBuf<'_> {
let max = std::cmp::min(self.remaining(), n);
// Saftey: We don't set any of the `unfilled_mut` with `MaybeUninit::uninit`.
unsafe { ReadBuf::uninit(&mut self.unfilled_mut()[..max]) }
}

/// Returns a shared reference to the initialized portion of the buffer.
///
/// This includes the filled portion.
Expand Down
5 changes: 1 addition & 4 deletions tokio/src/io/util/take.rs
Expand Up @@ -85,10 +85,7 @@ impl<R: AsyncRead> AsyncRead for Take<R> {
}

let me = self.project();
let max = std::cmp::min(buf.remaining() as u64, *me.limit_) as usize;
// Make a ReadBuf of the unfulled section up to max
// Saftey: We don't set any of the `unfilled_mut` with `MaybeUninit::uninit`.
let mut b = unsafe { ReadBuf::uninit(&mut buf.unfilled_mut()[..max]) };
let mut b = buf.take(*me.limit_ as usize);
ready!(me.inner.poll_read(cx, &mut b))?;
let n = b.filled().len();

Expand Down