This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +6
-4
lines changed Expand file tree Collapse file tree 2 files changed +6
-4
lines changed Original file line number Diff line number Diff line change @@ -214,7 +214,8 @@ unsafe impl<T> SliceIndex<[T]> for usize {
214
214
#[ inline]
215
215
fn get ( self , slice : & [ T ] ) -> Option < & T > {
216
216
// SAFETY: `self` is checked to be in bounds.
217
- if self < slice. len ( ) { unsafe { Some ( & * self . get_unchecked ( slice) ) } } else { None }
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 }
218
219
}
219
220
220
221
#[ inline]
@@ -229,13 +230,14 @@ unsafe impl<T> SliceIndex<[T]> for usize {
229
230
// SAFETY: the caller guarantees that `slice` is not dangling, so it
230
231
// cannot be longer than `isize::MAX`. They also guarantee that
231
232
// `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
232
- // so the call to `add` is safe.
233
+ // so we can `unwrap_unchecked`.
234
+ // We use `get().unwrap_unchecked()` for better codegen: see issue #116878
233
235
unsafe {
234
236
assert_unsafe_precondition ! (
235
237
"slice::get_unchecked requires that the index is within the slice" ,
236
238
[ T ] ( this: usize , slice: * const [ T ] ) => this < slice. len( )
237
239
) ;
238
- slice . as_ptr ( ) . add ( self )
240
+ self . get ( & * slice ) . unwrap_unchecked ( ) as * const _
239
241
}
240
242
}
241
243
Original file line number Diff line number Diff line change @@ -662,7 +662,7 @@ impl<T> [T] {
662
662
// SAFETY: the caller must uphold most of the safety requirements for `get_unchecked`;
663
663
// the slice is dereferenceable because `self` is a safe reference.
664
664
// The returned pointer is safe because impls of `SliceIndex` have to guarantee that it is.
665
- unsafe { & * index. get ( self ) . unwrap_unchecked ( ) }
665
+ unsafe { & * index. get_unchecked ( self ) }
666
666
}
667
667
668
668
/// Returns a mutable reference to an element or subslice, without doing
You can’t perform that action at this time.
0 commit comments