1
1
//! Indexing implementations for `[T]`.
2
2
3
+ #[ cfg( not( bootstrap) ) ]
4
+ use crate :: intrinsics:: slice_get_unchecked;
3
5
use crate :: panic:: const_panic;
4
6
use crate :: ub_checks:: assert_unsafe_precondition;
5
7
use crate :: { ops, range} ;
@@ -83,13 +85,15 @@ const fn slice_end_index_overflow_fail() -> ! {
83
85
// Both the safe and unsafe public methods share these helpers,
84
86
// which use intrinsics directly to get *no* extra checks.
85
87
88
+ #[ cfg( bootstrap) ]
86
89
#[ inline( always) ]
87
90
const unsafe fn get_noubcheck < T > ( ptr : * const [ T ] , index : usize ) -> * const T {
88
91
let ptr = ptr as * const T ;
89
92
// SAFETY: The caller already checked these preconditions
90
93
unsafe { crate :: intrinsics:: offset ( ptr, index) }
91
94
}
92
95
96
+ #[ cfg( bootstrap) ]
93
97
#[ inline( always) ]
94
98
const unsafe fn get_mut_noubcheck < T > ( ptr : * mut [ T ] , index : usize ) -> * mut T {
95
99
let ptr = ptr as * mut T ;
@@ -103,8 +107,9 @@ const unsafe fn get_offset_len_noubcheck<T>(
103
107
offset : usize ,
104
108
len : usize ,
105
109
) -> * const [ T ] {
110
+ let ptr = ptr as * const T ;
106
111
// SAFETY: The caller already checked these preconditions
107
- let ptr = unsafe { get_noubcheck ( ptr, offset) } ;
112
+ let ptr = unsafe { crate :: intrinsics :: offset ( ptr, offset) } ;
108
113
crate :: intrinsics:: aggregate_raw_ptr ( ptr, len)
109
114
}
110
115
@@ -114,8 +119,9 @@ const unsafe fn get_offset_len_mut_noubcheck<T>(
114
119
offset : usize ,
115
120
len : usize ,
116
121
) -> * mut [ T ] {
122
+ let ptr = ptr as * mut T ;
117
123
// SAFETY: The caller already checked these preconditions
118
- let ptr = unsafe { get_mut_noubcheck ( ptr, offset) } ;
124
+ let ptr = unsafe { crate :: intrinsics :: offset ( ptr, offset) } ;
119
125
crate :: intrinsics:: aggregate_raw_ptr ( ptr, len)
120
126
}
121
127
@@ -224,15 +230,35 @@ unsafe impl<T> SliceIndex<[T]> for usize {
224
230
225
231
#[ inline]
226
232
fn get ( self , slice : & [ T ] ) -> Option < & T > {
227
- // SAFETY: `self` is checked to be in bounds.
228
- if self < slice. len ( ) { unsafe { Some ( & * get_noubcheck ( slice, self ) ) } } else { None }
233
+ if self < slice. len ( ) {
234
+ #[ cfg( bootstrap) ]
235
+ // SAFETY: `self` is checked to be in bounds.
236
+ unsafe {
237
+ Some ( & * get_noubcheck ( slice, self ) )
238
+ }
239
+ #[ cfg( not( bootstrap) ) ]
240
+ // SAFETY: `self` is checked to be in bounds.
241
+ unsafe {
242
+ Some ( slice_get_unchecked ( slice, self ) )
243
+ }
244
+ } else {
245
+ None
246
+ }
229
247
}
230
248
231
249
#[ inline]
232
250
fn get_mut ( self , slice : & mut [ T ] ) -> Option < & mut T > {
233
251
if self < slice. len ( ) {
252
+ #[ cfg( bootstrap) ]
253
+ // SAFETY: `self` is checked to be in bounds.
254
+ unsafe {
255
+ Some ( & mut * get_mut_noubcheck ( slice, self ) )
256
+ }
257
+ #[ cfg( not( bootstrap) ) ]
234
258
// SAFETY: `self` is checked to be in bounds.
235
- unsafe { Some ( & mut * get_mut_noubcheck ( slice, self ) ) }
259
+ unsafe {
260
+ Some ( slice_get_unchecked ( slice, self ) )
261
+ }
236
262
} else {
237
263
None
238
264
}
@@ -254,7 +280,14 @@ unsafe impl<T> SliceIndex<[T]> for usize {
254
280
// Use intrinsics::assume instead of hint::assert_unchecked so that we don't check the
255
281
// precondition of this function twice.
256
282
crate :: intrinsics:: assume ( self < slice. len ( ) ) ;
257
- get_noubcheck ( slice, self )
283
+ #[ cfg( bootstrap) ]
284
+ {
285
+ get_noubcheck ( slice, self )
286
+ }
287
+ #[ cfg( not( bootstrap) ) ]
288
+ {
289
+ slice_get_unchecked ( slice, self )
290
+ }
258
291
}
259
292
}
260
293
@@ -267,7 +300,16 @@ unsafe impl<T> SliceIndex<[T]> for usize {
267
300
( this: usize = self , len: usize = slice. len( ) ) => this < len
268
301
) ;
269
302
// SAFETY: see comments for `get_unchecked` above.
270
- unsafe { get_mut_noubcheck ( slice, self ) }
303
+ unsafe {
304
+ #[ cfg( bootstrap) ]
305
+ {
306
+ get_mut_noubcheck ( slice, self )
307
+ }
308
+ #[ cfg( not( bootstrap) ) ]
309
+ {
310
+ slice_get_unchecked ( slice, self )
311
+ }
312
+ }
271
313
}
272
314
273
315
#[ inline]
0 commit comments