This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +4
-5
lines changed Expand file tree Collapse file tree 1 file changed +4
-5
lines changed Original file line number Diff line number Diff line change @@ -214,8 +214,7 @@ 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
- // 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 }
219
218
}
220
219
221
220
#[ inline]
@@ -230,14 +229,14 @@ unsafe impl<T> SliceIndex<[T]> for usize {
230
229
// SAFETY: the caller guarantees that `slice` is not dangling, so it
231
230
// cannot be longer than `isize::MAX`. They also guarantee that
232
231
// `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.
235
233
unsafe {
236
234
assert_unsafe_precondition ! (
237
235
"slice::get_unchecked requires that the index is within the slice" ,
238
236
[ T ] ( this: usize , slice: * const [ T ] ) => this < slice. len( )
239
237
) ;
240
- self . get ( & * slice) . unwrap_unchecked ( ) as * const _
238
+ crate :: intrinsics:: assume ( self < slice. len ( ) ) ;
239
+ slice. as_ptr ( ) . add ( self )
241
240
}
242
241
}
243
242
You can’t perform that action at this time.
0 commit comments