about summary refs log tree commit diff
diff options
context:
space:
mode:
authornham <hamann.nick@gmail.com>2014-08-01 22:59:54 -0400
committernham <hamann.nick@gmail.com>2014-08-01 23:05:16 -0400
commit7e5440ea9d0045741663f142e83a247fe540599e (patch)
tree3781d29bcf296ca7dd968035283b76d84602e956
parentd7cfc34a222c0280670690be0d618b67014cc28d (diff)
downloadrust-7e5440ea9d0045741663f142e83a247fe540599e.tar.gz
rust-7e5440ea9d0045741663f142e83a247fe540599e.zip
Add a split_at method to slice::ImmutableVector
This method is similar to the mut_split_at method of slice::MutableVector.
-rw-r--r--src/libcore/slice.rs21
1 files changed, 17 insertions, 4 deletions
diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs
index 8197a7c2dcb..d3b761f9665 100644
--- a/src/libcore/slice.rs
+++ b/src/libcore/slice.rs
@@ -76,6 +76,16 @@ pub trait ImmutableVector<'a, T> {
      * Fails when `end` points outside the bounds of self.
      */
     fn slice_to(&self, end: uint) -> &'a [T];
+
+    /// Divides one slice into two at an index.
+    ///
+    /// The first will contain all indices from `[0, mid)` (excluding
+    /// the index `mid` itself) and the second will contain all
+    /// indices from `[mid, len)` (excluding the index `len` itself).
+    ///
+    /// Fails if `mid > len`.
+    fn split_at(&self, mid: uint) -> (&'a [T], &'a [T]);
+
     /// Returns an iterator over the vector
     fn iter(self) -> Items<'a, T>;
     /// Returns an iterator over the subslices of the vector which are
@@ -248,6 +258,11 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] {
     }
 
     #[inline]
+    fn split_at(&self, mid: uint) -> (&'a [T], &'a [T]) {
+        (self.slice(0, mid), self.slice(mid, self.len()))
+    }
+
+    #[inline]
     fn iter(self) -> Items<'a, T> {
         unsafe {
             let p = self.as_ptr();
@@ -1192,8 +1207,7 @@ impl<'a, T> Iterator<&'a [T]> for Chunks<'a, T> {
             None
         } else {
             let chunksz = cmp::min(self.v.len(), self.size);
-            let (fst, snd) = (self.v.slice_to(chunksz),
-                              self.v.slice_from(chunksz));
+            let (fst, snd) = self.v.split_at(chunksz);
             self.v = snd;
             Some(fst)
         }
@@ -1219,8 +1233,7 @@ impl<'a, T> DoubleEndedIterator<&'a [T]> for Chunks<'a, T> {
         } else {
             let remainder = self.v.len() % self.size;
             let chunksz = if remainder != 0 { remainder } else { self.size };
-            let (fst, snd) = (self.v.slice_to(self.v.len() - chunksz),
-                              self.v.slice_from(self.v.len() - chunksz));
+            let (fst, snd) = self.v.split_at(self.v.len() - chunksz);
             self.v = fst;
             Some(snd)
         }