Skip to content

Commit b5230e5

Browse files
committed
constify SliceIndex trait
1 parent 7f2065a commit b5230e5

File tree

3 files changed

+53
-25
lines changed

3 files changed

+53
-25
lines changed

library/core/src/range.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,17 @@ impl<T> IntoBounds<T> for Range<T> {
186186
}
187187

188188
#[unstable(feature = "new_range_api", issue = "125687")]
189-
impl<T> From<Range<T>> for legacy::Range<T> {
189+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
190+
impl<T> const From<Range<T>> for legacy::Range<T> {
190191
#[inline]
191192
fn from(value: Range<T>) -> Self {
192193
Self { start: value.start, end: value.end }
193194
}
194195
}
196+
195197
#[unstable(feature = "new_range_api", issue = "125687")]
196-
impl<T> From<legacy::Range<T>> for Range<T> {
198+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
199+
impl<T> const From<legacy::Range<T>> for Range<T> {
197200
#[inline]
198201
fn from(value: legacy::Range<T>) -> Self {
199202
Self { start: value.start, end: value.end }
@@ -362,7 +365,8 @@ impl<T> IntoBounds<T> for RangeInclusive<T> {
362365
}
363366

364367
#[unstable(feature = "new_range_api", issue = "125687")]
365-
impl<T> From<RangeInclusive<T>> for legacy::RangeInclusive<T> {
368+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
369+
impl<T> const From<RangeInclusive<T>> for legacy::RangeInclusive<T> {
366370
#[inline]
367371
fn from(value: RangeInclusive<T>) -> Self {
368372
Self::new(value.start, value.end)
@@ -506,14 +510,16 @@ impl<T> IntoBounds<T> for RangeFrom<T> {
506510
}
507511

508512
#[unstable(feature = "new_range_api", issue = "125687")]
509-
impl<T> From<RangeFrom<T>> for legacy::RangeFrom<T> {
513+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
514+
impl<T> const From<RangeFrom<T>> for legacy::RangeFrom<T> {
510515
#[inline]
511516
fn from(value: RangeFrom<T>) -> Self {
512517
Self { start: value.start }
513518
}
514519
}
515520
#[unstable(feature = "new_range_api", issue = "125687")]
516-
impl<T> From<legacy::RangeFrom<T>> for RangeFrom<T> {
521+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
522+
impl<T> const From<legacy::RangeFrom<T>> for RangeFrom<T> {
517523
#[inline]
518524
fn from(value: legacy::RangeFrom<T>) -> Self {
519525
Self { start: value.start }

library/core/src/slice/index.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ mod private_slice_index {
158158
message = "the type `{T}` cannot be indexed by `{Self}`",
159159
label = "slice indices are of type `usize` or ranges of `usize`"
160160
)]
161+
#[const_trait]
162+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
161163
pub unsafe trait SliceIndex<T: ?Sized>: private_slice_index::Sealed {
162164
/// The output type returned by methods.
163165
#[stable(feature = "slice_get_slice", since = "1.28.0")]
@@ -208,7 +210,8 @@ pub unsafe trait SliceIndex<T: ?Sized>: private_slice_index::Sealed {
208210

209211
/// The methods `index` and `index_mut` panic if the index is out of bounds.
210212
#[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
211-
unsafe impl<T> SliceIndex<[T]> for usize {
213+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
214+
unsafe impl<T> const SliceIndex<[T]> for usize {
212215
type Output = T;
213216

214217
#[inline]
@@ -278,7 +281,8 @@ unsafe impl<T> SliceIndex<[T]> for usize {
278281

279282
/// Because `IndexRange` guarantees `start <= end`, fewer checks are needed here
280283
/// than there are for a general `Range<usize>` (which might be `100..3`).
281-
unsafe impl<T> SliceIndex<[T]> for ops::IndexRange {
284+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
285+
unsafe impl<T> const SliceIndex<[T]> for ops::IndexRange {
282286
type Output = [T];
283287

284288
#[inline]
@@ -354,7 +358,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::IndexRange {
354358
/// - the start of the range is greater than the end of the range or
355359
/// - the end of the range is out of bounds.
356360
#[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
357-
unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
361+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
362+
unsafe impl<T> const SliceIndex<[T]> for ops::Range<usize> {
358363
type Output = [T];
359364

360365
#[inline]
@@ -453,7 +458,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::Range<usize> {
453458
}
454459

455460
#[unstable(feature = "new_range_api", issue = "125687")]
456-
unsafe impl<T> SliceIndex<[T]> for range::Range<usize> {
461+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
462+
unsafe impl<T> const SliceIndex<[T]> for range::Range<usize> {
457463
type Output = [T];
458464

459465
#[inline]
@@ -491,7 +497,8 @@ unsafe impl<T> SliceIndex<[T]> for range::Range<usize> {
491497

492498
/// The methods `index` and `index_mut` panic if the end of the range is out of bounds.
493499
#[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
494-
unsafe impl<T> SliceIndex<[T]> for ops::RangeTo<usize> {
500+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
501+
unsafe impl<T> const SliceIndex<[T]> for ops::RangeTo<usize> {
495502
type Output = [T];
496503

497504
#[inline]
@@ -529,7 +536,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::RangeTo<usize> {
529536

530537
/// The methods `index` and `index_mut` panic if the start of the range is out of bounds.
531538
#[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
532-
unsafe impl<T> SliceIndex<[T]> for ops::RangeFrom<usize> {
539+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
540+
unsafe impl<T> const SliceIndex<[T]> for ops::RangeFrom<usize> {
533541
type Output = [T];
534542

535543
#[inline]
@@ -574,7 +582,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::RangeFrom<usize> {
574582
}
575583

576584
#[unstable(feature = "new_range_api", issue = "125687")]
577-
unsafe impl<T> SliceIndex<[T]> for range::RangeFrom<usize> {
585+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
586+
unsafe impl<T> const SliceIndex<[T]> for range::RangeFrom<usize> {
578587
type Output = [T];
579588

580589
#[inline]
@@ -611,7 +620,8 @@ unsafe impl<T> SliceIndex<[T]> for range::RangeFrom<usize> {
611620
}
612621

613622
#[stable(feature = "slice_get_slice_impls", since = "1.15.0")]
614-
unsafe impl<T> SliceIndex<[T]> for ops::RangeFull {
623+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
624+
unsafe impl<T> const SliceIndex<[T]> for ops::RangeFull {
615625
type Output = [T];
616626

617627
#[inline]
@@ -650,7 +660,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::RangeFull {
650660
/// - the start of the range is greater than the end of the range or
651661
/// - the end of the range is out of bounds.
652662
#[stable(feature = "inclusive_range", since = "1.26.0")]
653-
unsafe impl<T> SliceIndex<[T]> for ops::RangeInclusive<usize> {
663+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
664+
unsafe impl<T> const SliceIndex<[T]> for ops::RangeInclusive<usize> {
654665
type Output = [T];
655666

656667
#[inline]
@@ -693,7 +704,8 @@ unsafe impl<T> SliceIndex<[T]> for ops::RangeInclusive<usize> {
693704
}
694705

695706
#[unstable(feature = "new_range_api", issue = "125687")]
696-
unsafe impl<T> SliceIndex<[T]> for range::RangeInclusive<usize> {
707+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
708+
unsafe impl<T> const SliceIndex<[T]> for range::RangeInclusive<usize> {
697709
type Output = [T];
698710

699711
#[inline]
@@ -731,7 +743,8 @@ unsafe impl<T> SliceIndex<[T]> for range::RangeInclusive<usize> {
731743

732744
/// The methods `index` and `index_mut` panic if the end of the range is out of bounds.
733745
#[stable(feature = "inclusive_range", since = "1.26.0")]
734-
unsafe impl<T> SliceIndex<[T]> for ops::RangeToInclusive<usize> {
746+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
747+
unsafe impl<T> const SliceIndex<[T]> for ops::RangeToInclusive<usize> {
735748
type Output = [T];
736749

737750
#[inline]

library/core/src/str/traits.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ const fn str_index_overflow_fail() -> ! {
9292
///
9393
/// Equivalent to `&self[0 .. len]` or `&mut self[0 .. len]`.
9494
#[stable(feature = "str_checked_slicing", since = "1.20.0")]
95-
unsafe impl SliceIndex<str> for ops::RangeFull {
95+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
96+
unsafe impl const SliceIndex<str> for ops::RangeFull {
9697
type Output = str;
9798
#[inline]
9899
fn get(self, slice: &str) -> Option<&Self::Output> {
@@ -156,7 +157,8 @@ unsafe impl SliceIndex<str> for ops::RangeFull {
156157
/// // &s[3 .. 100];
157158
/// ```
158159
#[stable(feature = "str_checked_slicing", since = "1.20.0")]
159-
unsafe impl SliceIndex<str> for ops::Range<usize> {
160+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
161+
unsafe impl const SliceIndex<str> for ops::Range<usize> {
160162
type Output = str;
161163
#[inline]
162164
fn get(self, slice: &str) -> Option<&Self::Output> {
@@ -260,7 +262,8 @@ unsafe impl SliceIndex<str> for ops::Range<usize> {
260262
}
261263

262264
#[unstable(feature = "new_range_api", issue = "125687")]
263-
unsafe impl SliceIndex<str> for range::Range<usize> {
265+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
266+
unsafe impl const SliceIndex<str> for range::Range<usize> {
264267
type Output = str;
265268
#[inline]
266269
fn get(self, slice: &str) -> Option<&Self::Output> {
@@ -431,7 +434,8 @@ unsafe impl SliceIndex<str> for (ops::Bound<usize>, ops::Bound<usize>) {
431434
/// Panics if `end` does not point to the starting byte offset of a
432435
/// character (as defined by `is_char_boundary`), or if `end > len`.
433436
#[stable(feature = "str_checked_slicing", since = "1.20.0")]
434-
unsafe impl SliceIndex<str> for ops::RangeTo<usize> {
437+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
438+
unsafe impl const SliceIndex<str> for ops::RangeTo<usize> {
435439
type Output = str;
436440
#[inline]
437441
fn get(self, slice: &str) -> Option<&Self::Output> {
@@ -499,7 +503,8 @@ unsafe impl SliceIndex<str> for ops::RangeTo<usize> {
499503
/// Panics if `begin` does not point to the starting byte offset of
500504
/// a character (as defined by `is_char_boundary`), or if `begin > len`.
501505
#[stable(feature = "str_checked_slicing", since = "1.20.0")]
502-
unsafe impl SliceIndex<str> for ops::RangeFrom<usize> {
506+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
507+
unsafe impl const SliceIndex<str> for ops::RangeFrom<usize> {
503508
type Output = str;
504509
#[inline]
505510
fn get(self, slice: &str) -> Option<&Self::Output> {
@@ -554,7 +559,8 @@ unsafe impl SliceIndex<str> for ops::RangeFrom<usize> {
554559
}
555560

556561
#[unstable(feature = "new_range_api", issue = "125687")]
557-
unsafe impl SliceIndex<str> for range::RangeFrom<usize> {
562+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
563+
unsafe impl const SliceIndex<str> for range::RangeFrom<usize> {
558564
type Output = str;
559565
#[inline]
560566
fn get(self, slice: &str) -> Option<&Self::Output> {
@@ -625,7 +631,8 @@ unsafe impl SliceIndex<str> for range::RangeFrom<usize> {
625631
/// to the ending byte offset of a character (`end + 1` is either a starting
626632
/// byte offset or equal to `len`), if `begin > end`, or if `end >= len`.
627633
#[stable(feature = "inclusive_range", since = "1.26.0")]
628-
unsafe impl SliceIndex<str> for ops::RangeInclusive<usize> {
634+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
635+
unsafe impl const SliceIndex<str> for ops::RangeInclusive<usize> {
629636
type Output = str;
630637
#[inline]
631638
fn get(self, slice: &str) -> Option<&Self::Output> {
@@ -662,7 +669,8 @@ unsafe impl SliceIndex<str> for ops::RangeInclusive<usize> {
662669
}
663670

664671
#[unstable(feature = "new_range_api", issue = "125687")]
665-
unsafe impl SliceIndex<str> for range::RangeInclusive<usize> {
672+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
673+
unsafe impl const SliceIndex<str> for range::RangeInclusive<usize> {
666674
type Output = str;
667675
#[inline]
668676
fn get(self, slice: &str) -> Option<&Self::Output> {
@@ -713,7 +721,8 @@ unsafe impl SliceIndex<str> for range::RangeInclusive<usize> {
713721
/// (`end + 1` is either a starting byte offset as defined by
714722
/// `is_char_boundary`, or equal to `len`), or if `end >= len`.
715723
#[stable(feature = "inclusive_range", since = "1.26.0")]
716-
unsafe impl SliceIndex<str> for ops::RangeToInclusive<usize> {
724+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
725+
unsafe impl const SliceIndex<str> for ops::RangeToInclusive<usize> {
717726
type Output = str;
718727
#[inline]
719728
fn get(self, slice: &str) -> Option<&Self::Output> {

0 commit comments

Comments
 (0)