Skip to content

Commit

Permalink
Merge pull request rust-lang#2 from weiznich/improve_iterators
Browse files Browse the repository at this point in the history
Improve implementation of iterators
  • Loading branch information
bluss authored Oct 11, 2016
2 parents f004545 + 5155603 commit 9dc7c0c
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,18 @@ impl<'a, K, V> Iterator for Keys<'a, K, V> {
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}

fn count(self) -> usize {
self.iter.len()
}

fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.iter.nth(n).map(|ent| &ent.key)
}

fn last(mut self) -> Option<Self::Item> {
self.next_back()
}
}

impl<'a, K, V> DoubleEndedIterator for Keys<'a, K, V> {
Expand All @@ -1061,6 +1073,18 @@ impl<'a, K, V> Iterator for Iter<'a, K, V> {
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}

fn count(self) -> usize {
self.iter.len()
}

fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.iter.nth(n).map(|e| (&e.key, &e.value))
}

fn last(mut self) -> Option<Self::Item> {
self.next_back()
}
}

impl<'a, K, V> DoubleEndedIterator for Iter<'a, K, V> {
Expand All @@ -1083,6 +1107,18 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> {
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}

fn count(self) -> usize {
self.iter.len()
}

fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.iter.nth(n).map(|e| (&e.key, &mut e.value))
}

fn last(mut self) -> Option<Self::Item> {
self.next_back()
}
}

impl<'a, K, V> DoubleEndedIterator for IterMut<'a, K, V> {
Expand All @@ -1105,6 +1141,18 @@ impl<K, V> Iterator for IntoIter<K, V> {
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}

fn count(self) -> usize {
self.iter.len()
}

fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.iter.nth(n).map(|e| (e.key, e.value))
}

fn last(mut self) -> Option<Self::Item> {
self.next_back()
}
}

impl<'a, K, V> DoubleEndedIterator for IntoIter<K, V> {
Expand Down

0 comments on commit 9dc7c0c

Please sign in to comment.