1
1
//! Indexing implementations for `[T]`.
2
2
3
+ use crate :: intrinsics:: const_eval_select;
3
4
use crate :: ops;
4
5
use crate :: ptr;
5
6
6
7
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
7
- impl < T , I > ops:: Index < I > for [ T ]
8
+ #[ rustc_const_unstable( feature = "const_slice_index" , issue = "none" ) ]
9
+ impl < T , I > const ops:: Index < I > for [ T ]
8
10
where
9
- I : SliceIndex < [ T ] > ,
11
+ I : ~ const SliceIndex < [ T ] > ,
10
12
{
11
13
type Output = I :: Output ;
12
14
17
19
}
18
20
19
21
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
20
- impl < T , I > ops:: IndexMut < I > for [ T ]
22
+ #[ rustc_const_unstable( feature = "const_slice_index" , issue = "none" ) ]
23
+ impl < T , I > const ops:: IndexMut < I > for [ T ]
21
24
where
22
- I : SliceIndex < [ T ] > ,
25
+ I : ~ const SliceIndex < [ T ] > ,
23
26
{
24
27
#[ inline]
25
28
fn index_mut ( & mut self , index : I ) -> & mut I :: Output {
@@ -31,39 +34,80 @@ where
31
34
#[ cfg_attr( feature = "panic_immediate_abort" , inline) ]
32
35
#[ cold]
33
36
#[ track_caller]
34
- fn slice_start_index_len_fail ( index : usize , len : usize ) -> ! {
37
+ #[ rustc_const_unstable( feature = "const_slice_index" , issue = "none" ) ]
38
+ const fn slice_start_index_len_fail ( index : usize , len : usize ) -> ! {
39
+ // SAFETY: we are just panicking here
40
+ unsafe {
41
+ const_eval_select (
42
+ ( index, len) ,
43
+ slice_start_index_len_fail_ct,
44
+ slice_start_index_len_fail_rt,
45
+ )
46
+ }
47
+ }
48
+
49
+ // FIXME const-hack
50
+ fn slice_start_index_len_fail_rt ( index : usize , len : usize ) -> ! {
35
51
panic ! ( "range start index {} out of range for slice of length {}" , index, len) ;
36
52
}
37
53
54
+ const fn slice_start_index_len_fail_ct ( _: usize , _: usize ) -> ! {
55
+ panic ! ( "slice start index is out of range for slice" ) ;
56
+ }
57
+
38
58
#[ cfg_attr( not( feature = "panic_immediate_abort" ) , inline( never) ) ]
39
59
#[ cfg_attr( feature = "panic_immediate_abort" , inline) ]
40
60
#[ cold]
41
61
#[ track_caller]
42
- fn slice_end_index_len_fail ( index : usize , len : usize ) -> ! {
62
+ #[ rustc_const_unstable( feature = "const_slice_index" , issue = "none" ) ]
63
+ const fn slice_end_index_len_fail ( index : usize , len : usize ) -> ! {
64
+ // SAFETY: we are just panicking here
65
+ unsafe {
66
+ const_eval_select ( ( index, len) , slice_end_index_len_fail_ct, slice_end_index_len_fail_rt)
67
+ }
68
+ }
69
+
70
+ // FIXME const-hack
71
+ fn slice_end_index_len_fail_rt ( index : usize , len : usize ) -> ! {
43
72
panic ! ( "range end index {} out of range for slice of length {}" , index, len) ;
44
73
}
45
74
75
+ const fn slice_end_index_len_fail_ct ( _: usize , _: usize ) -> ! {
76
+ panic ! ( "slice end index is out of range for slice" ) ;
77
+ }
78
+
46
79
#[ cfg_attr( not( feature = "panic_immediate_abort" ) , inline( never) ) ]
47
80
#[ cfg_attr( feature = "panic_immediate_abort" , inline) ]
48
81
#[ cold]
49
82
#[ track_caller]
50
- fn slice_index_order_fail ( index : usize , end : usize ) -> ! {
83
+ #[ rustc_const_unstable( feature = "const_slice_index" , issue = "none" ) ]
84
+ const fn slice_index_order_fail ( index : usize , end : usize ) -> ! {
85
+ // SAFETY: we are just panicking here
86
+ unsafe { const_eval_select ( ( index, end) , slice_index_order_fail_ct, slice_index_order_fail_rt) }
87
+ }
88
+
89
+ // FIXME const-hack
90
+ fn slice_index_order_fail_rt ( index : usize , end : usize ) -> ! {
51
91
panic ! ( "slice index starts at {} but ends at {}" , index, end) ;
52
92
}
53
93
94
+ const fn slice_index_order_fail_ct ( _: usize , _: usize ) -> ! {
95
+ panic ! ( "slice index start is larger than end" ) ;
96
+ }
97
+
54
98
#[ cfg_attr( not( feature = "panic_immediate_abort" ) , inline( never) ) ]
55
99
#[ cfg_attr( feature = "panic_immediate_abort" , inline) ]
56
100
#[ cold]
57
101
#[ track_caller]
58
- fn slice_start_index_overflow_fail ( ) -> ! {
102
+ const fn slice_start_index_overflow_fail ( ) -> ! {
59
103
panic ! ( "attempted to index slice from after maximum usize" ) ;
60
104
}
61
105
62
106
#[ cfg_attr( not( feature = "panic_immediate_abort" ) , inline( never) ) ]
63
107
#[ cfg_attr( feature = "panic_immediate_abort" , inline) ]
64
108
#[ cold]
65
109
#[ track_caller]
66
- fn slice_end_index_overflow_fail ( ) -> ! {
110
+ const fn slice_end_index_overflow_fail ( ) -> ! {
67
111
panic ! ( "attempted to index slice up to maximum usize" ) ;
68
112
}
69
113
@@ -153,7 +197,8 @@ pub unsafe trait SliceIndex<T: ?Sized>: private_slice_index::Sealed {
153
197
}
154
198
155
199
#[ stable( feature = "slice_get_slice_impls" , since = "1.15.0" ) ]
156
- unsafe impl < T > SliceIndex < [ T ] > for usize {
200
+ #[ rustc_const_unstable( feature = "const_slice_index" , issue = "none" ) ]
201
+ unsafe impl < T > const SliceIndex < [ T ] > for usize {
157
202
type Output = T ;
158
203
159
204
#[ inline]
@@ -197,7 +242,8 @@ unsafe impl<T> SliceIndex<[T]> for usize {
197
242
}
198
243
199
244
#[ stable( feature = "slice_get_slice_impls" , since = "1.15.0" ) ]
200
- unsafe impl < T > SliceIndex < [ T ] > for ops:: Range < usize > {
245
+ #[ rustc_const_unstable( feature = "const_slice_index" , issue = "none" ) ]
246
+ unsafe impl < T > const SliceIndex < [ T ] > for ops:: Range < usize > {
201
247
type Output = [ T ] ;
202
248
203
249
#[ inline]
@@ -261,7 +307,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
261
307
}
262
308
263
309
#[ stable( feature = "slice_get_slice_impls" , since = "1.15.0" ) ]
264
- unsafe impl < T > SliceIndex < [ T ] > for ops:: RangeTo < usize > {
310
+ #[ rustc_const_unstable( feature = "const_slice_index" , issue = "none" ) ]
311
+ unsafe impl < T > const SliceIndex < [ T ] > for ops:: RangeTo < usize > {
265
312
type Output = [ T ] ;
266
313
267
314
#[ inline]
@@ -298,7 +345,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::RangeTo<usize> {
298
345
}
299
346
300
347
#[ stable( feature = "slice_get_slice_impls" , since = "1.15.0" ) ]
301
- unsafe impl < T > SliceIndex < [ T ] > for ops:: RangeFrom < usize > {
348
+ #[ rustc_const_unstable( feature = "const_slice_index" , issue = "none" ) ]
349
+ unsafe impl < T > const SliceIndex < [ T ] > for ops:: RangeFrom < usize > {
302
350
type Output = [ T ] ;
303
351
304
352
#[ inline]
@@ -343,7 +391,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::RangeFrom<usize> {
343
391
}
344
392
345
393
#[ stable( feature = "slice_get_slice_impls" , since = "1.15.0" ) ]
346
- unsafe impl < T > SliceIndex < [ T ] > for ops:: RangeFull {
394
+ #[ rustc_const_unstable( feature = "const_slice_index" , issue = "none" ) ]
395
+ unsafe impl < T > const SliceIndex < [ T ] > for ops:: RangeFull {
347
396
type Output = [ T ] ;
348
397
349
398
#[ inline]
@@ -378,7 +427,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::RangeFull {
378
427
}
379
428
380
429
#[ stable( feature = "inclusive_range" , since = "1.26.0" ) ]
381
- unsafe impl < T > SliceIndex < [ T ] > for ops:: RangeInclusive < usize > {
430
+ #[ rustc_const_unstable( feature = "const_slice_index" , issue = "none" ) ]
431
+ unsafe impl < T > const SliceIndex < [ T ] > for ops:: RangeInclusive < usize > {
382
432
type Output = [ T ] ;
383
433
384
434
#[ inline]
@@ -421,7 +471,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::RangeInclusive<usize> {
421
471
}
422
472
423
473
#[ stable( feature = "inclusive_range" , since = "1.26.0" ) ]
424
- unsafe impl < T > SliceIndex < [ T ] > for ops:: RangeToInclusive < usize > {
474
+ #[ rustc_const_unstable( feature = "const_slice_index" , issue = "none" ) ]
475
+ unsafe impl < T > const SliceIndex < [ T ] > for ops:: RangeToInclusive < usize > {
425
476
type Output = [ T ] ;
426
477
427
478
#[ inline]
0 commit comments