Auto merge of #123786 - a1phyr:cursor_unsafe, r=joboet
Remove bound checks from `BorrowedBuf` and `BorrowedCursor` methods
This commit is contained in:
commit
959a67a7f2
@ -92,14 +92,20 @@ pub fn init_len(&self) -> usize {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub fn filled(&self) -> &[u8] {
|
pub fn filled(&self) -> &[u8] {
|
||||||
// SAFETY: We only slice the filled part of the buffer, which is always valid
|
// SAFETY: We only slice the filled part of the buffer, which is always valid
|
||||||
unsafe { MaybeUninit::slice_assume_init_ref(&self.buf[0..self.filled]) }
|
unsafe {
|
||||||
|
let buf = self.buf.get_unchecked(..self.filled);
|
||||||
|
MaybeUninit::slice_assume_init_ref(buf)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a mutable reference to the filled portion of the buffer.
|
/// Returns a mutable reference to the filled portion of the buffer.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn filled_mut(&mut self) -> &mut [u8] {
|
pub fn filled_mut(&mut self) -> &mut [u8] {
|
||||||
// SAFETY: We only slice the filled part of the buffer, which is always valid
|
// SAFETY: We only slice the filled part of the buffer, which is always valid
|
||||||
unsafe { MaybeUninit::slice_assume_init_mut(&mut self.buf[0..self.filled]) }
|
unsafe {
|
||||||
|
let buf = self.buf.get_unchecked_mut(..self.filled);
|
||||||
|
MaybeUninit::slice_assume_init_mut(buf)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a cursor over the unfilled part of the buffer.
|
/// Returns a cursor over the unfilled part of the buffer.
|
||||||
@ -205,7 +211,10 @@ pub fn written(&self) -> usize {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub fn init_ref(&self) -> &[u8] {
|
pub fn init_ref(&self) -> &[u8] {
|
||||||
// SAFETY: We only slice the initialized part of the buffer, which is always valid
|
// SAFETY: We only slice the initialized part of the buffer, which is always valid
|
||||||
unsafe { MaybeUninit::slice_assume_init_ref(&self.buf.buf[self.buf.filled..self.buf.init]) }
|
unsafe {
|
||||||
|
let buf = self.buf.buf.get_unchecked(self.buf.filled..self.buf.init);
|
||||||
|
MaybeUninit::slice_assume_init_ref(buf)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a mutable reference to the initialized portion of the cursor.
|
/// Returns a mutable reference to the initialized portion of the cursor.
|
||||||
@ -213,7 +222,8 @@ pub fn init_ref(&self) -> &[u8] {
|
|||||||
pub fn init_mut(&mut self) -> &mut [u8] {
|
pub fn init_mut(&mut self) -> &mut [u8] {
|
||||||
// SAFETY: We only slice the initialized part of the buffer, which is always valid
|
// SAFETY: We only slice the initialized part of the buffer, which is always valid
|
||||||
unsafe {
|
unsafe {
|
||||||
MaybeUninit::slice_assume_init_mut(&mut self.buf.buf[self.buf.filled..self.buf.init])
|
let buf = self.buf.buf.get_unchecked_mut(self.buf.filled..self.buf.init);
|
||||||
|
MaybeUninit::slice_assume_init_mut(buf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -222,7 +232,8 @@ pub fn init_mut(&mut self) -> &mut [u8] {
|
|||||||
/// It is safe to uninitialize any of these bytes.
|
/// It is safe to uninitialize any of these bytes.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn uninit_mut(&mut self) -> &mut [MaybeUninit<u8>] {
|
pub fn uninit_mut(&mut self) -> &mut [MaybeUninit<u8>] {
|
||||||
&mut self.buf.buf[self.buf.init..]
|
// SAFETY: always in bounds
|
||||||
|
unsafe { self.buf.buf.get_unchecked_mut(self.buf.init..) }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a mutable reference to the whole cursor.
|
/// Returns a mutable reference to the whole cursor.
|
||||||
@ -232,7 +243,8 @@ pub fn uninit_mut(&mut self) -> &mut [MaybeUninit<u8>] {
|
|||||||
/// The caller must not uninitialize any bytes in the initialized portion of the cursor.
|
/// The caller must not uninitialize any bytes in the initialized portion of the cursor.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub unsafe fn as_mut(&mut self) -> &mut [MaybeUninit<u8>] {
|
pub unsafe fn as_mut(&mut self) -> &mut [MaybeUninit<u8>] {
|
||||||
&mut self.buf.buf[self.buf.filled..]
|
// SAFETY: always in bounds
|
||||||
|
unsafe { self.buf.buf.get_unchecked_mut(self.buf.filled..) }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Advance the cursor by asserting that `n` bytes have been filled.
|
/// Advance the cursor by asserting that `n` bytes have been filled.
|
||||||
|
Loading…
Reference in New Issue
Block a user