Skip to content

Commit

Permalink
Override nth for VecDeque Iter and IterMut
Browse files Browse the repository at this point in the history
  • Loading branch information
crgl committed Oct 11, 2019
1 parent 10671f1 commit d21eeb1
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/liballoc/collections/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2286,6 +2286,16 @@ impl<'a, T> Iterator for Iter<'a, T> {
final_res
}

fn nth(&mut self, n: usize) -> Option<Self::Item> {
if n >= count(self.tail, self.head, self.ring.len()) {
self.tail = self.head;
None
} else {
self.tail = wrap_index(self.tail.wrapping_add(n), self.ring.len());
self.next()
}
}

#[inline]
fn last(mut self) -> Option<&'a T> {
self.next_back()
Expand Down Expand Up @@ -2404,6 +2414,16 @@ impl<'a, T> Iterator for IterMut<'a, T> {
back.iter_mut().fold(accum, &mut f)
}

fn nth(&mut self, n: usize) -> Option<Self::Item> {
if n >= count(self.tail, self.head, self.ring.len()) {
self.tail = self.head;
None
} else {
self.tail = wrap_index(self.tail.wrapping_add(n), self.ring.len());
self.next()
}
}

#[inline]
fn last(mut self) -> Option<&'a mut T> {
self.next_back()
Expand Down

0 comments on commit d21eeb1

Please sign in to comment.