]> git.lizzy.rs Git - rust.git/commitdiff
dlist: Fix .peek_next() w.r.t double ended iterators
authorblake2-ppc <blake2-ppc>
Mon, 22 Jul 2013 15:51:11 +0000 (17:51 +0200)
committerblake2-ppc <blake2-ppc>
Mon, 22 Jul 2013 15:51:11 +0000 (17:51 +0200)
.peek_next() needs to check the element counter just like the .next()
and .next_back() iterators do.

Also clarify .insert_next() doc w.r.t double ended iteration.

src/libextra/dlist.rs

index a715d4aeeaeb431543b02cc3032fe89c49f529e9..276d80043148158c3012ba99df6c32853c84e71a 100644 (file)
@@ -477,7 +477,9 @@ fn next_back(&mut self) -> Option<&'self mut A> {
 
 /// Allow mutating the DList while iterating
 pub trait ListInsertion<A> {
-    /// Insert `elt` just after to the most recently yielded element
+    /// Insert `elt` just after to the element most recently returned by `.next()`
+    ///
+    /// The inserted element does not appear in the iteration.
     fn insert_next(&mut self, elt: A);
 
     /// Provide a reference to the next element, without changing the iterator
@@ -515,6 +517,9 @@ fn insert_next(&mut self, elt: A) {
 
     #[inline]
     fn peek_next<'a>(&'a mut self) -> Option<&'a mut A> {
+        if self.nelem == 0 {
+            return None
+        }
         self.head.resolve().map_consume(|head| &mut head.value)
     }
 }