Skip to content

Commit

Permalink
Don't have Vec<T> delegate to [T]'s bounds for indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
fintelia committed Mar 3, 2018
1 parent ae73a21 commit 370df40
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
18 changes: 11 additions & 7 deletions src/liballoc/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1527,23 +1527,27 @@ impl<T: Hash> Hash for Vec<T> {

#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_on_unimplemented = "vector indices are of type `usize` or ranges of `usize`"]
impl<T, I> Index<I> for Vec<T> where [T]: Index<I> {
type Output = <[T] as Index<I>>::Output;
impl<T, I> Index<I> for Vec<T>
where
I: ::core::slice::SliceIndex<[T]>,
{
type Output = I::Output;

#[inline]
fn index(&self, index: I) -> &Self::Output {
// NB indexing via implementation on slice
&(**self)[index]
Index::index(&**self, index)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_on_unimplemented = "vector indices are of type `usize` or ranges of `usize`"]
impl<T, I> IndexMut<I> for Vec<T> where [T]: IndexMut<I> {
impl<T, I> IndexMut<I> for Vec<T>
where
I: ::core::slice::SliceIndex<[T]>,
{
#[inline]
fn index_mut(&mut self, index: I) -> &mut Self::Output {
// NB indexing via implementation on slice
&mut (**self)[index]
IndexMut::index_mut(&mut **self, index)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/index-help.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | x[0i32]; //~ ERROR E0277
| ^^^^^^^ slice indices are of type `usize` or ranges of `usize`
|
= help: the trait `std::slice::SliceIndex<[{integer}]>` is not implemented for `i32`
= note: required because of the requirements on the impl of `std::ops::Index<i32>` for `[{integer}]`
= note: required because of the requirements on the impl of `std::ops::Index<i32>` for `std::vec::Vec<{integer}>`

error: aborting due to previous error

Expand Down

0 comments on commit 370df40

Please sign in to comment.