Skip to content

Commit ebad345

Browse files
committed
constify Index trait and its slice impls
1 parent cde6468 commit ebad345

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

core/src/array/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,10 @@ impl<'a, T, const N: usize> IntoIterator for &'a mut [T; N] {
374374
}
375375

376376
#[stable(feature = "index_trait_on_arrays", since = "1.50.0")]
377-
impl<T, I, const N: usize> Index<I> for [T; N]
377+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
378+
impl<T, I, const N: usize> const Index<I> for [T; N]
378379
where
379-
[T]: Index<I>,
380+
[T]: ~const Index<I>,
380381
{
381382
type Output = <[T] as Index<I>>::Output;
382383

@@ -387,9 +388,10 @@ where
387388
}
388389

389390
#[stable(feature = "index_trait_on_arrays", since = "1.50.0")]
390-
impl<T, I, const N: usize> IndexMut<I> for [T; N]
391+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
392+
impl<T, I, const N: usize> const IndexMut<I> for [T; N]
391393
where
392-
[T]: IndexMut<I>,
394+
[T]: ~const IndexMut<I>,
393395
{
394396
#[inline]
395397
fn index_mut(&mut self, index: I) -> &mut Self::Output {

core/src/ops/index.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
#[doc(alias = "]")]
5656
#[doc(alias = "[")]
5757
#[doc(alias = "[]")]
58+
#[const_trait]
59+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
5860
pub trait Index<Idx: ?Sized> {
5961
/// The returned type after indexing.
6062
#[stable(feature = "rust1", since = "1.0.0")]
@@ -165,7 +167,9 @@ see chapter in The Book <https://doc.rust-lang.org/book/ch08-02-strings.html#ind
165167
#[doc(alias = "[")]
166168
#[doc(alias = "]")]
167169
#[doc(alias = "[]")]
168-
pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
170+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
171+
#[const_trait]
172+
pub trait IndexMut<Idx: ?Sized>: ~const Index<Idx> {
169173
/// Performs the mutable indexing (`container[index]`) operation.
170174
///
171175
/// # Panics

core/src/slice/index.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ use crate::ub_checks::assert_unsafe_precondition;
66
use crate::{ops, range};
77

88
#[stable(feature = "rust1", since = "1.0.0")]
9-
impl<T, I> ops::Index<I> for [T]
9+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
10+
impl<T, I> const ops::Index<I> for [T]
1011
where
11-
I: SliceIndex<[T]>,
12+
I: ~const SliceIndex<[T]>,
1213
{
1314
type Output = I::Output;
1415

@@ -19,9 +20,10 @@ where
1920
}
2021

2122
#[stable(feature = "rust1", since = "1.0.0")]
22-
impl<T, I> ops::IndexMut<I> for [T]
23+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
24+
impl<T, I> const ops::IndexMut<I> for [T]
2325
where
24-
I: SliceIndex<[T]>,
26+
I: ~const SliceIndex<[T]>,
2527
{
2628
#[inline(always)]
2729
fn index_mut(&mut self, index: I) -> &mut I::Output {

core/src/str/traits.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ impl PartialOrd for str {
4949
}
5050

5151
#[stable(feature = "rust1", since = "1.0.0")]
52-
impl<I> ops::Index<I> for str
52+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
53+
impl<I> const ops::Index<I> for str
5354
where
54-
I: SliceIndex<str>,
55+
I: ~const SliceIndex<str>,
5556
{
5657
type Output = I::Output;
5758

@@ -62,9 +63,10 @@ where
6263
}
6364

6465
#[stable(feature = "rust1", since = "1.0.0")]
65-
impl<I> ops::IndexMut<I> for str
66+
#[rustc_const_unstable(feature = "const_index", issue = "143775")]
67+
impl<I> const ops::IndexMut<I> for str
6668
where
67-
I: SliceIndex<str>,
69+
I: ~const SliceIndex<str>,
6870
{
6971
#[inline]
7072
fn index_mut(&mut self, index: I) -> &mut I::Output {

0 commit comments

Comments
 (0)