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

unsafe impl<'a> BufMut for ReadBuf<'a> #5590

Merged
merged 19 commits into from
Apr 4, 2023
20 changes: 20 additions & 0 deletions tokio/src/io/read_buf.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(feature = "bytes")]
use bytes::{buf::UninitSlice, BufMut};

use std::fmt;
use std::mem::MaybeUninit;

Expand Down Expand Up @@ -270,6 +273,23 @@ impl<'a> ReadBuf<'a> {
}
}

#[cfg(feature = "bytes")]
amab8901 marked this conversation as resolved.
Show resolved Hide resolved
unsafe impl<'a> BufMut for ReadBuf<'a> {
amab8901 marked this conversation as resolved.
Show resolved Hide resolved
fn remaining_mut(&self) -> usize {
self.remaining()
}

unsafe fn advance_mut(&mut self, cnt: usize) {
self.advance(cnt);
}

fn chunk_mut(&mut self) -> &mut UninitSlice {
let len = self.filled().len();
amab8901 marked this conversation as resolved.
Show resolved Hide resolved
let ptr = unsafe { self.unfilled_mut().as_mut_ptr() };
unsafe { UninitSlice::from_raw_parts_mut(ptr as *mut u8, len) }
}
}

impl fmt::Debug for ReadBuf<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ReadBuf")
Expand Down