diff options
| author | bors <bors@rust-lang.org> | 2014-10-02 03:07:17 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-10-02 03:07:17 +0000 |
| commit | 07b2c1be9dad53272575844efedfb7314fc4fb84 (patch) | |
| tree | d26dab000c4444cf6e972bb30da0392e675ca52b /src/libcollections | |
| parent | d53874eccf0657d5d9c0a9ed9f84380d27d1c423 (diff) | |
| parent | 6e0611a48707a1f5d90aee32a02b2b15957ef25b (diff) | |
| download | rust-07b2c1be9dad53272575844efedfb7314fc4fb84.tar.gz rust-07b2c1be9dad53272575844efedfb7314fc4fb84.zip | |
auto merge of #17620 : nick29581/rust/slice4, r=aturon
cc @aturon r? anyone?
Diffstat (limited to 'src/libcollections')
| -rw-r--r-- | src/libcollections/bitv.rs | 2 | ||||
| -rw-r--r-- | src/libcollections/lib.rs | 3 | ||||
| -rw-r--r-- | src/libcollections/ringbuf.rs | 4 | ||||
| -rw-r--r-- | src/libcollections/slice.rs | 63 | ||||
| -rw-r--r-- | src/libcollections/str.rs | 4 | ||||
| -rw-r--r-- | src/libcollections/string.rs | 35 | ||||
| -rw-r--r-- | src/libcollections/trie.rs | 21 | ||||
| -rw-r--r-- | src/libcollections/vec.rs | 110 |
8 files changed, 181 insertions, 61 deletions
diff --git a/src/libcollections/bitv.rs b/src/libcollections/bitv.rs index 60c9dfcff18..9f26c098013 100644 --- a/src/libcollections/bitv.rs +++ b/src/libcollections/bitv.rs @@ -194,7 +194,7 @@ impl Bitv { if start > self.storage.len() { start = self.storage.len(); } - let mut iter = self.storage.slice_from(start).iter(); + let mut iter = self.storage[start..].iter(); MaskWords { next_word: iter.next(), iter: iter, diff --git a/src/libcollections/lib.rs b/src/libcollections/lib.rs index 8b9a0ec796e..4d0aaf83907 100644 --- a/src/libcollections/lib.rs +++ b/src/libcollections/lib.rs @@ -19,8 +19,9 @@ html_root_url = "http://doc.rust-lang.org/master/", html_playground_url = "http://play.rust-lang.org/")] +#![allow(unknown_features)] #![feature(macro_rules, managed_boxes, default_type_params, phase, globs)] -#![feature(unsafe_destructor, import_shadowing)] +#![feature(unsafe_destructor, import_shadowing, slicing_syntax)] #![no_std] #[phase(plugin, link)] extern crate core; diff --git a/src/libcollections/ringbuf.rs b/src/libcollections/ringbuf.rs index 02c8af2c470..0e1c39535be 100644 --- a/src/libcollections/ringbuf.rs +++ b/src/libcollections/ringbuf.rs @@ -271,7 +271,7 @@ impl<T> RingBuf<T> { /// *num = *num - 2; /// } /// let b: &[_] = &[&mut 3, &mut 1, &mut 2]; - /// assert_eq!(buf.iter_mut().collect::<Vec<&mut int>>().as_slice(), b); + /// assert_eq!(buf.iter_mut().collect::<Vec<&mut int>>()[], b); /// ``` pub fn iter_mut<'a>(&'a mut self) -> MutItems<'a, T> { let start_index = raw_index(self.lo, self.elts.len(), 0); @@ -291,7 +291,7 @@ impl<T> RingBuf<T> { } else { // Items to iterate goes from start_index to end_index: let (empty, elts) = self.elts.split_at_mut(0); - let remaining1 = elts.slice_mut(start_index, end_index); + let remaining1 = elts[mut start_index..end_index]; MutItems { remaining1: remaining1, remaining2: empty, nelts: self.nelts } diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs index 45489bbf84e..253375aabe8 100644 --- a/src/libcollections/slice.rs +++ b/src/libcollections/slice.rs @@ -44,15 +44,20 @@ //! //! A number of traits add methods that allow you to accomplish tasks with slices. //! These traits include `ImmutableSlice`, which is defined for `&[T]` types, -//! and `MutableSlice`, defined for `&mut [T]` types. +//! `MutableSlice`, defined for `&mut [T]` types, and `Slice` and `SliceMut` +//! which are defined for `[T]`. //! -//! An example is the method `.slice(a, b)` that returns an immutable "view" into -//! a `Vec` or another slice from the index interval `[a, b)`: +//! An example is the `slice` method which enables slicing syntax `[a..b]` that +//! returns an immutable "view" into a `Vec` or another slice from the index +//! interval `[a, b)`: //! //! ```rust -//! let numbers = [0i, 1i, 2i]; -//! let last_numbers = numbers.slice(1, 3); -//! // last_numbers is now &[1i, 2i] +//! #![feature(slicing_syntax)] +//! fn main() { +//! let numbers = [0i, 1i, 2i]; +//! let last_numbers = numbers[1..3]; +//! // last_numbers is now &[1i, 2i] +//! } //! ``` //! //! ## Implementations of other traits @@ -610,7 +615,7 @@ impl<'a,T> MutableSliceAllocating<'a, T> for &'a mut [T] { #[inline] fn move_from(self, mut src: Vec<T>, start: uint, end: uint) -> uint { - for (a, b) in self.iter_mut().zip(src.slice_mut(start, end).iter_mut()) { + for (a, b) in self.iter_mut().zip(src[mut start..end].iter_mut()) { mem::swap(a, b); } cmp::min(self.len(), end-start) @@ -702,7 +707,7 @@ impl<'a, T: Ord> MutableOrdSlice<T> for &'a mut [T] { self.swap(j, i-1); // Step 4: Reverse the (previously) weakly decreasing part - self.slice_from_mut(i).reverse(); + self[mut i..].reverse(); true } @@ -723,7 +728,7 @@ impl<'a, T: Ord> MutableOrdSlice<T> for &'a mut [T] { } // Step 2: Reverse the weakly increasing part - self.slice_from_mut(i).reverse(); + self[mut i..].reverse(); // Step 3: Find the rightmost element equal to or bigger than the pivot (i-1) let mut j = self.len() - 1; @@ -990,7 +995,7 @@ mod tests { fn test_slice() { // Test fixed length vector. let vec_fixed = [1i, 2, 3, 4]; - let v_a = vec_fixed.slice(1u, vec_fixed.len()).to_vec(); + let v_a = vec_fixed[1u..vec_fixed.len()].to_vec(); assert_eq!(v_a.len(), 3u); let v_a = v_a.as_slice(); assert_eq!(v_a[0], 2); @@ -998,8 +1003,8 @@ mod tests { assert_eq!(v_a[2], 4); // Test on stack. - let vec_stack = &[1i, 2, 3]; - let v_b = vec_stack.slice(1u, 3u).to_vec(); + let vec_stack: &[_] = &[1i, 2, 3]; + let v_b = vec_stack[1u..3u].to_vec(); assert_eq!(v_b.len(), 2u); let v_b = v_b.as_slice(); assert_eq!(v_b[0], 2); @@ -1007,7 +1012,7 @@ mod tests { // Test `Box<[T]>` let vec_unique = vec![1i, 2, 3, 4, 5, 6]; - let v_d = vec_unique.slice(1u, 6u).to_vec(); + let v_d = vec_unique[1u..6u].to_vec(); assert_eq!(v_d.len(), 5u); let v_d = v_d.as_slice(); assert_eq!(v_d[0], 2); @@ -1020,21 +1025,21 @@ mod tests { #[test] fn test_slice_from() { let vec: &[int] = &[1, 2, 3, 4]; - assert_eq!(vec.slice_from(0), vec); + assert_eq!(vec[0..], vec); let b: &[int] = &[3, 4]; - assert_eq!(vec.slice_from(2), b); + assert_eq!(vec[2..], b); let b: &[int] = &[]; - assert_eq!(vec.slice_from(4), b); + assert_eq!(vec[4..], b); } #[test] fn test_slice_to() { let vec: &[int] = &[1, 2, 3, 4]; - assert_eq!(vec.slice_to(4), vec); + assert_eq!(vec[..4], vec); let b: &[int] = &[1, 2]; - assert_eq!(vec.slice_to(2), b); + assert_eq!(vec[..2], b); let b: &[int] = &[]; - assert_eq!(vec.slice_to(0), b); + assert_eq!(vec[..0], b); } @@ -1975,7 +1980,7 @@ mod tests { assert!(a == [7i,2,3,4]); let mut a = [1i,2,3,4,5]; let b = vec![5i,6,7,8,9,0]; - assert_eq!(a.slice_mut(2,4).move_from(b,1,6), 2); + assert_eq!(a[mut 2..4].move_from(b,1,6), 2); assert!(a == [1i,2,6,7,5]); } @@ -1995,7 +2000,7 @@ mod tests { #[test] fn test_reverse_part() { let mut values = [1i,2,3,4,5]; - values.slice_mut(1, 4).reverse(); + values[mut 1..4].reverse(); assert!(values == [1,4,3,2,5]); } @@ -2042,9 +2047,9 @@ mod tests { fn test_bytes_set_memory() { use slice::bytes::MutableByteVector; let mut values = [1u8,2,3,4,5]; - values.slice_mut(0,5).set_memory(0xAB); + values[mut 0..5].set_memory(0xAB); assert!(values == [0xAB, 0xAB, 0xAB, 0xAB, 0xAB]); - values.slice_mut(2,4).set_memory(0xFF); + values[mut 2..4].set_memory(0xFF); assert!(values == [0xAB, 0xAB, 0xFF, 0xFF, 0xAB]); } @@ -2070,12 +2075,18 @@ mod tests { let mut values = [1u8,2,3,4,5]; { let (left, right) = values.split_at_mut(2); - assert!(left.slice(0, left.len()) == [1, 2]); + { + let left: &[_] = left; + assert!(left[0..left.len()] == [1, 2]); + } for p in left.iter_mut() { *p += 1; } - assert!(right.slice(0, right.len()) == [3, 4, 5]); + { + let right: &[_] = right; + assert!(right[0..right.len()] == [3, 4, 5]); + } for p in right.iter_mut() { *p += 2; } @@ -2099,7 +2110,7 @@ mod tests { } assert_eq!(cnt, 3); - for f in v.slice(1, 3).iter() { + for f in v[1..3].iter() { assert!(*f == Foo); cnt += 1; } diff --git a/src/libcollections/str.rs b/src/libcollections/str.rs index d198e948ac8..11bda25fee5 100644 --- a/src/libcollections/str.rs +++ b/src/libcollections/str.rs @@ -1680,7 +1680,7 @@ mod tests { let mut bytes = [0u8, ..4]; for c in range(0u32, 0x110000).filter_map(|c| ::core::char::from_u32(c)) { let len = c.encode_utf8(bytes).unwrap_or(0); - let s = ::core::str::from_utf8(bytes.slice_to(len)).unwrap(); + let s = ::core::str::from_utf8(bytes[..len]).unwrap(); if Some(c) != s.chars().next() { fail!("character {:x}={} does not decode correctly", c as u32, c); } @@ -1692,7 +1692,7 @@ mod tests { let mut bytes = [0u8, ..4]; for c in range(0u32, 0x110000).filter_map(|c| ::core::char::from_u32(c)) { let len = c.encode_utf8(bytes).unwrap_or(0); - let s = ::core::str::from_utf8(bytes.slice_to(len)).unwrap(); + let s = ::core::str::from_utf8(bytes[..len]).unwrap(); if Some(c) != s.chars().rev().next() { fail!("character {:x}={} does not decode correctly", c as u32, c); } diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index d6adbd30264..1032a504330 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -160,7 +160,7 @@ impl String { if i > 0 { unsafe { - res.as_mut_vec().push_all(v.slice_to(i)) + res.as_mut_vec().push_all(v[..i]) }; } @@ -177,7 +177,7 @@ impl String { macro_rules! error(() => ({ unsafe { if subseqidx != i_ { - res.as_mut_vec().push_all(v.slice(subseqidx, i_)); + res.as_mut_vec().push_all(v[subseqidx..i_]); } subseqidx = i; res.as_mut_vec().push_all(REPLACEMENT); @@ -246,7 +246,7 @@ impl String { } if subseqidx < total { unsafe { - res.as_mut_vec().push_all(v.slice(subseqidx, total)) + res.as_mut_vec().push_all(v[subseqidx..total]) }; } Owned(res.into_string()) @@ -927,6 +927,7 @@ impl<S: Str> Add<S, String> for String { } } +#[cfg(stage0)] impl ops::Slice<uint, str> for String { #[inline] fn as_slice_<'a>(&'a self) -> &'a str { @@ -949,6 +950,34 @@ impl ops::Slice<uint, str> for String { } } +#[cfg(not(stage0))] +#[inline] +fn str_to_slice<'a, U: Str>(this: &'a U) -> &'a str { + this.as_slice() +} +#[cfg(not(stage0))] +impl ops::Slice<uint, str> for String { + #[inline] + fn as_slice<'a>(&'a self) -> &'a str { + str_to_slice(self) + } + + #[inline] + fn slice_from<'a>(&'a self, from: &uint) -> &'a str { + self[][*from..] + } + + #[inline] + fn slice_to<'a>(&'a self, to: &uint) -> &'a str { + self[][..*to] + } + + #[inline] + fn slice<'a>(&'a self, from: &uint, to: &uint) -> &'a str { + self[][*from..*to] + } +} + /// Unsafe operations #[unstable = "waiting on raw module conventions"] pub mod raw { diff --git a/src/libcollections/trie.rs b/src/libcollections/trie.rs index e9981790f7d..9bfc8e08d8d 100644 --- a/src/libcollections/trie.rs +++ b/src/libcollections/trie.rs @@ -24,6 +24,7 @@ use core::fmt; use core::fmt::Show; use core::mem::zeroed; use core::mem; +use core::ops::{Slice,SliceMut}; use core::uint; use core::iter; use std::hash::{Writer, Hash}; @@ -378,7 +379,7 @@ macro_rules! bound { } }; // push to the stack. - it.stack[it.length] = children.$slice_from(slice_idx).$iter(); + it.stack[it.length] = children.$slice_from(&slice_idx).$iter(); it.length += 1; if ret { return it } }) @@ -388,6 +389,15 @@ macro_rules! bound { impl<T> TrieMap<T> { // If `upper` is true then returns upper_bound else returns lower_bound. + #[cfg(stage0)] + #[inline] + fn bound<'a>(&'a self, key: uint, upper: bool) -> Entries<'a, T> { + bound!(Entries, self = self, + key = key, is_upper = upper, + slice_from = slice_from_, iter = iter, + mutability = ) + } + #[cfg(not(stage0))] #[inline] fn bound<'a>(&'a self, key: uint, upper: bool) -> Entries<'a, T> { bound!(Entries, self = self, @@ -430,6 +440,15 @@ impl<T> TrieMap<T> { self.bound(key, true) } // If `upper` is true then returns upper_bound else returns lower_bound. + #[cfg(stage0)] + #[inline] + fn bound_mut<'a>(&'a mut self, key: uint, upper: bool) -> MutEntries<'a, T> { + bound!(MutEntries, self = self, + key = key, is_upper = upper, + slice_from = slice_from_mut_, iter = iter_mut, + mutability = mut) + } + #[cfg(not(stage0))] #[inline] fn bound_mut<'a>(&'a mut self, key: uint, upper: bool) -> MutEntries<'a, T> { bound!(MutEntries, self = self, diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index 280fbdeffa2..9dc122cfc7d 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -24,6 +24,7 @@ use core::num; use core::ops; use core::ptr; use core::raw::Slice as RawSlice; +use core::slice::Slice as SliceSlice; use core::uint; use {Mutable, MutableSeq}; @@ -438,7 +439,7 @@ impl<T:Clone> Clone for Vec<T> { // self.len <= other.len due to the truncate above, so the // slice here is always in-bounds. - let slice = other.slice_from(self.len()); + let slice = other[self.len()..]; self.push_all(slice); } } @@ -460,6 +461,37 @@ impl<T> Index<uint,T> for Vec<T> { } }*/ +// Annoying helper function because there are two Slice::as_slice functions in +// scope. +#[cfg(not(stage0))] +#[inline] +fn slice_to_slice<'a, T, U: Slice<T>>(this: &'a U) -> &'a [T] { + this.as_slice() +} + + +#[cfg(not(stage0))] +impl<T> ops::Slice<uint, [T]> for Vec<T> { + #[inline] + fn as_slice<'a>(&'a self) -> &'a [T] { + slice_to_slice(self) + } + + #[inline] + fn slice_from<'a>(&'a self, start: &uint) -> &'a [T] { + slice_to_slice(self).slice_from(start) + } + + #[inline] + fn slice_to<'a>(&'a self, end: &uint) -> &'a [T] { + slice_to_slice(self).slice_to(end) + } + #[inline] + fn slice<'a>(&'a self, start: &uint, end: &uint) -> &'a [T] { + slice_to_slice(self).slice(start, end) + } +} +#[cfg(stage0)] impl<T> ops::Slice<uint, [T]> for Vec<T> { #[inline] fn as_slice_<'a>(&'a self) -> &'a [T] { @@ -481,6 +513,28 @@ impl<T> ops::Slice<uint, [T]> for Vec<T> { } } +#[cfg(not(stage0))] +impl<T> ops::SliceMut<uint, [T]> for Vec<T> { + #[inline] + fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T] { + self.as_mut_slice() + } + + #[inline] + fn slice_from_mut<'a>(&'a mut self, start: &uint) -> &'a mut [T] { + self.as_mut_slice().slice_from_mut(start) + } + + #[inline] + fn slice_to_mut<'a>(&'a mut self, end: &uint) -> &'a mut [T] { + self.as_mut_slice().slice_to_mut(end) + } + #[inline] + fn slice_mut<'a>(&'a mut self, start: &uint, end: &uint) -> &'a mut [T] { + self.as_mut_slice().slice_mut(start, end) + } +} +#[cfg(stage0)] impl<T> ops::SliceMut<uint, [T]> for Vec<T> { #[inline] fn as_mut_slice_<'a>(&'a mut self) -> &'a mut [T] { @@ -928,11 +982,11 @@ impl<T> Vec<T> { /// /// ``` /// let vec = vec![1i, 2, 3, 4]; - /// assert!(vec.slice(0, 2) == [1, 2]); + /// assert!(vec[0..2] == [1, 2]); /// ``` #[inline] pub fn slice<'a>(&'a self, start: uint, end: uint) -> &'a [T] { - self.as_slice().slice(start, end) + self[start..end] } /// Returns a slice containing all but the first element of the vector. @@ -949,7 +1003,7 @@ impl<T> Vec<T> { /// ``` #[inline] pub fn tail<'a>(&'a self) -> &'a [T] { - self.as_slice().tail() + self[].tail() } /// Returns all but the first `n' elements of a vector. @@ -968,7 +1022,7 @@ impl<T> Vec<T> { #[inline] #[deprecated = "use slice_from"] pub fn tailn<'a>(&'a self, n: uint) -> &'a [T] { - self.as_slice().slice_from(n) + self[n..] } /// Returns a reference to the last element of a vector, or `None` if it is @@ -982,7 +1036,7 @@ impl<T> Vec<T> { /// ``` #[inline] pub fn last<'a>(&'a self) -> Option<&'a T> { - self.as_slice().last() + self[].last() } /// Deprecated: use `last_mut`. @@ -1176,10 +1230,10 @@ impl<T> Vec<T> { } /// Deprecated: use `slice_mut`. - #[deprecated = "use slice_mut"] + #[deprecated = "use slice_from"] pub fn mut_slice<'a>(&'a mut self, start: uint, end: uint) -> &'a mut [T] { - self.slice_mut(start, end) + self[mut start..end] } /// Returns a mutable slice of `self` between `start` and `end`. @@ -1193,18 +1247,18 @@ impl<T> Vec<T> { /// /// ``` /// let mut vec = vec![1i, 2, 3, 4]; - /// assert!(vec.slice_mut(0, 2) == [1, 2]); + /// assert!(vec[mut 0..2] == [1, 2]); /// ``` #[inline] pub fn slice_mut<'a>(&'a mut self, start: uint, end: uint) -> &'a mut [T] { - self.as_mut_slice().slice_mut(start, end) + self[mut start..end] } /// Deprecated: use "slice_from_mut". #[deprecated = "use slice_from_mut"] pub fn mut_slice_from<'a>(&'a mut self, start: uint) -> &'a mut [T] { - self.slice_from_mut(start) + self[mut start..] } /// Returns a mutable slice of `self` from `start` to the end of the `Vec`. @@ -1217,17 +1271,17 @@ impl<T> Vec<T> { /// /// ``` /// let mut vec = vec![1i, 2, 3, 4]; - /// assert!(vec.slice_from_mut(2) == [3, 4]); + /// assert!(vec[mut 2..] == [3, 4]); /// ``` #[inline] pub fn slice_from_mut<'a>(&'a mut self, start: uint) -> &'a mut [T] { - self.as_mut_slice().slice_from_mut(start) + self[mut start..] } /// Deprecated: use `slice_to_mut`. #[deprecated = "use slice_to_mut"] pub fn mut_slice_to<'a>(&'a mut self, end: uint) -> &'a mut [T] { - self.slice_to_mut(end) + self[mut ..end] } /// Returns a mutable slice of `self` from the start of the `Vec` to `end`. @@ -1240,11 +1294,11 @@ impl<T> Vec<T> { /// /// ``` /// let mut vec = vec![1i, 2, 3, 4]; - /// assert!(vec.slice_to_mut(2) == [1, 2]); + /// assert!(vec[mut ..2] == [1, 2]); /// ``` #[inline] pub fn slice_to_mut<'a>(&'a mut self, end: uint) -> &'a mut [T] { - self.as_mut_slice().slice_to_mut(end) + self[mut ..end] } /// Deprecated: use `split_at_mut`. @@ -1289,7 +1343,7 @@ impl<T> Vec<T> { /// ``` #[inline] pub fn split_at_mut<'a>(&'a mut self, mid: uint) -> (&'a mut [T], &'a mut [T]) { - self.as_mut_slice().split_at_mut(mid) + self[mut].split_at_mut(mid) } /// Reverses the order of elements in a vector, in place. @@ -1303,7 +1357,7 @@ impl<T> Vec<T> { /// ``` #[inline] pub fn reverse(&mut self) { - self.as_mut_slice().reverse() + self[mut].reverse() } /// Returns a slice of `self` from `start` to the end of the vec. @@ -1316,11 +1370,11 @@ impl<T> Vec<T> { /// /// ``` /// let vec = vec![1i, 2, 3]; - /// assert!(vec.slice_from(1) == [2, 3]); + /// assert!(vec[1..] == [2, 3]); /// ``` #[inline] pub fn slice_from<'a>(&'a self, start: uint) -> &'a [T] { - self.as_slice().slice_from(start) + self[start..] } /// Returns a slice of self from the start of the vec to `end`. @@ -1333,11 +1387,11 @@ impl<T> Vec<T> { /// /// ``` /// let vec = vec![1i, 2, 3, 4]; - /// assert!(vec.slice_to(2) == [1, 2]); + /// assert!(vec[..2] == [1, 2]); /// ``` #[inline] pub fn slice_to<'a>(&'a self, end: uint) -> &'a [T] { - self.as_slice().slice_to(end) + self[..end] } /// Returns a slice containing all but the last element of the vector. @@ -1354,7 +1408,7 @@ impl<T> Vec<T> { /// ``` #[inline] pub fn init<'a>(&'a self) -> &'a [T] { - self.slice(0, self.len() - 1) + self[0..self.len() - 1] } @@ -2212,12 +2266,18 @@ mod tests { let mut values = Vec::from_slice([1u8,2,3,4,5]); { let (left, right) = values.split_at_mut(2); - assert!(left.slice(0, left.len()) == [1, 2]); + { + let left: &[_] = left; + assert!(left[0..left.len()] == [1, 2]); + } for p in left.iter_mut() { *p += 1; } - assert!(right.slice(0, right.len()) == [3, 4, 5]); + { + let right: &[_] = right; + assert!(right[0..right.len()] == [3, 4, 5]); + } for p in right.iter_mut() { *p += 2; } |
