about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2015-02-15 18:36:49 +0530
committerManish Goregaokar <manishsmail@gmail.com>2015-02-15 18:42:48 +0530
commitb1a46db649d1eabce7c1e2f6d69ff7af09d72bc1 (patch)
treec35f9ddbf07ef9825619ea78811bd8d4f237f94b /src
parent97503e1c1e3febedcd8f528a86d1da85cf9584c8 (diff)
parent1c494046a51e2e75126a12d60a9e0a07153e9fd6 (diff)
downloadrust-b1a46db649d1eabce7c1e2f6d69ff7af09d72bc1.tar.gz
rust-b1a46db649d1eabce7c1e2f6d69ff7af09d72bc1.zip
Rollup merge of #22247 - Gankro:dlist_split, r=alexcrichton
Diffstat (limited to 'src')
-rw-r--r--src/libcollections/dlist.rs21
1 files changed, 20 insertions, 1 deletions
diff --git a/src/libcollections/dlist.rs b/src/libcollections/dlist.rs
index a080146e0ec..abe206c3914 100644
--- a/src/libcollections/dlist.rs
+++ b/src/libcollections/dlist.rs
@@ -560,7 +560,12 @@ impl<T> DList<T> {
     /// Splits the list into two at the given index. Returns everything after the given index,
     /// including the index.
     ///
+    /// # Panics
+    ///
+    /// Panics if `at > len`.
+    ///
     /// This operation should compute in O(n) time.
+    ///
     /// # Examples
     ///
     /// ```
@@ -580,9 +585,11 @@ impl<T> DList<T> {
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn split_off(&mut self, at: usize) -> DList<T> {
         let len = self.len();
-        assert!(at < len, "Cannot split off at a nonexistent index");
+        assert!(at <= len, "Cannot split off at a nonexistent index");
         if at == 0 {
             return mem::replace(self, DList::new());
+        } else if at == len {
+            return DList::new();
         }
 
         // Below, we iterate towards the `i-1`th node, either from the start or the end,
@@ -1116,6 +1123,18 @@ mod tests {
             }
         }
 
+        // no-op on the last index
+        {
+            let mut m = DList::new();
+            m.push_back(1);
+
+            let p = m.split_off(1);
+            assert_eq!(m.len(), 1);
+            assert_eq!(p.len(), 0);
+            assert_eq!(m.back(), Some(&1));
+            assert_eq!(m.front(), Some(&1));
+        }
+
     }
 
     #[test]