-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Add ExactSizeIterator impls for Hash{Map, Set, Table} #20915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ use self::BucketState::*; | |
use clone::Clone; | ||
use cmp; | ||
use hash::{Hash, Hasher}; | ||
use iter::{Iterator, count}; | ||
use iter::{Iterator, ExactSizeIterator, count}; | ||
use marker::{Copy, Sized, self}; | ||
use mem::{min_align_of, size_of}; | ||
use mem; | ||
|
@@ -838,10 +838,13 @@ impl<'a, K, V> Iterator for Iter<'a, K, V> { | |
}) | ||
} | ||
|
||
fn size_hint(&self) -> (uint, Option<uint>) { | ||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
(self.elems_left, Some(self.elems_left)) | ||
} | ||
} | ||
impl<'a, K, V> ExactSizeIterator for Iter<'a, K, V> { | ||
fn len(&self) -> usize { self.elems_left } | ||
} | ||
|
||
impl<'a, K, V> Iterator for IterMut<'a, K, V> { | ||
type Item = (&'a K, &'a mut V); | ||
|
@@ -856,10 +859,13 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> { | |
}) | ||
} | ||
|
||
fn size_hint(&self) -> (uint, Option<uint>) { | ||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
(self.elems_left, Some(self.elems_left)) | ||
} | ||
} | ||
impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> { | ||
fn len(&self) -> usize { self.elems_left } | ||
} | ||
|
||
impl<K, V> Iterator for IntoIter<K, V> { | ||
type Item = (SafeHash, K, V); | ||
|
@@ -879,13 +885,16 @@ impl<K, V> Iterator for IntoIter<K, V> { | |
}) | ||
} | ||
|
||
fn size_hint(&self) -> (uint, Option<uint>) { | ||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
let size = self.table.size(); | ||
(size, Some(size)) | ||
} | ||
} | ||
impl<K, V> ExactSizeIterator for IntoIter<K, V> { | ||
fn len(&self) -> usize { self.table.size() } | ||
} | ||
|
||
impl<'a, K: 'a, V: 'a> Iterator for Drain<'a, K, V> { | ||
impl<'a, K, V> Iterator for Drain<'a, K, V> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ! Have the rules changed, or were these always legitmately uneccessary? I guess because they're returned by-value? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think these lifetime bounds were ever truly necessary because they are given in the struct definition (I don't think I've ever seen lifetime bounds in an iterator impl like this before, anyway.) |
||
type Item = (SafeHash, K, V); | ||
|
||
#[inline] | ||
|
@@ -904,11 +913,14 @@ impl<'a, K: 'a, V: 'a> Iterator for Drain<'a, K, V> { | |
}) | ||
} | ||
|
||
fn size_hint(&self) -> (uint, Option<uint>) { | ||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
let size = self.table.size(); | ||
(size, Some(size)) | ||
} | ||
} | ||
impl<'a, K, V> ExactSizeIterator for Drain<'a, K, V> { | ||
fn len(&self) -> usize { self.table.size() } | ||
} | ||
|
||
#[unsafe_destructor] | ||
impl<'a, K: 'a, V: 'a> Drop for Drain<'a, K, V> { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this is strictly necessary, but sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I figured I'd avoid the unnecessary
size_hint()
unwrap by just calling straight down, but these can be removed if desired.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the strictly more correct thing, although if T: ExactSize, then size_hint is probably trivially inlineable to discard the upper-bound creation. This is fine as is; we need a better story for impl forwarding anyway (but thanks for soldiering through all this in the interim!)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem! And I'll be happy to bring this in line with impl forwarding conventions if and when we get them if this happens to run afoul of them.