Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 63e3e48

Browse files
committed
assume
1 parent 2073441 commit 63e3e48

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

library/core/src/slice/index.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,7 @@ unsafe impl<T> SliceIndex<[T]> for usize {
214214
#[inline]
215215
fn get(self, slice: &[T]) -> Option<&T> {
216216
// SAFETY: `self` is checked to be in bounds.
217-
// The call to `add` is safe, as we check the bounds.
218-
if self < slice.len() { unsafe { Some(&*slice.as_ptr().add(self)) } } else { None }
217+
if self < slice.len() { unsafe { Some(&*self.get_unchecked(slice)) } } else { None }
219218
}
220219

221220
#[inline]
@@ -230,14 +229,14 @@ unsafe impl<T> SliceIndex<[T]> for usize {
230229
// SAFETY: the caller guarantees that `slice` is not dangling, so it
231230
// cannot be longer than `isize::MAX`. They also guarantee that
232231
// `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
233-
// so we can `unwrap_unchecked`.
234-
// We use `get().unwrap_unchecked()` for better codegen: see issue #116878
232+
// so the call to `add` is safe.
235233
unsafe {
236234
assert_unsafe_precondition!(
237235
"slice::get_unchecked requires that the index is within the slice",
238236
[T](this: usize, slice: *const [T]) => this < slice.len()
239237
);
240-
self.get(&*slice).unwrap_unchecked() as *const _
238+
crate::intrinsics::assume(self < slice.len());
239+
slice.as_ptr().add(self)
241240
}
242241
}
243242

0 commit comments

Comments
 (0)