diff options
| author | Aaron Turon <aturon@mozilla.com> | 2014-10-02 11:48:07 -0700 |
|---|---|---|
| committer | Aaron Turon <aturon@mozilla.com> | 2014-10-02 11:48:07 -0700 |
| commit | d2ea0315e09cbd495a67c9e3d5053b57e2b5a8b7 (patch) | |
| tree | d39de6be5866c0f0f37f9f3219b8217c873d8b52 /src/libcore/slice.rs | |
| parent | c0c6c895890770d7029324fd9b592f42e0564e8b (diff) | |
| download | rust-d2ea0315e09cbd495a67c9e3d5053b57e2b5a8b7.tar.gz rust-d2ea0315e09cbd495a67c9e3d5053b57e2b5a8b7.zip | |
Revert "Use slice syntax instead of slice_to, etc."
This reverts commit 40b9f5ded50ac4ce8c9323921ec556ad611af6b7.
Diffstat (limited to 'src/libcore/slice.rs')
| -rw-r--r-- | src/libcore/slice.rs | 136 |
1 files changed, 89 insertions, 47 deletions
diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index b8828d310f0..1a00107a216 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -68,7 +68,7 @@ pub trait ImmutableSlice<'a, T> { /// /// Slicing with `start` equal to `end` yields an empty slice. #[unstable = "waiting on final error conventions"] - //fn slice(&self, start: uint, end: uint) -> &'a [T]; + fn slice(&self, start: uint, end: uint) -> &'a [T]; /// Returns a subslice from `start` to the end of the slice. /// @@ -76,8 +76,7 @@ pub trait ImmutableSlice<'a, T> { /// /// Slicing from `self.len()` yields an empty slice. #[unstable = "waiting on final error conventions"] - // TODO - //fn slice_from(&self, start: uint) -> &'a [T]; + fn slice_from(&self, start: uint) -> &'a [T]; /// Returns a subslice from the start of the slice to `end`. /// @@ -85,7 +84,7 @@ pub trait ImmutableSlice<'a, T> { /// /// Slicing to `0` yields an empty slice. #[unstable = "waiting on final error conventions"] - //fn slice_to(&self, end: uint) -> &'a [T]; + fn slice_to(&self, end: uint) -> &'a [T]; /// Divides one slice into two at an index. /// @@ -178,7 +177,7 @@ pub trait ImmutableSlice<'a, T> { fn tail(&self) -> &'a [T]; /// Returns all but the first `n' elements of a slice. - #[deprecated = "use slicing syntax"] + #[deprecated = "use slice_from"] fn tailn(&self, n: uint) -> &'a [T]; /// Returns all but the last element of a slice. @@ -187,7 +186,6 @@ pub trait ImmutableSlice<'a, T> { /// Returns all but the last `n' elements of a slice. #[deprecated = "use slice_to but note the arguments are different"] - #[deprecated = "use slicing syntax, but note the arguments are different"] fn initn(&self, n: uint) -> &'a [T]; /// Returns the last element of a slice, or `None` if it is empty. @@ -242,7 +240,7 @@ pub trait ImmutableSlice<'a, T> { * ```ignore * if self.len() == 0 { return None } * let head = &self[0]; - * *self = self[1..]; + * *self = self.slice_from(1); * Some(head) * ``` * @@ -261,7 +259,7 @@ pub trait ImmutableSlice<'a, T> { * ```ignore * if self.len() == 0 { return None; } * let tail = &self[self.len() - 1]; - * *self = self[..self.len() - 1]; + * *self = self.slice_to(self.len() - 1); * Some(tail) * ``` * @@ -274,8 +272,30 @@ pub trait ImmutableSlice<'a, T> { #[unstable] impl<'a,T> ImmutableSlice<'a, T> for &'a [T] { #[inline] + fn slice(&self, start: uint, end: uint) -> &'a [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) -> &'a [T] { + self.slice(start, self.len()) + } + + #[inline] + fn slice_to(&self, end: uint) -> &'a [T] { + self.slice(0, end) + } + + #[inline] fn split_at(&self, mid: uint) -> (&'a [T], &'a [T]) { - ((*self)[..mid], (*self)[mid..]) + (self.slice(0, mid), self.slice(mid, self.len())) } #[inline] @@ -344,21 +364,21 @@ impl<'a,T> ImmutableSlice<'a, T> for &'a [T] { } #[inline] - fn tail(&self) -> &'a [T] { (*self)[1..] } + fn tail(&self) -> &'a [T] { self.slice(1, self.len()) } #[inline] - #[deprecated = "use slicing syntax"] - fn tailn(&self, n: uint) -> &'a [T] { (*self)[n..] } + #[deprecated = "use slice_from"] + fn tailn(&self, n: uint) -> &'a [T] { self.slice(n, self.len()) } #[inline] fn init(&self) -> &'a [T] { - (*self)[..self.len() - 1] + self.slice(0, self.len() - 1) } #[inline] - #[deprecated = "use slicing syntax but note the arguments are different"] + #[deprecated = "use slice_to but note the arguments are different"] fn initn(&self, n: uint) -> &'a [T] { - (*self)[..self.len() - n] + self.slice(0, self.len() - n) } #[inline] @@ -511,14 +531,13 @@ pub trait MutableSlice<'a, T> { fn get_mut(self, index: uint) -> Option<&'a mut T>; /// Work with `self` as a mut slice. /// Primarily intended for getting a &mut [T] from a [T, ..N]. - #[deprecated = "use slicing syntax"] fn as_mut_slice(self) -> &'a mut [T]; /// Deprecated: use `slice_mut`. - #[deprecated = "use slicing syntax"] - //fn mut_slice(self, start: uint, end: uint) -> &'a mut [T] { - // self[mut start..end] - //} + #[deprecated = "use slice_mut"] + fn mut_slice(self, start: uint, end: uint) -> &'a mut [T] { + self.slice_mut(start, end) + } /// Returns a mutable subslice spanning the interval [`start`, `end`). /// @@ -527,13 +546,13 @@ pub trait MutableSlice<'a, T> { /// /// Slicing with `start` equal to `end` yields an empty slice. #[unstable = "waiting on final error conventions"] - //fn slice_mut(self, start: uint, end: uint) -> &'a mut [T]; + fn slice_mut(self, start: uint, end: uint) -> &'a mut [T]; - /// Deprecated: use `slicing syntax`. - #[deprecated = "use slicing syntax"] - //fn mut_slice_from(self, start: uint) -> &'a mut [T] { - // self[mut start..] - //} + /// Deprecated: use `slice_from_mut`. + #[deprecated = "use slice_from_mut"] + fn mut_slice_from(self, start: uint) -> &'a mut [T] { + self.slice_from_mut(start) + } /// Returns a mutable subslice from `start` to the end of the slice. /// @@ -541,13 +560,13 @@ pub trait MutableSlice<'a, T> { /// /// Slicing from `self.len()` yields an empty slice. #[unstable = "waiting on final error conventions"] - //fn slice_from_mut(self, start: uint) -> &'a mut [T]; + fn slice_from_mut(self, start: uint) -> &'a mut [T]; - /// Deprecated: use `slicing syntax`. - #[deprecated = "use slicing syntax"] - //fn mut_slice_to(self, end: uint) -> &'a mut [T] { - // self[mut ..end] - //} + /// Deprecated: use `slice_to_mut`. + #[deprecated = "use slice_to_mut"] + fn mut_slice_to(self, end: uint) -> &'a mut [T] { + self.slice_to_mut(end) + } /// Returns a mutable subslice from the start of the slice to `end`. /// @@ -555,7 +574,7 @@ pub trait MutableSlice<'a, T> { /// /// Slicing to `0` yields an empty slice. #[unstable = "waiting on final error conventions"] - //fn slice_to_mut(self, end: uint) -> &'a mut [T]; + fn slice_to_mut(self, end: uint) -> &'a mut [T]; /// Deprecated: use `iter_mut`. #[deprecated = "use iter_mut"] @@ -640,7 +659,7 @@ pub trait MutableSlice<'a, T> { * ```ignore * if self.len() == 0 { return None; } * let head = &mut self[0]; - * *self = self[mut 1..]; + * *self = self.slice_from_mut(1); * Some(head) * ``` * @@ -659,7 +678,7 @@ pub trait MutableSlice<'a, T> { * ```ignore * if self.len() == 0 { return None; } * let tail = &mut self[self.len() - 1]; - * *self = self[mut ..self.len() - 1]; + * *self = self.slice_to_mut(self.len() - 1); * Some(tail) * ``` * @@ -784,11 +803,34 @@ impl<'a,T> MutableSlice<'a, T> for &'a mut [T] { #[inline] fn as_mut_slice(self) -> &'a mut [T] { self } + fn slice_mut(self, start: uint, end: uint) -> &'a mut [T] { + assert!(start <= end); + assert!(end <= self.len()); + unsafe { + transmute(RawSlice { + data: self.as_mut_ptr().offset(start as int) as *const T, + len: (end - start) + }) + } + } + + #[inline] + fn slice_from_mut(self, start: uint) -> &'a mut [T] { + let len = self.len(); + self.slice_mut(start, len) + } + + #[inline] + fn slice_to_mut(self, end: uint) -> &'a mut [T] { + self.slice_mut(0, end) + } + #[inline] fn split_at_mut(self, mid: uint) -> (&'a mut [T], &'a mut [T]) { unsafe { + let len = self.len(); let self2: &'a mut [T] = mem::transmute_copy(&self); - (self[mut ..mid], self2[mut mid..]) + (self.slice_mut(0, mid), self2.slice_mut(mid, len)) } } @@ -978,13 +1020,13 @@ impl<'a,T:PartialEq> ImmutablePartialEqSlice<T> for &'a [T] { #[inline] fn starts_with(&self, needle: &[T]) -> bool { let n = needle.len(); - self.len() >= n && needle == (*self)[..n] + self.len() >= n && needle == self.slice_to(n) } #[inline] fn ends_with(&self, needle: &[T]) -> bool { let (m, n) = (self.len(), needle.len()); - m >= n && needle == (*self)[m-n..] + m >= n && needle == self.slice_from(m - n) } } @@ -1257,8 +1299,8 @@ impl<'a, T> Iterator<&'a [T]> for Splits<'a, T> { match self.v.iter().position(|x| (self.pred)(x)) { None => self.finish(), Some(idx) => { - let ret = Some(self.v[..idx]); - self.v = self.v[idx + 1..]; + let ret = Some(self.v.slice(0, idx)); + self.v = self.v.slice(idx + 1, self.v.len()); ret } } @@ -1283,8 +1325,8 @@ impl<'a, T> DoubleEndedIterator<&'a [T]> for Splits<'a, T> { match self.v.iter().rposition(|x| (self.pred)(x)) { None => self.finish(), Some(idx) => { - let ret = Some(self.v[idx + 1..]); - self.v = self.v[..idx]; + let ret = Some(self.v.slice(idx + 1, self.v.len())); + self.v = self.v.slice(0, idx); ret } } @@ -1334,7 +1376,7 @@ impl<'a, T> Iterator<&'a mut [T]> for MutSplits<'a, T> { Some(idx) => { let tmp = mem::replace(&mut self.v, &mut []); let (head, tail) = tmp.split_at_mut(idx); - self.v = tail[mut 1..]; + self.v = tail.slice_from_mut(1); Some(head) } } @@ -1368,7 +1410,7 @@ impl<'a, T> DoubleEndedIterator<&'a mut [T]> for MutSplits<'a, T> { let tmp = mem::replace(&mut self.v, &mut []); let (head, tail) = tmp.split_at_mut(idx); self.v = head; - Some(tail[mut 1..]) + Some(tail.slice_from_mut(1)) } } } @@ -1416,8 +1458,8 @@ impl<'a, T> Iterator<&'a [T]> for Windows<'a, T> { if self.size > self.v.len() { None } else { - let ret = Some(self.v[..self.size]); - self.v = self.v[1..]; + let ret = Some(self.v.slice(0, self.size)); + self.v = self.v.slice(1, self.v.len()); ret } } @@ -1501,7 +1543,7 @@ impl<'a, T> RandomAccessIterator<&'a [T]> for Chunks<'a, T> { let mut hi = lo + self.size; if hi < lo || hi > self.v.len() { hi = self.v.len(); } - Some(self.v[lo..hi]) + Some(self.v.slice(lo, hi)) } else { None } |
