Deprecate slicing methods in favor of notation

This commit deprecates `slice`, `slice_from`, `slice_to` and their
mutable variants in favor of slice notation.

The `as_slice` methods are left intact, for now.

[breaking-change]
This commit is contained in:
Aaron Turon
2015-01-17 16:15:47 -08:00
parent fba0bf63a9
commit 092ba6a856
5 changed files with 115 additions and 185 deletions

View File

@@ -67,9 +67,6 @@ use raw::Slice as RawSlice;
pub trait SliceExt {
type Item;
fn slice<'a>(&'a self, start: uint, end: uint) -> &'a [Self::Item];
fn slice_from<'a>(&'a self, start: uint) -> &'a [Self::Item];
fn slice_to<'a>(&'a self, end: uint) -> &'a [Self::Item];
fn split_at<'a>(&'a self, mid: uint) -> (&'a [Self::Item], &'a [Self::Item]);
fn iter<'a>(&'a self) -> Iter<'a, Self::Item>;
fn split<'a, P>(&'a self, pred: P) -> Split<'a, Self::Item, P>
@@ -93,9 +90,6 @@ pub trait SliceExt {
fn is_empty(&self) -> bool { self.len() == 0 }
fn get_mut<'a>(&'a mut self, index: uint) -> Option<&'a mut Self::Item>;
fn as_mut_slice<'a>(&'a mut self) -> &'a mut [Self::Item];
fn slice_mut<'a>(&'a mut self, start: uint, end: uint) -> &'a mut [Self::Item];
fn slice_from_mut<'a>(&'a mut self, start: uint) -> &'a mut [Self::Item];
fn slice_to_mut<'a>(&'a mut self, end: uint) -> &'a mut [Self::Item];
fn iter_mut<'a>(&'a mut self) -> IterMut<'a, Self::Item>;
fn first_mut<'a>(&'a mut self) -> Option<&'a mut Self::Item>;
fn tail_mut<'a>(&'a mut self) -> &'a mut [Self::Item];
@@ -135,28 +129,6 @@ pub trait SliceExt {
impl<T> SliceExt for [T] {
type Item = T;
#[inline]
fn slice(&self, start: uint, end: uint) -> &[T] {
assert!(start <= end);
assert!(end <= self.len());
unsafe {
transmute(RawSlice {
data: self.as_ptr().offset(start as int),
len: (end - start)
})
}
}
#[inline]
fn slice_from(&self, start: uint) -> &[T] {
self.slice(start, self.len())
}
#[inline]
fn slice_to(&self, end: uint) -> &[T] {
self.slice(0, end)
}
#[inline]
fn split_at(&self, mid: uint) -> (&[T], &[T]) {
(&self[..mid], &self[mid..])
@@ -291,20 +263,6 @@ impl<T> SliceExt for [T] {
#[inline]
fn as_mut_slice(&mut self) -> &mut [T] { self }
fn slice_mut(&mut self, start: uint, end: uint) -> &mut [T] {
ops::IndexMut::index_mut(self, &ops::Range { start: start, end: end } )
}
#[inline]
fn slice_from_mut(&mut self, start: uint) -> &mut [T] {
ops::IndexMut::index_mut(self, &ops::RangeFrom { start: start } )
}
#[inline]
fn slice_to_mut(&mut self, end: uint) -> &mut [T] {
ops::IndexMut::index_mut(self, &ops::RangeTo { end: end } )
}
#[inline]
fn split_at_mut(&mut self, mid: uint) -> (&mut [T], &mut [T]) {
unsafe {
@@ -345,13 +303,13 @@ impl<T> SliceExt for [T] {
#[inline]
fn tail_mut(&mut self) -> &mut [T] {
self.slice_from_mut(1)
&mut self[1 ..]
}
#[inline]
fn init_mut(&mut self) -> &mut [T] {
let len = self.len();
self.slice_to_mut(len-1)
&mut self[.. (len - 1)]
}
#[inline]
@@ -483,7 +441,7 @@ impl<T> SliceExt for [T] {
self.swap(j, i-1);
// Step 4: Reverse the (previously) weakly decreasing part
self.slice_from_mut(i).reverse();
self[i..].reverse();
true
}
@@ -505,7 +463,7 @@ impl<T> SliceExt for [T] {
}
// Step 2: Reverse the weakly increasing part
self.slice_from_mut(i).reverse();
self[i..].reverse();
// Step 3: Find the rightmost element equal to or bigger than the pivot (i-1)
let mut j = self.len() - 1;
@@ -522,8 +480,8 @@ impl<T> SliceExt for [T] {
#[inline]
fn clone_from_slice(&mut self, src: &[T]) -> uint where T: Clone {
let min = cmp::min(self.len(), src.len());
let dst = self.slice_to_mut(min);
let src = src.slice_to(min);
let dst = &mut self[.. min];
let src = &src[.. min];
for i in range(0, min) {
dst[i].clone_from(&src[i]);
}
@@ -531,6 +489,7 @@ impl<T> SliceExt for [T] {
}
}
#[stable]
impl<T> ops::Index<uint> for [T] {
type Output = T;
@@ -541,6 +500,7 @@ impl<T> ops::Index<uint> for [T] {
}
}
#[stable]
impl<T> ops::IndexMut<uint> for [T] {
type Output = T;
@@ -551,6 +511,7 @@ impl<T> ops::IndexMut<uint> for [T] {
}
}
#[stable]
impl<T> ops::Index<ops::Range<uint>> for [T] {
type Output = [T];
#[inline]
@@ -565,6 +526,7 @@ impl<T> ops::Index<ops::Range<uint>> for [T] {
}
}
}
#[stable]
impl<T> ops::Index<ops::RangeTo<uint>> for [T] {
type Output = [T];
#[inline]
@@ -572,6 +534,7 @@ impl<T> ops::Index<ops::RangeTo<uint>> for [T] {
self.index(&ops::Range{ start: 0, end: index.end })
}
}
#[stable]
impl<T> ops::Index<ops::RangeFrom<uint>> for [T] {
type Output = [T];
#[inline]
@@ -579,6 +542,7 @@ impl<T> ops::Index<ops::RangeFrom<uint>> for [T] {
self.index(&ops::Range{ start: index.start, end: self.len() })
}
}
#[stable]
impl<T> ops::Index<ops::FullRange> for [T] {
type Output = [T];
#[inline]
@@ -587,6 +551,7 @@ impl<T> ops::Index<ops::FullRange> for [T] {
}
}
#[stable]
impl<T> ops::IndexMut<ops::Range<uint>> for [T] {
type Output = [T];
#[inline]
@@ -601,6 +566,7 @@ impl<T> ops::IndexMut<ops::Range<uint>> for [T] {
}
}
}
#[stable]
impl<T> ops::IndexMut<ops::RangeTo<uint>> for [T] {
type Output = [T];
#[inline]
@@ -608,6 +574,7 @@ impl<T> ops::IndexMut<ops::RangeTo<uint>> for [T] {
self.index_mut(&ops::Range{ start: 0, end: index.end })
}
}
#[stable]
impl<T> ops::IndexMut<ops::RangeFrom<uint>> for [T] {
type Output = [T];
#[inline]
@@ -616,6 +583,7 @@ impl<T> ops::IndexMut<ops::RangeFrom<uint>> for [T] {
self.index_mut(&ops::Range{ start: index.start, end: len })
}
}
#[stable]
impl<T> ops::IndexMut<ops::FullRange> for [T] {
type Output = [T];
#[inline]
@@ -1051,7 +1019,7 @@ impl<'a, T, P> Iterator for SplitMut<'a, T, P> where P: FnMut(&T) -> bool {
Some(idx) => {
let tmp = mem::replace(&mut self.v, &mut []);
let (head, tail) = tmp.split_at_mut(idx);
self.v = tail.slice_from_mut(1);
self.v = &mut tail[1..];
Some(head)
}
}
@@ -1087,7 +1055,7 @@ impl<'a, T, P> DoubleEndedIterator for SplitMut<'a, T, P> where
let tmp = mem::replace(&mut self.v, &mut []);
let (head, tail) = tmp.split_at_mut(idx);
self.v = head;
Some(tail.slice_from_mut(1))
Some(&mut tail[1..])
}
}
}