13
13
{
14
14
type Output = I :: Output ;
15
15
16
- #[ inline]
16
+ #[ inline( always ) ]
17
17
fn index ( & self , index : I ) -> & I :: Output {
18
18
index. index ( self )
19
19
}
@@ -24,7 +24,7 @@ impl<T, I> ops::IndexMut<I> for [T]
24
24
where
25
25
I : SliceIndex < [ T ] > ,
26
26
{
27
- #[ inline]
27
+ #[ inline( always ) ]
28
28
fn index_mut ( & mut self , index : I ) -> & mut I :: Output {
29
29
index. index_mut ( self )
30
30
}
@@ -227,14 +227,16 @@ unsafe impl<T> SliceIndex<[T]> for usize {
227
227
unsafe fn get_unchecked ( self , slice : * const [ T ] ) -> * const T {
228
228
debug_assert_nounwind ! (
229
229
self < slice. len( ) ,
230
- "slice::get_unchecked requires that the index is within the slice" ,
230
+ "slice::get_unchecked requires that the index is within the slice"
231
231
) ;
232
232
// SAFETY: the caller guarantees that `slice` is not dangling, so it
233
233
// cannot be longer than `isize::MAX`. They also guarantee that
234
234
// `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
235
235
// so the call to `add` is safe.
236
236
unsafe {
237
- crate :: hint:: assert_unchecked ( self < slice. len ( ) ) ;
237
+ // Use intrinsics::assume instead of hint::assert_unchecked so that we don't check the
238
+ // precondition of this function twice.
239
+ crate :: intrinsics:: assume ( self < slice. len ( ) ) ;
238
240
slice. as_ptr ( ) . add ( self )
239
241
}
240
242
}
@@ -243,7 +245,7 @@ unsafe impl<T> SliceIndex<[T]> for usize {
243
245
unsafe fn get_unchecked_mut ( self , slice : * mut [ T ] ) -> * mut T {
244
246
debug_assert_nounwind ! (
245
247
self < slice. len( ) ,
246
- "slice::get_unchecked_mut requires that the index is within the slice" ,
248
+ "slice::get_unchecked_mut requires that the index is within the slice"
247
249
) ;
248
250
// SAFETY: see comments for `get_unchecked` above.
249
251
unsafe { slice. as_mut_ptr ( ) . add ( self ) }
@@ -305,8 +307,9 @@ unsafe impl<T> SliceIndex<[T]> for ops::IndexRange {
305
307
unsafe fn get_unchecked_mut ( self , slice : * mut [ T ] ) -> * mut [ T ] {
306
308
debug_assert_nounwind ! (
307
309
self . end( ) <= slice. len( ) ,
308
- "slice::get_unchecked_mut requires that the index is within the slice" ,
310
+ "slice::get_unchecked_mut requires that the index is within the slice"
309
311
) ;
312
+
310
313
// SAFETY: see comments for `get_unchecked` above.
311
314
unsafe { ptr:: slice_from_raw_parts_mut ( slice. as_mut_ptr ( ) . add ( self . start ( ) ) , self . len ( ) ) }
312
315
}
@@ -361,8 +364,9 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
361
364
unsafe fn get_unchecked ( self , slice : * const [ T ] ) -> * const [ T ] {
362
365
debug_assert_nounwind ! (
363
366
self . end >= self . start && self . end <= slice. len( ) ,
364
- "slice::get_unchecked requires that the range is within the slice" ,
367
+ "slice::get_unchecked requires that the range is within the slice"
365
368
) ;
369
+
366
370
// SAFETY: the caller guarantees that `slice` is not dangling, so it
367
371
// cannot be longer than `isize::MAX`. They also guarantee that
368
372
// `self` is in bounds of `slice` so `self` cannot overflow an `isize`,
@@ -377,7 +381,7 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
377
381
unsafe fn get_unchecked_mut ( self , slice : * mut [ T ] ) -> * mut [ T ] {
378
382
debug_assert_nounwind ! (
379
383
self . end >= self . start && self . end <= slice. len( ) ,
380
- "slice::get_unchecked_mut requires that the range is within the slice" ,
384
+ "slice::get_unchecked_mut requires that the range is within the slice"
381
385
) ;
382
386
// SAFETY: see comments for `get_unchecked` above.
383
387
unsafe {
0 commit comments