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_impls" , 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,53 +19,92 @@ where
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_impls" , 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 {
26
29
index. index_mut ( self )
27
30
}
28
31
}
29
32
33
+
30
34
#[ cfg_attr( not( feature = "panic_immediate_abort" ) , inline( never) ) ]
31
35
#[ cfg_attr( feature = "panic_immediate_abort" , inline) ]
32
36
#[ cold]
33
37
#[ track_caller]
34
- fn slice_start_index_len_fail ( index : usize , len : usize ) -> ! {
38
+ #[ rustc_const_unstable( feature = "const_slice_index_impls" , issue = "none" ) ]
39
+ const fn slice_start_index_len_fail ( index : usize , len : usize ) -> ! {
40
+ // SAFETY: we are just panicking here
41
+ unsafe {
42
+ const_eval_select ( ( index, len) , slice_start_index_len_fail_ct, slice_start_index_len_fail_rt)
43
+ }
44
+ }
45
+
46
+ // FIXME const-hack
47
+ fn slice_start_index_len_fail_rt ( index : usize , len : usize ) -> ! {
35
48
panic ! ( "range start index {} out of range for slice of length {}" , index, len) ;
36
49
}
37
50
51
+ const fn slice_start_index_len_fail_ct ( _: usize , _: usize ) -> ! {
52
+ panic ! ( "slice start index is out of range for slice" ) ;
53
+ }
54
+
38
55
#[ cfg_attr( not( feature = "panic_immediate_abort" ) , inline( never) ) ]
39
56
#[ cfg_attr( feature = "panic_immediate_abort" , inline) ]
40
57
#[ cold]
41
58
#[ track_caller]
42
- fn slice_end_index_len_fail ( index : usize , len : usize ) -> ! {
59
+ #[ rustc_const_unstable( feature = "const_slice_index_impls" , issue = "none" ) ]
60
+ const fn slice_end_index_len_fail ( index : usize , len : usize ) -> ! {
61
+ // SAFETY: we are just panicking here
62
+ unsafe {
63
+ const_eval_select ( ( index, len) , slice_end_index_len_fail_ct, slice_end_index_len_fail_rt)
64
+ }
65
+ }
66
+
67
+ // FIXME const-hack
68
+ fn slice_end_index_len_fail_rt ( index : usize , len : usize ) -> ! {
43
69
panic ! ( "range end index {} out of range for slice of length {}" , index, len) ;
44
70
}
45
71
72
+ const fn slice_end_index_len_fail_ct ( _: usize , _: usize ) -> ! {
73
+ panic ! ( "slice end index is out of range for slice" ) ;
74
+ }
75
+
46
76
#[ cfg_attr( not( feature = "panic_immediate_abort" ) , inline( never) ) ]
47
77
#[ cfg_attr( feature = "panic_immediate_abort" , inline) ]
48
78
#[ cold]
49
79
#[ track_caller]
50
- fn slice_index_order_fail ( index : usize , end : usize ) -> ! {
80
+ #[ rustc_const_unstable( feature = "const_slice_index_impls" , issue = "none" ) ]
81
+ const fn slice_index_order_fail ( index : usize , end : usize ) -> ! {
82
+ // SAFETY: we are just panicking here
83
+ unsafe { const_eval_select ( ( index, end) , slice_index_order_fail_ct, slice_index_order_fail_rt) }
84
+ }
85
+
86
+ // FIXME const-hack
87
+ fn slice_index_order_fail_rt ( index : usize , end : usize ) -> ! {
51
88
panic ! ( "slice index starts at {} but ends at {}" , index, end) ;
52
89
}
53
90
91
+ const fn slice_index_order_fail_ct ( _: usize , _: usize ) -> ! {
92
+ panic ! ( "slice index start is larger than end" ) ;
93
+ }
94
+
54
95
#[ cfg_attr( not( feature = "panic_immediate_abort" ) , inline( never) ) ]
55
96
#[ cfg_attr( feature = "panic_immediate_abort" , inline) ]
56
97
#[ cold]
57
98
#[ track_caller]
58
- fn slice_start_index_overflow_fail ( ) -> ! {
99
+ const fn slice_start_index_overflow_fail ( ) -> ! {
59
100
panic ! ( "attempted to index slice from after maximum usize" ) ;
60
101
}
61
102
62
103
#[ cfg_attr( not( feature = "panic_immediate_abort" ) , inline( never) ) ]
63
104
#[ cfg_attr( feature = "panic_immediate_abort" , inline) ]
64
105
#[ cold]
65
106
#[ track_caller]
66
- fn slice_end_index_overflow_fail ( ) -> ! {
107
+ const fn slice_end_index_overflow_fail ( ) -> ! {
67
108
panic ! ( "attempted to index slice up to maximum usize" ) ;
68
109
}
69
110
@@ -153,7 +194,8 @@ pub unsafe trait SliceIndex<T: ?Sized>: private_slice_index::Sealed {
153
194
}
154
195
155
196
#[ stable( feature = "slice_get_slice_impls" , since = "1.15.0" ) ]
156
- unsafe impl < T > SliceIndex < [ T ] > for usize {
197
+ #[ rustc_const_unstable( feature = "const_slice_index_impls" , issue = "none" ) ]
198
+ unsafe impl < T > const SliceIndex < [ T ] > for usize {
157
199
type Output = T ;
158
200
159
201
#[ inline]
@@ -197,7 +239,8 @@ unsafe impl<T> SliceIndex<[T]> for usize {
197
239
}
198
240
199
241
#[ stable( feature = "slice_get_slice_impls" , since = "1.15.0" ) ]
200
- unsafe impl < T > SliceIndex < [ T ] > for ops:: Range < usize > {
242
+ #[ rustc_const_unstable( feature = "const_slice_index_impls" , issue = "none" ) ]
243
+ unsafe impl < T > const SliceIndex < [ T ] > for ops:: Range < usize > {
201
244
type Output = [ T ] ;
202
245
203
246
#[ inline]
@@ -261,7 +304,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
261
304
}
262
305
263
306
#[ stable( feature = "slice_get_slice_impls" , since = "1.15.0" ) ]
264
- unsafe impl < T > SliceIndex < [ T ] > for ops:: RangeTo < usize > {
307
+ #[ rustc_const_unstable( feature = "const_slice_index_impls" , issue = "none" ) ]
308
+ unsafe impl < T > const SliceIndex < [ T ] > for ops:: RangeTo < usize > {
265
309
type Output = [ T ] ;
266
310
267
311
#[ inline]
@@ -298,7 +342,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::RangeTo<usize> {
298
342
}
299
343
300
344
#[ stable( feature = "slice_get_slice_impls" , since = "1.15.0" ) ]
301
- unsafe impl < T > SliceIndex < [ T ] > for ops:: RangeFrom < usize > {
345
+ #[ rustc_const_unstable( feature = "const_slice_index_impls" , issue = "none" ) ]
346
+ unsafe impl < T > const SliceIndex < [ T ] > for ops:: RangeFrom < usize > {
302
347
type Output = [ T ] ;
303
348
304
349
#[ inline]
@@ -343,7 +388,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::RangeFrom<usize> {
343
388
}
344
389
345
390
#[ stable( feature = "slice_get_slice_impls" , since = "1.15.0" ) ]
346
- unsafe impl < T > SliceIndex < [ T ] > for ops:: RangeFull {
391
+ #[ rustc_const_unstable( feature = "const_slice_index_impls" , issue = "none" ) ]
392
+ unsafe impl < T > const SliceIndex < [ T ] > for ops:: RangeFull {
347
393
type Output = [ T ] ;
348
394
349
395
#[ inline]
@@ -378,7 +424,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::RangeFull {
378
424
}
379
425
380
426
#[ stable( feature = "inclusive_range" , since = "1.26.0" ) ]
381
- unsafe impl < T > SliceIndex < [ T ] > for ops:: RangeInclusive < usize > {
427
+ #[ rustc_const_unstable( feature = "const_slice_index_impls" , issue = "none" ) ]
428
+ unsafe impl < T > const SliceIndex < [ T ] > for ops:: RangeInclusive < usize > {
382
429
type Output = [ T ] ;
383
430
384
431
#[ inline]
@@ -421,7 +468,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::RangeInclusive<usize> {
421
468
}
422
469
423
470
#[ stable( feature = "inclusive_range" , since = "1.26.0" ) ]
424
- unsafe impl < T > SliceIndex < [ T ] > for ops:: RangeToInclusive < usize > {
471
+ #[ rustc_const_unstable( feature = "const_slice_index_impls" , issue = "none" ) ]
472
+ unsafe impl < T > const SliceIndex < [ T ] > for ops:: RangeToInclusive < usize > {
425
473
type Output = [ T ] ;
426
474
427
475
#[ inline]
0 commit comments