Skip to content

Implement RFC 1679 #36340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 27, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libcollections/lib.rs
Original file line number Diff line number Diff line change
@@ -54,6 +54,7 @@
#![feature(trusted_len)]
#![feature(unicode)]
#![feature(unique)]
#![feature(slice_get_slice)]
#![cfg_attr(test, feature(rand, test))]

#![no_std]
18 changes: 14 additions & 4 deletions src/libcollections/slice.rs
Original file line number Diff line number Diff line change
@@ -118,6 +118,8 @@ pub use core::slice::{SplitMut, ChunksMut, Split};
pub use core::slice::{SplitN, RSplitN, SplitNMut, RSplitNMut};
#[stable(feature = "rust1", since = "1.0.0")]
pub use core::slice::{from_raw_parts, from_raw_parts_mut};
#[unstable(feature = "slice_get_slice", issue = "35729")]
pub use core::slice::SliceIndex;

////////////////////////////////////////////////////////////////////////////////
// Basic slice extension methods
@@ -353,7 +355,9 @@ impl<T> [T] {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn get(&self, index: usize) -> Option<&T> {
pub fn get<I>(&self, index: I) -> Option<&I::Output>
where I: SliceIndex<T>
{
core_slice::SliceExt::get(self, index)
}

@@ -372,7 +376,9 @@ impl<T> [T] {
/// or `None` if the index is out of bounds
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
pub fn get_mut<I>(&mut self, index: I) -> Option<&mut I::Output>
where I: SliceIndex<T>
{
core_slice::SliceExt::get_mut(self, index)
}

@@ -390,7 +396,9 @@ impl<T> [T] {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub unsafe fn get_unchecked(&self, index: usize) -> &T {
pub unsafe fn get_unchecked<I>(&self, index: I) -> &I::Output
where I: SliceIndex<T>
{
core_slice::SliceExt::get_unchecked(self, index)
}

@@ -410,7 +418,9 @@ impl<T> [T] {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T {
pub unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Output
where I: SliceIndex<T>
{
core_slice::SliceExt::get_unchecked_mut(self, index)
}

499 changes: 331 additions & 168 deletions src/libcore/slice.rs

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions src/libcoretest/slice.rs
Original file line number Diff line number Diff line change
@@ -180,3 +180,47 @@ fn test_windows_last() {
let c2 = v2.windows(2);
assert_eq!(c2.last().unwrap()[0], 3);
}

#[test]
fn get_range() {
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
assert_eq!(v.get(..), Some(&[0, 1, 2, 3, 4, 5][..]));
assert_eq!(v.get(..2), Some(&[0, 1][..]));
assert_eq!(v.get(2..), Some(&[2, 3, 4, 5][..]));
assert_eq!(v.get(1..4), Some(&[1, 2, 3][..]));
assert_eq!(v.get(7..), None);
assert_eq!(v.get(7..10), None);
}

#[test]
fn get_mut_range() {
let mut v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
assert_eq!(v.get_mut(..), Some(&mut [0, 1, 2, 3, 4, 5][..]));
assert_eq!(v.get_mut(..2), Some(&mut [0, 1][..]));
assert_eq!(v.get_mut(2..), Some(&mut [2, 3, 4, 5][..]));
assert_eq!(v.get_mut(1..4), Some(&mut [1, 2, 3][..]));
assert_eq!(v.get_mut(7..), None);
assert_eq!(v.get_mut(7..10), None);
}

#[test]
fn get_unchecked_range() {
unsafe {
let v: &[i32] = &[0, 1, 2, 3, 4, 5];
assert_eq!(v.get_unchecked(..), &[0, 1, 2, 3, 4, 5][..]);
assert_eq!(v.get_unchecked(..2), &[0, 1][..]);
assert_eq!(v.get_unchecked(2..), &[2, 3, 4, 5][..]);
assert_eq!(v.get_unchecked(1..4), &[1, 2, 3][..]);
}
}

#[test]
fn get_unchecked_mut_range() {
unsafe {
let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5];
assert_eq!(v.get_unchecked_mut(..), &mut [0, 1, 2, 3, 4, 5][..]);
assert_eq!(v.get_unchecked_mut(..2), &mut [0, 1][..]);
assert_eq!(v.get_unchecked_mut(2..), &mut[2, 3, 4, 5][..]);
assert_eq!(v.get_unchecked_mut(1..4), &mut [1, 2, 3][..]);
}
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/indexing-requires-a-uint.rs
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@

fn main() {
fn bar<T>(_: T) {}
[0][0u8]; //~ ERROR: `[{integer}]: std::ops::Index<u8>` is not satisfied
[0][0u8]; //~ ERROR: the trait bound `u8: std::slice::SliceIndex<{integer}>` is not satisfied

[0][0]; // should infer to be a usize

8 changes: 4 additions & 4 deletions src/test/compile-fail/integral-indexing.rs
Original file line number Diff line number Diff line change
@@ -19,8 +19,8 @@ pub fn main() {
v[3i32]; //~ERROR : std::ops::Index<i32>` is not satisfied
s.as_bytes()[3_usize];
s.as_bytes()[3];
s.as_bytes()[3u8]; //~ERROR : std::ops::Index<u8>` is not satisfied
s.as_bytes()[3i8]; //~ERROR : std::ops::Index<i8>` is not satisfied
s.as_bytes()[3u32]; //~ERROR : std::ops::Index<u32>` is not satisfied
s.as_bytes()[3i32]; //~ERROR : std::ops::Index<i32>` is not satisfied
s.as_bytes()[3u8]; //~ERROR : std::slice::SliceIndex<u8>` is not satisfied
s.as_bytes()[3i8]; //~ERROR : std::slice::SliceIndex<u8>` is not satisfied
s.as_bytes()[3u32]; //~ERROR : std::slice::SliceIndex<u8>` is not satisfied
s.as_bytes()[3i32]; //~ERROR : std::slice::SliceIndex<u8>` is not satisfied
}
17 changes: 9 additions & 8 deletions src/test/compile-fail/on-unimplemented/slice-index.rs
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
// except according to those terms.

// Test new Index error message for slices
// ignore-tidy-linelength

#![feature(rustc_attrs)]

@@ -17,12 +18,12 @@ use std::ops::Index;
#[rustc_error]
fn main() {
let x = &[1, 2, 3] as &[i32];
x[1i32];
//~^ ERROR E0277
//~| NOTE the trait `std::ops::Index<i32>` is not implemented for `[i32]`
//~| NOTE slice indices are of type `usize`
x[..1i32];
//~^ ERROR E0277
//~| NOTE the trait `std::ops::Index<std::ops::RangeTo<i32>>` is not implemented for `[i32]`
//~| NOTE slice indices are of type `usize`
x[1i32]; //~ ERROR E0277
//~| NOTE slice indices are of type `usize` or ranges of `usize`
//~| NOTE trait `std::slice::SliceIndex<i32>` is not implemented for `i32`
//~| NOTE required because of the requirements on the impl of `std::ops::Index<i32>`
x[..1i32]; //~ ERROR E0277
//~| NOTE slice indices are of type `usize` or ranges of `usize`
//~| NOTE trait `std::slice::SliceIndex<i32>` is not implemented for `std::ops::RangeTo<i32>`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tidy failed, line longer than 100 chars (should not impact the crater run though).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, fixed.

//~| NOTE requirements on the impl of `std::ops::Index<std::ops::RangeTo<i32>>`
}