11//! Indexing implementations for `[T]`.
22
3+ use crate :: intrinsics:: const_eval_select;
34use crate :: ops;
45use crate :: ptr;
56
67#[ 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 ]
810where
9- I : SliceIndex < [ T ] > ,
11+ I : ~ const SliceIndex < [ T ] > ,
1012{
1113 type Output = I :: Output ;
1214
1719}
1820
1921#[ 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 ]
2124where
22- I : SliceIndex < [ T ] > ,
25+ I : ~ const SliceIndex < [ T ] > ,
2326{
2427 #[ inline]
2528 fn index_mut ( & mut self , index : I ) -> & mut I :: Output {
@@ -31,39 +34,80 @@ where
3134#[ cfg_attr( feature = "panic_immediate_abort" , inline) ]
3235#[ cold]
3336#[ 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 ) -> ! {
3551 panic ! ( "range start index {} out of range for slice of length {}" , index, len) ;
3652}
3753
54+ const fn slice_start_index_len_fail_ct ( _: usize , _: usize ) -> ! {
55+ panic ! ( "slice start index is out of range for slice" ) ;
56+ }
57+
3858#[ cfg_attr( not( feature = "panic_immediate_abort" ) , inline( never) ) ]
3959#[ cfg_attr( feature = "panic_immediate_abort" , inline) ]
4060#[ cold]
4161#[ 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 ) -> ! {
4372 panic ! ( "range end index {} out of range for slice of length {}" , index, len) ;
4473}
4574
75+ const fn slice_end_index_len_fail_ct ( _: usize , _: usize ) -> ! {
76+ panic ! ( "slice end index is out of range for slice" ) ;
77+ }
78+
4679#[ cfg_attr( not( feature = "panic_immediate_abort" ) , inline( never) ) ]
4780#[ cfg_attr( feature = "panic_immediate_abort" , inline) ]
4881#[ cold]
4982#[ 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 ) -> ! {
5191 panic ! ( "slice index starts at {} but ends at {}" , index, end) ;
5292}
5393
94+ const fn slice_index_order_fail_ct ( _: usize , _: usize ) -> ! {
95+ panic ! ( "slice index start is larger than end" ) ;
96+ }
97+
5498#[ cfg_attr( not( feature = "panic_immediate_abort" ) , inline( never) ) ]
5599#[ cfg_attr( feature = "panic_immediate_abort" , inline) ]
56100#[ cold]
57101#[ track_caller]
58- fn slice_start_index_overflow_fail ( ) -> ! {
102+ const fn slice_start_index_overflow_fail ( ) -> ! {
59103 panic ! ( "attempted to index slice from after maximum usize" ) ;
60104}
61105
62106#[ cfg_attr( not( feature = "panic_immediate_abort" ) , inline( never) ) ]
63107#[ cfg_attr( feature = "panic_immediate_abort" , inline) ]
64108#[ cold]
65109#[ track_caller]
66- fn slice_end_index_overflow_fail ( ) -> ! {
110+ const fn slice_end_index_overflow_fail ( ) -> ! {
67111 panic ! ( "attempted to index slice up to maximum usize" ) ;
68112}
69113
@@ -153,7 +197,8 @@ pub unsafe trait SliceIndex<T: ?Sized>: private_slice_index::Sealed {
153197}
154198
155199#[ 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 {
157202 type Output = T ;
158203
159204 #[ inline]
@@ -197,7 +242,8 @@ unsafe impl<T> SliceIndex<[T]> for usize {
197242}
198243
199244#[ 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 > {
201247 type Output = [ T ] ;
202248
203249 #[ inline]
@@ -261,7 +307,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
261307}
262308
263309#[ 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 > {
265312 type Output = [ T ] ;
266313
267314 #[ inline]
@@ -298,7 +345,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::RangeTo<usize> {
298345}
299346
300347#[ 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 > {
302350 type Output = [ T ] ;
303351
304352 #[ inline]
@@ -343,7 +391,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::RangeFrom<usize> {
343391}
344392
345393#[ 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 {
347396 type Output = [ T ] ;
348397
349398 #[ inline]
@@ -378,7 +427,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::RangeFull {
378427}
379428
380429#[ 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 > {
382432 type Output = [ T ] ;
383433
384434 #[ inline]
@@ -421,7 +471,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::RangeInclusive<usize> {
421471}
422472
423473#[ 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 > {
425476 type Output = [ T ] ;
426477
427478 #[ inline]
0 commit comments