Add slice_from and slice_to methods for vec, like the
methods of the same names that already exist for strs.
This commit is contained in:
@@ -702,6 +702,8 @@ impl<'self,T:Clone> CopyableVector<T> for &'self [T] {
|
|||||||
#[allow(missing_doc)]
|
#[allow(missing_doc)]
|
||||||
pub trait ImmutableVector<'self, T> {
|
pub trait ImmutableVector<'self, T> {
|
||||||
fn slice(&self, start: uint, end: uint) -> &'self [T];
|
fn slice(&self, start: uint, end: uint) -> &'self [T];
|
||||||
|
fn slice_from(&self, start: uint) -> &'self [T];
|
||||||
|
fn slice_to(&self, end: uint) -> &'self [T];
|
||||||
fn iter(self) -> VecIterator<'self, T>;
|
fn iter(self) -> VecIterator<'self, T>;
|
||||||
fn rev_iter(self) -> VecRevIterator<'self, T>;
|
fn rev_iter(self) -> VecRevIterator<'self, T>;
|
||||||
fn split_iter(self, pred: &'self fn(&T) -> bool) -> VecSplitIterator<'self, T>;
|
fn split_iter(self, pred: &'self fn(&T) -> bool) -> VecSplitIterator<'self, T>;
|
||||||
@@ -733,7 +735,13 @@ pub trait ImmutableVector<'self, T> {
|
|||||||
|
|
||||||
/// Extension methods for vectors
|
/// Extension methods for vectors
|
||||||
impl<'self,T> ImmutableVector<'self, T> for &'self [T] {
|
impl<'self,T> ImmutableVector<'self, T> for &'self [T] {
|
||||||
/// Return a slice that points into another slice.
|
|
||||||
|
/**
|
||||||
|
* Returns a slice of self between `start` and `end`.
|
||||||
|
*
|
||||||
|
* Fails when `start` or `end` point outside the bounds of self,
|
||||||
|
* or when `start` > `end`.
|
||||||
|
*/
|
||||||
#[inline]
|
#[inline]
|
||||||
fn slice(&self, start: uint, end: uint) -> &'self [T] {
|
fn slice(&self, start: uint, end: uint) -> &'self [T] {
|
||||||
assert!(start <= end);
|
assert!(start <= end);
|
||||||
@@ -746,6 +754,26 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a slice of self from `start` to the end of the vec.
|
||||||
|
*
|
||||||
|
* Fails when `start` points outside the bounds of self.
|
||||||
|
*/
|
||||||
|
#[inline]
|
||||||
|
fn slice_from(&self, start: uint) -> &'self [T] {
|
||||||
|
self.slice(start, self.len())
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a slice of self from the start of the vec to `end`.
|
||||||
|
*
|
||||||
|
* Fails when `end` points outside the bounds of self.
|
||||||
|
*/
|
||||||
|
#[inline]
|
||||||
|
fn slice_to(&self, end: uint) -> &'self [T] {
|
||||||
|
self.slice(0, end)
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn iter(self) -> VecIterator<'self, T> {
|
fn iter(self) -> VecIterator<'self, T> {
|
||||||
unsafe {
|
unsafe {
|
||||||
@@ -2570,6 +2598,22 @@ mod tests {
|
|||||||
assert_eq!(v_d[4], 6);
|
assert_eq!(v_d[4], 6);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_slice_from() {
|
||||||
|
let vec = &[1, 2, 3, 4];
|
||||||
|
assert_eq!(vec.slice_from(0), vec);
|
||||||
|
assert_eq!(vec.slice_from(2), &[3, 4]);
|
||||||
|
assert_eq!(vec.slice_from(4), &[]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_slice_to() {
|
||||||
|
let vec = &[1, 2, 3, 4];
|
||||||
|
assert_eq!(vec.slice_to(4), vec);
|
||||||
|
assert_eq!(vec.slice_to(2), &[1, 2]);
|
||||||
|
assert_eq!(vec.slice_to(0), &[]);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_pop() {
|
fn test_pop() {
|
||||||
// Test on-heap pop.
|
// Test on-heap pop.
|
||||||
|
|||||||
Reference in New Issue
Block a user