diff options
| author | bors <bors@rust-lang.org> | 2013-07-22 13:49:34 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-07-22 13:49:34 -0700 |
| commit | 9ed82fbb43804ebc7e06daca5812079630ec8952 (patch) | |
| tree | 44fd77dc33a7ca6a876b3e3f2056717684f6ca79 /src/libstd | |
| parent | 2f7d86f9a86f0da23579eef50ba1275d36e6c0bc (diff) | |
| parent | 30f13e661a8dc2281b4c57484629f03c096f24f1 (diff) | |
| download | rust-9ed82fbb43804ebc7e06daca5812079630ec8952.tar.gz rust-9ed82fbb43804ebc7e06daca5812079630ec8952.zip | |
auto merge of #7943 : Dretch/rust/vec-slice-from-to, r=huonw
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/vec.rs | 50 |
1 files changed, 47 insertions, 3 deletions
diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 6c9d3c15b9e..baeb87e51b9 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -689,6 +689,8 @@ impl<'self,T:Clone> CopyableVector<T> for &'self [T] { #[allow(missing_doc)] pub trait ImmutableVector<'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 rev_iter(self) -> VecRevIterator<'self, T>; fn split_iter(self, pred: &'self fn(&T) -> bool) -> VecSplitIterator<'self, T>; @@ -720,11 +722,17 @@ pub trait ImmutableVector<'self, T> { /// Extension methods for vectors 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] fn slice(&self, start: uint, end: uint) -> &'self [T] { - assert!(start <= end); - assert!(end <= self.len()); + assert!(start <= end); + assert!(end <= self.len()); do self.as_imm_buf |p, _len| { unsafe { transmute((ptr::offset(p, start), @@ -733,6 +741,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] fn iter(self) -> VecIterator<'self, T> { unsafe { @@ -2454,6 +2482,22 @@ mod tests { } #[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] fn test_pop() { // Test on-heap pop. let mut v = ~[1, 2, 3, 4, 5]; |
