Skip to content

Commit e62ef37

Browse files
committed
auto merge of rust-lang#17807 : nick29581/rust/slice6, r=aturon
r? @aturon
2 parents 8d70216 + eb2fdc8 commit e62ef37

File tree

123 files changed

+663
-405
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+663
-405
lines changed

src/compiletest/compiletest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
#![crate_type = "bin"]
12-
#![feature(phase)]
12+
#![feature(phase, slicing_syntax)]
1313

1414
#![deny(warnings)]
1515

src/compiletest/runtest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ fn check_error_patterns(props: &TestProps,
874874
if done { return; }
875875

876876
let missing_patterns =
877-
props.error_patterns.slice(next_err_idx, props.error_patterns.len());
877+
props.error_patterns[next_err_idx..];
878878
if missing_patterns.len() == 1u {
879879
fatal_proc_rec(format!("error pattern '{}' not found!",
880880
missing_patterns[0]).as_slice(),

src/doc/reference.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3837,7 +3837,7 @@ type signature of `print`, and the cast expression in `main`.
38373837
Within the body of an item that has type parameter declarations, the names of
38383838
its type parameters are types:
38393839

3840-
```
3840+
```ignore
38413841
fn map<A: Clone, B: Clone>(f: |A| -> B, xs: &[A]) -> Vec<B> {
38423842
if xs.len() == 0 {
38433843
return vec![];

src/libcollections/bitv.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl Bitv {
194194
if start > self.storage.len() {
195195
start = self.storage.len();
196196
}
197-
let mut iter = self.storage.slice_from(start).iter();
197+
let mut iter = self.storage[start..].iter();
198198
MaskWords {
199199
next_word: iter.next(),
200200
iter: iter,

src/libcollections/hash/sip.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ mod tests {
273273

274274
use str::Str;
275275
use string::String;
276-
use slice::{Slice, ImmutableSlice};
276+
use slice::{AsSlice, ImmutableSlice};
277277
use vec::Vec;
278278

279279
use super::super::{Hash, Writer};

src/libcollections/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
html_root_url = "http://doc.rust-lang.org/master/",
2020
html_playground_url = "http://play.rust-lang.org/")]
2121

22+
#![allow(unknown_features)]
2223
#![feature(macro_rules, default_type_params, phase, globs)]
23-
#![feature(unsafe_destructor, import_shadowing)]
24+
#![feature(unsafe_destructor, import_shadowing, slicing_syntax)]
2425
#![no_std]
2526

2627
#[phase(plugin, link)] extern crate core;

src/libcollections/ringbuf.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ impl<T> RingBuf<T> {
271271
/// *num = *num - 2;
272272
/// }
273273
/// let b: &[_] = &[&mut 3, &mut 1, &mut 2];
274-
/// assert_eq!(buf.iter_mut().collect::<Vec<&mut int>>().as_slice(), b);
274+
/// assert_eq!(buf.iter_mut().collect::<Vec<&mut int>>()[], b);
275275
/// ```
276276
pub fn iter_mut<'a>(&'a mut self) -> MutItems<'a, T> {
277277
let start_index = raw_index(self.lo, self.elts.len(), 0);
@@ -291,7 +291,7 @@ impl<T> RingBuf<T> {
291291
} else {
292292
// Items to iterate goes from start_index to end_index:
293293
let (empty, elts) = self.elts.split_at_mut(0);
294-
let remaining1 = elts.slice_mut(start_index, end_index);
294+
let remaining1 = elts[mut start_index..end_index];
295295
MutItems { remaining1: remaining1,
296296
remaining2: empty,
297297
nelts: self.nelts }

src/libcollections/slice.rs

+39-28
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,20 @@
4444
//!
4545
//! A number of traits add methods that allow you to accomplish tasks with slices.
4646
//! These traits include `ImmutableSlice`, which is defined for `&[T]` types,
47-
//! and `MutableSlice`, defined for `&mut [T]` types.
47+
//! `MutableSlice`, defined for `&mut [T]` types, and `Slice` and `SliceMut`
48+
//! which are defined for `[T]`.
4849
//!
49-
//! An example is the method `.slice(a, b)` that returns an immutable "view" into
50-
//! a `Vec` or another slice from the index interval `[a, b)`:
50+
//! An example is the `slice` method which enables slicing syntax `[a..b]` that
51+
//! returns an immutable "view" into a `Vec` or another slice from the index
52+
//! interval `[a, b)`:
5153
//!
5254
//! ```rust
53-
//! let numbers = [0i, 1i, 2i];
54-
//! let last_numbers = numbers.slice(1, 3);
55-
//! // last_numbers is now &[1i, 2i]
55+
//! #![feature(slicing_syntax)]
56+
//! fn main() {
57+
//! let numbers = [0i, 1i, 2i];
58+
//! let last_numbers = numbers[1..3];
59+
//! // last_numbers is now &[1i, 2i]
60+
//! }
5661
//! ```
5762
//!
5863
//! ## Implementations of other traits
@@ -93,7 +98,7 @@ use core::iter::{range_step, MultiplicativeIterator};
9398
use MutableSeq;
9499
use vec::Vec;
95100

96-
pub use core::slice::{Chunks, Slice, ImmutableSlice, ImmutablePartialEqSlice};
101+
pub use core::slice::{Chunks, AsSlice, ImmutableSlice, ImmutablePartialEqSlice};
97102
pub use core::slice::{ImmutableOrdSlice, MutableSlice, Items, MutItems};
98103
pub use core::slice::{MutSplits, MutChunks, Splits};
99104
pub use core::slice::{bytes, mut_ref_slice, ref_slice, MutableCloneableSlice};
@@ -112,7 +117,7 @@ pub trait VectorVector<T> {
112117
fn connect_vec(&self, sep: &T) -> Vec<T>;
113118
}
114119

115-
impl<'a, T: Clone, V: Slice<T>> VectorVector<T> for &'a [V] {
120+
impl<'a, T: Clone, V: AsSlice<T>> VectorVector<T> for &'a [V] {
116121
fn concat_vec(&self) -> Vec<T> {
117122
let size = self.iter().fold(0u, |acc, v| acc + v.as_slice().len());
118123
let mut result = Vec::with_capacity(size);
@@ -610,7 +615,7 @@ impl<'a,T> MutableSliceAllocating<'a, T> for &'a mut [T] {
610615

611616
#[inline]
612617
fn move_from(self, mut src: Vec<T>, start: uint, end: uint) -> uint {
613-
for (a, b) in self.iter_mut().zip(src.slice_mut(start, end).iter_mut()) {
618+
for (a, b) in self.iter_mut().zip(src[mut start..end].iter_mut()) {
614619
mem::swap(a, b);
615620
}
616621
cmp::min(self.len(), end-start)
@@ -702,7 +707,7 @@ impl<'a, T: Ord> MutableOrdSlice<T> for &'a mut [T] {
702707
self.swap(j, i-1);
703708

704709
// Step 4: Reverse the (previously) weakly decreasing part
705-
self.slice_from_mut(i).reverse();
710+
self[mut i..].reverse();
706711

707712
true
708713
}
@@ -723,7 +728,7 @@ impl<'a, T: Ord> MutableOrdSlice<T> for &'a mut [T] {
723728
}
724729

725730
// Step 2: Reverse the weakly increasing part
726-
self.slice_from_mut(i).reverse();
731+
self[mut i..].reverse();
727732

728733
// Step 3: Find the rightmost element equal to or bigger than the pivot (i-1)
729734
let mut j = self.len() - 1;
@@ -990,24 +995,24 @@ mod tests {
990995
fn test_slice() {
991996
// Test fixed length vector.
992997
let vec_fixed = [1i, 2, 3, 4];
993-
let v_a = vec_fixed.slice(1u, vec_fixed.len()).to_vec();
998+
let v_a = vec_fixed[1u..vec_fixed.len()].to_vec();
994999
assert_eq!(v_a.len(), 3u);
9951000
let v_a = v_a.as_slice();
9961001
assert_eq!(v_a[0], 2);
9971002
assert_eq!(v_a[1], 3);
9981003
assert_eq!(v_a[2], 4);
9991004

10001005
// Test on stack.
1001-
let vec_stack = &[1i, 2, 3];
1002-
let v_b = vec_stack.slice(1u, 3u).to_vec();
1006+
let vec_stack: &[_] = &[1i, 2, 3];
1007+
let v_b = vec_stack[1u..3u].to_vec();
10031008
assert_eq!(v_b.len(), 2u);
10041009
let v_b = v_b.as_slice();
10051010
assert_eq!(v_b[0], 2);
10061011
assert_eq!(v_b[1], 3);
10071012

10081013
// Test `Box<[T]>`
10091014
let vec_unique = vec![1i, 2, 3, 4, 5, 6];
1010-
let v_d = vec_unique.slice(1u, 6u).to_vec();
1015+
let v_d = vec_unique[1u..6u].to_vec();
10111016
assert_eq!(v_d.len(), 5u);
10121017
let v_d = v_d.as_slice();
10131018
assert_eq!(v_d[0], 2);
@@ -1020,21 +1025,21 @@ mod tests {
10201025
#[test]
10211026
fn test_slice_from() {
10221027
let vec: &[int] = &[1, 2, 3, 4];
1023-
assert_eq!(vec.slice_from(0), vec);
1028+
assert_eq!(vec[0..], vec);
10241029
let b: &[int] = &[3, 4];
1025-
assert_eq!(vec.slice_from(2), b);
1030+
assert_eq!(vec[2..], b);
10261031
let b: &[int] = &[];
1027-
assert_eq!(vec.slice_from(4), b);
1032+
assert_eq!(vec[4..], b);
10281033
}
10291034

10301035
#[test]
10311036
fn test_slice_to() {
10321037
let vec: &[int] = &[1, 2, 3, 4];
1033-
assert_eq!(vec.slice_to(4), vec);
1038+
assert_eq!(vec[..4], vec);
10341039
let b: &[int] = &[1, 2];
1035-
assert_eq!(vec.slice_to(2), b);
1040+
assert_eq!(vec[..2], b);
10361041
let b: &[int] = &[];
1037-
assert_eq!(vec.slice_to(0), b);
1042+
assert_eq!(vec[..0], b);
10381043
}
10391044

10401045

@@ -1975,7 +1980,7 @@ mod tests {
19751980
assert!(a == [7i,2,3,4]);
19761981
let mut a = [1i,2,3,4,5];
19771982
let b = vec![5i,6,7,8,9,0];
1978-
assert_eq!(a.slice_mut(2,4).move_from(b,1,6), 2);
1983+
assert_eq!(a[mut 2..4].move_from(b,1,6), 2);
19791984
assert!(a == [1i,2,6,7,5]);
19801985
}
19811986

@@ -1995,7 +2000,7 @@ mod tests {
19952000
#[test]
19962001
fn test_reverse_part() {
19972002
let mut values = [1i,2,3,4,5];
1998-
values.slice_mut(1, 4).reverse();
2003+
values[mut 1..4].reverse();
19992004
assert!(values == [1,4,3,2,5]);
20002005
}
20012006

@@ -2042,9 +2047,9 @@ mod tests {
20422047
fn test_bytes_set_memory() {
20432048
use slice::bytes::MutableByteVector;
20442049
let mut values = [1u8,2,3,4,5];
2045-
values.slice_mut(0,5).set_memory(0xAB);
2050+
values[mut 0..5].set_memory(0xAB);
20462051
assert!(values == [0xAB, 0xAB, 0xAB, 0xAB, 0xAB]);
2047-
values.slice_mut(2,4).set_memory(0xFF);
2052+
values[mut 2..4].set_memory(0xFF);
20482053
assert!(values == [0xAB, 0xAB, 0xFF, 0xFF, 0xAB]);
20492054
}
20502055

@@ -2070,12 +2075,18 @@ mod tests {
20702075
let mut values = [1u8,2,3,4,5];
20712076
{
20722077
let (left, right) = values.split_at_mut(2);
2073-
assert!(left.slice(0, left.len()) == [1, 2]);
2078+
{
2079+
let left: &[_] = left;
2080+
assert!(left[0..left.len()] == [1, 2]);
2081+
}
20742082
for p in left.iter_mut() {
20752083
*p += 1;
20762084
}
20772085

2078-
assert!(right.slice(0, right.len()) == [3, 4, 5]);
2086+
{
2087+
let right: &[_] = right;
2088+
assert!(right[0..right.len()] == [3, 4, 5]);
2089+
}
20792090
for p in right.iter_mut() {
20802091
*p += 2;
20812092
}
@@ -2099,7 +2110,7 @@ mod tests {
20992110
}
21002111
assert_eq!(cnt, 3);
21012112

2102-
for f in v.slice(1, 3).iter() {
2113+
for f in v[1..3].iter() {
21032114
assert!(*f == Foo);
21042115
cnt += 1;
21052116
}

src/libcollections/str.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use core::iter::AdditiveIterator;
6161
use core::mem;
6262
use core::prelude::{Char, Clone, Collection, Eq, Equiv, ImmutableSlice};
6363
use core::prelude::{Iterator, MutableSlice, None, Option, Ord, Ordering};
64-
use core::prelude::{PartialEq, PartialOrd, Result, Slice, Some, Tuple2};
64+
use core::prelude::{PartialEq, PartialOrd, Result, AsSlice, Some, Tuple2};
6565
use core::prelude::{range};
6666

6767
use {Deque, MutableSeq};
@@ -880,7 +880,7 @@ mod tests {
880880
use {Collection, MutableSeq};
881881

882882
use super::*;
883-
use std::slice::{Slice, ImmutableSlice};
883+
use std::slice::{AsSlice, ImmutableSlice};
884884
use string::String;
885885
use vec::Vec;
886886

@@ -1678,7 +1678,7 @@ mod tests {
16781678
let mut bytes = [0u8, ..4];
16791679
for c in range(0u32, 0x110000).filter_map(|c| ::core::char::from_u32(c)) {
16801680
let len = c.encode_utf8(bytes).unwrap_or(0);
1681-
let s = ::core::str::from_utf8(bytes.slice_to(len)).unwrap();
1681+
let s = ::core::str::from_utf8(bytes[..len]).unwrap();
16821682
if Some(c) != s.chars().next() {
16831683
fail!("character {:x}={} does not decode correctly", c as u32, c);
16841684
}
@@ -1690,7 +1690,7 @@ mod tests {
16901690
let mut bytes = [0u8, ..4];
16911691
for c in range(0u32, 0x110000).filter_map(|c| ::core::char::from_u32(c)) {
16921692
let len = c.encode_utf8(bytes).unwrap_or(0);
1693-
let s = ::core::str::from_utf8(bytes.slice_to(len)).unwrap();
1693+
let s = ::core::str::from_utf8(bytes[..len]).unwrap();
16941694
if Some(c) != s.chars().rev().next() {
16951695
fail!("character {:x}={} does not decode correctly", c as u32, c);
16961696
}

src/libcollections/string.rs

+26-3
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl String {
160160

161161
if i > 0 {
162162
unsafe {
163-
res.as_mut_vec().push_all(v.slice_to(i))
163+
res.as_mut_vec().push_all(v[..i])
164164
};
165165
}
166166

@@ -177,7 +177,7 @@ impl String {
177177
macro_rules! error(() => ({
178178
unsafe {
179179
if subseqidx != i_ {
180-
res.as_mut_vec().push_all(v.slice(subseqidx, i_));
180+
res.as_mut_vec().push_all(v[subseqidx..i_]);
181181
}
182182
subseqidx = i;
183183
res.as_mut_vec().push_all(REPLACEMENT);
@@ -246,7 +246,7 @@ impl String {
246246
}
247247
if subseqidx < total {
248248
unsafe {
249-
res.as_mut_vec().push_all(v.slice(subseqidx, total))
249+
res.as_mut_vec().push_all(v[subseqidx..total])
250250
};
251251
}
252252
Owned(res.into_string())
@@ -928,6 +928,7 @@ impl<S: Str> Add<S, String> for String {
928928
}
929929
}
930930

931+
#[cfg(stage0)]
931932
impl ops::Slice<uint, str> for String {
932933
#[inline]
933934
fn as_slice_<'a>(&'a self) -> &'a str {
@@ -949,6 +950,28 @@ impl ops::Slice<uint, str> for String {
949950
self[][*from..*to]
950951
}
951952
}
953+
#[cfg(not(stage0))]
954+
impl ops::Slice<uint, str> for String {
955+
#[inline]
956+
fn as_slice_<'a>(&'a self) -> &'a str {
957+
self.as_slice()
958+
}
959+
960+
#[inline]
961+
fn slice_from_or_fail<'a>(&'a self, from: &uint) -> &'a str {
962+
self[][*from..]
963+
}
964+
965+
#[inline]
966+
fn slice_to_or_fail<'a>(&'a self, to: &uint) -> &'a str {
967+
self[][..*to]
968+
}
969+
970+
#[inline]
971+
fn slice_or_fail<'a>(&'a self, from: &uint, to: &uint) -> &'a str {
972+
self[][*from..*to]
973+
}
974+
}
952975

953976
/// Unsafe operations
954977
#[unstable = "waiting on raw module conventions"]

0 commit comments

Comments
 (0)