-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Use slicing syntax instead of methods and add a feature gate #17620
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,24 +995,24 @@ 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); | ||
assert_eq!(v_a[1], 3); | ||
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); | ||
assert_eq!(v_b[1], 3); | ||
|
||
// 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]); | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm curious why this new scope is needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its because we can't take a mutable slice (because we do a mutable iter later) and we can't take an immutable slice of a mutable variable (#17293), so we need to get an immutable version of |
||
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; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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))] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this temporary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes |
||
#[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, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you undo the deprecation, please remove the
ignore
here. (And in general, better to add a silent#[allow(deprecated)]
but otherwise keep the test.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some reason I couldn't add attributes to this code snippet - I tried to allow slicing syntax but there was no way I could get it to work.