Skip to content

Commit 959a67a

Browse files
committed
Auto merge of #123786 - a1phyr:cursor_unsafe, r=joboet
Remove bound checks from `BorrowedBuf` and `BorrowedCursor` methods
2 parents 6982935 + 6ce268e commit 959a67a

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

library/core/src/io/borrowed_buf.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,20 @@ impl<'data> BorrowedBuf<'data> {
9292
#[inline]
9393
pub fn filled(&self) -> &[u8] {
9494
// SAFETY: We only slice the filled part of the buffer, which is always valid
95-
unsafe { MaybeUninit::slice_assume_init_ref(&self.buf[0..self.filled]) }
95+
unsafe {
96+
let buf = self.buf.get_unchecked(..self.filled);
97+
MaybeUninit::slice_assume_init_ref(buf)
98+
}
9699
}
97100

98101
/// Returns a mutable reference to the filled portion of the buffer.
99102
#[inline]
100103
pub fn filled_mut(&mut self) -> &mut [u8] {
101104
// SAFETY: We only slice the filled part of the buffer, which is always valid
102-
unsafe { MaybeUninit::slice_assume_init_mut(&mut self.buf[0..self.filled]) }
105+
unsafe {
106+
let buf = self.buf.get_unchecked_mut(..self.filled);
107+
MaybeUninit::slice_assume_init_mut(buf)
108+
}
103109
}
104110

105111
/// Returns a cursor over the unfilled part of the buffer.
@@ -205,15 +211,19 @@ impl<'a> BorrowedCursor<'a> {
205211
#[inline]
206212
pub fn init_ref(&self) -> &[u8] {
207213
// SAFETY: We only slice the initialized part of the buffer, which is always valid
208-
unsafe { MaybeUninit::slice_assume_init_ref(&self.buf.buf[self.buf.filled..self.buf.init]) }
214+
unsafe {
215+
let buf = self.buf.buf.get_unchecked(self.buf.filled..self.buf.init);
216+
MaybeUninit::slice_assume_init_ref(buf)
217+
}
209218
}
210219

211220
/// Returns a mutable reference to the initialized portion of the cursor.
212221
#[inline]
213222
pub fn init_mut(&mut self) -> &mut [u8] {
214223
// SAFETY: We only slice the initialized part of the buffer, which is always valid
215224
unsafe {
216-
MaybeUninit::slice_assume_init_mut(&mut self.buf.buf[self.buf.filled..self.buf.init])
225+
let buf = self.buf.buf.get_unchecked_mut(self.buf.filled..self.buf.init);
226+
MaybeUninit::slice_assume_init_mut(buf)
217227
}
218228
}
219229

@@ -222,7 +232,8 @@ impl<'a> BorrowedCursor<'a> {
222232
/// It is safe to uninitialize any of these bytes.
223233
#[inline]
224234
pub fn uninit_mut(&mut self) -> &mut [MaybeUninit<u8>] {
225-
&mut self.buf.buf[self.buf.init..]
235+
// SAFETY: always in bounds
236+
unsafe { self.buf.buf.get_unchecked_mut(self.buf.init..) }
226237
}
227238

228239
/// Returns a mutable reference to the whole cursor.
@@ -232,7 +243,8 @@ impl<'a> BorrowedCursor<'a> {
232243
/// The caller must not uninitialize any bytes in the initialized portion of the cursor.
233244
#[inline]
234245
pub unsafe fn as_mut(&mut self) -> &mut [MaybeUninit<u8>] {
235-
&mut self.buf.buf[self.buf.filled..]
246+
// SAFETY: always in bounds
247+
unsafe { self.buf.buf.get_unchecked_mut(self.buf.filled..) }
236248
}
237249

238250
/// Advance the cursor by asserting that `n` bytes have been filled.

0 commit comments

Comments
 (0)